This is a migrated thread and some comments may be shown as answers.

Pass a parameter value to a usercontrol

5 Answers 544 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Antony
Top achievements
Rank 1
Antony asked on 29 Oct 2008, 11:53 AM
HI,

Is there a way to pass a parameter value to a usercontrol that is displayed by the radgrid during editing.
When a user clicks on the edit command in a row I would like to pass the "ID" of the row to my usercontrol that is displayed.

Many thanks
Antony

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Oct 2008, 12:15 PM
Hello Antony,

You can set the DataKeyNames property as the ID and then access it when the grid is in EditMode as shown below.You can then pass the value to the UserControl accordingly.
aspx:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource2" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" > 
        <MasterTableView DataKeyNames="ID" DataSourceID="SqlDataSource2" > 

cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    {   
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
        { 
             
                GridEditFormItem item = e.Item as GridEditFormItem; 
                string ID = item.GetDataKeyValue("ID").ToString(); 
                UserControl user = item.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl; 
                // pass ID as required 
        } 
    } 

Thanks
Princy.
0
Antony
Top achievements
Rank 1
answered on 29 Oct 2008, 12:26 PM
Hi Princy,

Thanks for your quick response, I'm very new to all of this, if possible could you give me an example of how I would pass the ID value to the usercontrol and how to retrieve it during the Page_Load of the usercontrol.

Many thanks
Antony
0
Pether Wiklander
Top achievements
Rank 2
answered on 18 Dec 2008, 04:55 PM
I have the same problem as Antony. I want do place a grid in the UserControl and need to know the ID when NeedDataSource of the Grid is executed. ItemDataBound on the parent grid (parent of the UserControl) is too late.

Any suggestions?
0
Accepted
Georgi Krustev
Telerik team
answered on 19 Dec 2008, 11:37 AM
Hello guys,

@Antony: To attain the functionality you are searching for just put in the user control a property DataItem. When an item of the grid is in edit mode the DataItem property will be bound to the currently edited row data. From there you can get the current ID of the editable row.

Here is a code snippet:
public partial class YourUserControl: System.Web.UI.UserControl 
{    
 private object _dataItem; 
 
    public object DataItem 
    { 
        get  
        { 
            return _dataItem; 
        } 
        set  
        { 
            _dataItem = value; 
        } 
    } 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (_dataItem != null) ; 
        int propertyID = (int)((System.Data.DataRowView)(_dataItem)).Row.ItemArray[0]; //This property is assigned with  
                                                          //current DataRow object which is in edit mode.  
    } 

For more information how to use user control for edit template please refer to this link.

Furthermore, if the user control is used as an edit form you can get it only when a current item is in edit mode. Before that the user control is not loaded at all. The id of the current item is set in the DataItem property in your user control (see the previous code excerpt in this thread). Hence you do not need to pass it in some other way.

You are right about that user control's Page_Load event is fired earlier that the ItemDataBound event of the grid, but this is part of the normal lifecycle of ASP.NET page. So if you want to set some property in the user control you have to use its value after user control's Page_Load event is raised.

Best regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Abiri Masoni
Top achievements
Rank 1
answered on 25 Mar 2010, 12:10 PM
Hi!
I've put the code below in the .cs of my UserControl but am getting casting issues ie. Unable to cast object of type 'ARIMSLITE.BUSINESSENTITIES.EntityClasses.TblStudentsEntity' to type 'System.Data.DataRowView'.

private

 

object _dataItem;

 

 

public object DataItem

 

{

 

get

 

{

 

return _dataItem;

 

}

 

set

 

{

_dataItem =

value;

 

}

}

 

 

protected void Page_Load(object sender, EventArgs e)

 

{

 

//if (!Page.IsPostBack)

 

 

//{

 

 

RadTabStrip1.SelectedIndex = 0;

StudentDetailsPageview.SelectedIndex = 0;

 

LoadGuardianCombo();

LoadClassCombo();

 

if (_dataItem != null) ;

 

 

Guid propertyID = (Guid)((System.Data.DataRowView)(_dataItem)).Row.ItemArray[0]; //This property is assigned with

 

 

//current DataRow object which is in edit mode.

 

 

// LoadResCountryCombo();

 

 

// LoadDormCombo();

 

 

//}

 

 

}

Tags
Grid
Asked by
Antony
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Antony
Top achievements
Rank 1
Pether Wiklander
Top achievements
Rank 2
Georgi Krustev
Telerik team
Abiri Masoni
Top achievements
Rank 1
Share this question
or