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

programatically creating cascading dropdownlist in radgrid

2 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 30 Jul 2013, 07:14 PM
I'm newbee to ASP.Net. Can anyone give me a code snippet for creating a cascading dropdownlists (Country,state and city) in radgrid, I see many post where in .aspx is used for this, but I want to create everything in .cs file. Its urgent, your help is highly appreciated.

Thanks,
Andy.

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 31 Jul 2013, 04:04 AM
Hi Andy,

Please try the below code snippet to programmatically create a cascading dropdownlist.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"
     AllowPaging="true"  OnItemCreated="RadGrid1_ItemCreated">
    <MasterTableView>    
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void Page_Init(object sender, EventArgs e)
  {     
      string templateColumnName = "OrderID";
      GridTemplateColumn templateColumn = new GridTemplateColumn();
      templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
      templateColumn.HeaderText = "OrderID";
      RadGrid1.MasterTableView.Columns.Add(templateColumn);
     
  }
  private class MyTemplate : ITemplate
  {
      protected DropDownList DropDownList1, DropDownList2, DropDownList3;      
      private string colname;
      public MyTemplate(string cName)
      {
          colname = cName;
      }
      public void InstantiateIn(System.Web.UI.Control container)
      {
          DropDownList1 = new DropDownList();
          DropDownList1.ID = "DropDownList1";
          container.Controls.Add(DropDownList1);
          DropDownList1.DataBinding += new EventHandler(DropDownList1_DataBinding);
 
          DropDownList2 = new DropDownList();
          DropDownList2.ID = "DropDownList2";
          container.Controls.Add(DropDownList2);
 
          DropDownList3 = new DropDownList();
          DropDownList3.ID = "DropDownList3";
          container.Controls.Add(DropDownList3);
      }
 
      void DropDownList1_DataBinding(object sender, EventArgs e)
      {
          DropDownList drop = (DropDownList)sender;
          drop.DataSourceID = "SqlDataSource1";
          drop.DataTextField = "OrderID";
          drop.DataValueField = "OrderID";
      }
  }
 
  protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
  {
      if (e.Item is GridDataItem)
      {
          GridDataItem dataItem = (GridDataItem)e.Item;
 
          DropDownList list1 = (DropDownList)dataItem.FindControl("DropDownList1");
          list1.AutoPostBack = true;
          list1.SelectedIndexChanged += new EventHandler(list1_SelectedIndexChanged);
 
          DropDownList list2 = (DropDownList)dataItem.FindControl("DropDownList2");
          list2.AutoPostBack = true;
          list2.SelectedIndexChanged += new EventHandler(list2_SelectedIndexChanged);
      }
  }
  void list2_SelectedIndexChanged(object sender, EventArgs e)
  {
      GridDataItem item = (GridDataItem)(sender as DropDownList).NamingContainer;
      DropDownList ddl3 = (DropDownList)item.FindControl("DropDownList3");
      ddl3.DataSourceID = "SqlDataSource1";
      ddl3.DataTextField = "ShipCity";
  }
 
  void list1_SelectedIndexChanged(object sender, EventArgs e)
  {
      GridDataItem item = (GridDataItem)(sender as DropDownList).NamingContainer;
      DropDownList ddl2 = (DropDownList)item.FindControl("DropDownList2");
      ddl2.DataSourceID = "SqlDataSource1";
      ddl2.DataTextField = "ShipCountry";
  }


Hope this helps,let me know if any concern.

Thanks,
Princy
0
Andy
Top achievements
Rank 1
answered on 31 Jul 2013, 03:08 PM
Thanks Princy, It was cool.
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Share this question
or