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

Postback while in radgrid edit form mode

1 Answer 283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 17 Jul 2012, 05:20 PM

Radgrid edit mode rocks with a special formtemplate, it this might be simple but cannot figure out.  I need to be able to postback while in edit mode, i have 2 dropdownlists and need the second to be filled by the first while in edit mode. I can fill them to existing values but if a user need to change them they stay with existing values and don't change becuase of no postback.  So basically if they pick another make I need the models to refresh themselves based on the make.  If this is not possible in edit mode I will take it out to a panel and do it here.


Protected Sub myRadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
           Dim editedItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
           Dim Loc As DropDownList = DirectCast(editedItem.FindControl("ddlLocation"), DropDownList)
           Dim make As DropDownList = DirectCast(editedItem.FindControl("ddlMake"), DropDownList)
           Dim model As DropDownList = DirectCast(editedItem.FindControl("ddlModel"), DropDownList)
           'Get the Location filled
           sql = "Select intDurableId, strLocation from Drat_J6DurableInfo Order by strLocation"
           Loc.Items.Add(New ListItem("Pick Location", "0"))
           buildDD(sql, Loc)
           Loc.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "intDurableId").ToString(), String)
           Loc.DataBind()
           'Get the make of the item
           sql = "Select intmakeId, strmake from Drat_Make order by strmake"
           make.Items.Add(New ListItem("Pick Make", "0"))
           buildDD(sql, make)
           make.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "intMakeId").ToString(), String)
           make.DataBind()
           'Get the Model of the item
           sql = "Select intModelId, strModel from Drat_Model order by strModel"
           model.Items.Add(New ListItem("Pick Model", "0"))
           buildDD(sql, model)
           model.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "intModelId").ToString(), String)
           model.DataBind()
       End If
   End Sub

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Jul 2012, 07:19 PM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound"
            OnNeedDataSource="RadGrid1_NeedDataSource">
            <MasterTableView DataKeyNames="ID,Name" ClientDataKeyNames="ID,Name" CommandItemDisplay="Top"
                EditMode="EditForms">
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
                        </asp:DropDownList>
                        <br />
                        <asp:DropDownList ID="DropDownList2" runat="server">
                        </asp:DropDownList>
                    </FormTemplate>
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
      {
          dynamic data = new[] {
            new { ID = 1, Name ="name1"},
            new { ID = 2, Name = "name2"},
            new { ID = 3, Name = "name3"},
            new { ID = 4, Name = "Name4"},
             new { ID = 5, Name ="name5"}
          };
          RadGrid1.DataSource = data;
      }
 
      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
      {
          if (e.Item.IsInEditMode && e.Item is GridEditFormItem)
          {
              GridEditFormItem item = e.Item as GridEditFormItem;
              string id = item.GetDataKeyValue("ID").ToString();
              DropDownList DropDownList1 = item.FindControl("DropDownList1") as DropDownList;
              DropDownList DropDownList2 = item.FindControl("DropDownList2") as DropDownList;
 
              bindFirstDDL(DropDownList1);
              if (DropDownList1.Items.FindByValue(id) != null)
              {
                  DropDownList1.Items.FindByValue(id).Selected = true;
                  bindSecondDDL(DropDownList2, Convert.ToInt32(id));
              }
 
          }
      }
 
      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
          DropDownList DropDownList1 = sender as DropDownList;
          GridEditFormItem item = DropDownList1.NamingContainer as GridEditFormItem;
          DropDownList DropDownList2 = item.FindControl("DropDownList2") as DropDownList;
          if (DropDownList1.SelectedIndex != -1)
          {
              bindSecondDDL(DropDownList2, Convert.ToInt32(DropDownList1.SelectedValue));
          }
      }
 
      protected void bindFirstDDL(DropDownList ddl)
      {
          dynamic data = new[] {
            new { CountyID = 1, CountyName ="name1"},
            new { CountyID = 2, CountyName = "name2"},
          };
          ddl.DataSource = data;
          ddl.DataTextField = "CountyName";
          ddl.DataValueField = "CountyID";
          ddl.DataBind();
      }
 
      protected void bindSecondDDL(DropDownList ddl, int CoutryID)
      {
          if (CoutryID > 0)
          {
              dynamic data = new[] {
            new { StateID = 1, StateName ="name_1_" + CoutryID.ToString()},
            new { StateID = 2, StateName = "name_2_"+ CoutryID.ToString()},
          };
              ddl.DataSource = data;
              ddl.DataTextField = "StateName";
              ddl.DataValueField = "StateID";
              ddl.DataBind();
          }
      }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or