Wednesday, March 23, 2011

Finding a Control within a RadGrid Edit Template

I’ve recently needed to incorporate a RadGrid in which my edit template contained two RadComboBoxes. Depending on the value of my first combo box, the second would be updated with different sets of data. The problem I ran into was how to connect to this second combo box so I can bind it with data.

Here’s the basic premise:

RadComboBox combo1 = (RadComboBox)sender;
GridEditFormItem editItem = (GridEditFormItem)combo1.NamingContainer;
RadComboBox combo2 = (RadComboBox)editItem.FindControl("RadComboBox2");

And the more complete code sample here:


ASPX --



<telerik:RadGrid ID="RadGrid1" runat="server">
  <MasterTableView EditMode="EditForms">
    <EditFormSettings EditFormType="Template">
      <FormTemplate>
        <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadComboBox1_OnSelectedIndexChanged">
          <Items>
            <telerik:RadComboBoxItem Text="Selection 1" Value="1" />
            <telerik:RadComboBoxItem Text="Selection 2" Value="2" />
          </Items>
        </telerik:RadComboBox>
        <telerik:RadComboBox ID="RadComboBox2" runat="server">
        </telerik:RadComboBox>
      </FormTemplate>
    </EditFormSettings>
  </MasterTableView>
</telerik:RadGrid>
 
 
 
 
 


Code-behind --


protected void RadComboBox1_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    RadComboBox combo1 = (RadComboBox)sender;
    GridEditFormItem editItem = (GridEditFormItem)combo1.NamingContainer;
    RadComboBox combo2 = (RadComboBox)editItem.FindControl("RadComboBox2");
 
    switch(e.Value)
    {    
       case "1":
          combo2.Items.Add(new RadComboBoxItem("Test"));
          break;
       case "2":
          combo2.Items.Add(new RadComboBoxItem("Test Again"));
          break;
    }
}



Good luck :)

5 comments:

  1. The assemblies in .NET can be broadly classified in two categories metadata and intermediate language code.
    .net Obfuscator

    ReplyDelete
  2. Finally someone has explained it short and right to the point. Thank you!

    ReplyDelete