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

How to find dynamic controls in EditFormTemplate

2 Answers 72 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John John
Top achievements
Rank 1
John John asked on 16 Mar 2010, 04:18 PM
Hi,
   I just want to find the dynamically created Check box controls from the EditFormTemplate section. The controls are created dynamically in the EditFormTemplate section as the code given below; So i need to find the same controls under Update/Insert commapnd section to mke me to do the data manipulation work.

 

if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))

 

{

 


       TableRow row = new TableRow();
        TableCell cell = new TableCell();
        cC.Width = Unit.Percentage(20);
        cC.HorizontalAlign = HorizontalAlign.Left;
        HtmlInputCheckBox e0 = new HtmlInputCheckBox();
        e0.ID = "cbx-" + x.ToString();
        e0.Value  = dR["Abbreviation"].ToString();
        e0.Checked = true;
        
        cell.Controls.Add(e0);
        row.Controls.Add(cC);
        myTable.Controls.Add(row);
}

The above is the code i used to create htmnl check boxes dynamically in ItemDataBound event of the grid

-Thanks

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Mar 2010, 08:07 AM
Hello,

A better approach to create the dynamic controls is by using ItemCreated event than ItemDataBound. For accessing the dynamically created controls in Update/Insert command event use the following code:

C#:
 
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)  
{  
    HtmlInputCheckBox dynamicCheck = ((GridEditFormItem)e.Item).FindControl("ControlID"as HtmlInputCheckBox;  
}  
  
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)  
{  
    HtmlInputCheckBox dynamicCheck = ((GridEditFormItem)e.Item).FindControl("ControlID"as HtmlInputCheckBox;        
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
    { 
        TableCell cell = new TableCell(); 
        HtmlInputCheckBox chk = new HtmlInputCheckBox(); 
        chk.Checked = true
        chk.ID = "dynamicChk"
        chk.Value = "Dynamic control"
        chk.Attributes.Add("runat""server"); 
        cell.Controls.Add(chk); 
        ((GridEditFormItem)e.Item).Controls.Add(cell); 
    } 

-Shinu
0
John John
Top achievements
Rank 1
answered on 17 Mar 2010, 11:38 AM
Hi Shinu,
                Thanks for your response. But i need to add dynamically created controls into my already defined table instead of adding cells as mentioned the above code.

I defined the table inside the EditFormTemplate section, later i used the same to add dynamically created controls

 <asp:Table ID="myTable" runat="server" GridLines="None" CellSpacing="0" CellPadding="0" 
                                         Width="100%" Style="margin-top: 8px;" /> 

So we have to find the controls from there.

-Thanks
 
Tags
Grid
Asked by
John John
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
John John
Top achievements
Rank 1
Share this question
or