title: avoid postback on dropdownlist change in asp net C#
body: <p>I have nested gridview with dropdown list.when i change parent grid's dropdown box,Child gris'd dropdown box should change automatically. but after performing all operations due to postpack problem,child grid hides...</p>
  
  <p>my code:</p>
  
  <pre><code>&lt;asp:GridView ID="gvParentMenu" Width="100%" runat="server" BorderStyle="Solid" BorderColor="#000" BorderWidth="1px" GridLines="None" DataKeyNames="menu_id"  AutoGenerateColumns="false" OnPageIndexChanged="gvParentMenu_PageIndexChanged" OnPageIndexChanging="gvParentMenu_PageIndexChanging" OnRowCancelingEdit="gvParentMenu_RowCancelingEdit" OnRowCommand="gvParentMenu_RowCommand" OnRowCreated="gvParentMenu_RowCreated" OnRowDataBound="gvParentMenu_RowDataBound" OnRowDeleted="gvParentMenu_RowDeleted" OnRowDeleting="gvParentMenu_RowDeleting" OnRowEditing="gvParentMenu_RowEditing" OnRowUpdated="gvParentMenu_RowUpdated" OnRowUpdating="gvParentMenu_RowUpdating"&gt;
  &lt;HeaderStyle BackColor="#76C4ED" Font-Bold="true" ForeColor="White" BorderColor="#76C4ED" /&gt;
  &lt;Columns&gt;
  
  &lt;asp:TemplateField ItemStyle-Width="20px"&gt;
  &lt;ItemTemplate&gt;
  &lt;a href="JavaScript:divexpandcollapse('div&lt;%# Eval("menu_id") %&gt;');"&gt;
  &lt;img id="imgdiv&lt;%# Eval("menu_id") %&gt;" border="0" src="common/img/button-plus-icon.png" /&gt;
  &lt;/a&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  
  &lt;asp:BoundField DataField="menu_id" HeaderText="Menu Id" Visible="false" /&gt;
  &lt;asp:BoundField DataField="menu_desc" HeaderText="Menu Name" /&gt;
  &lt;asp:TemplateField HeaderText="Rights"&gt;
  &lt;ItemTemplate&gt;
  
  &lt;asp:DropDownList ID="drpRights"  runat="server" Width="100%" AutoPostBack="true" SelectedValue='&lt;%# Bind("rights") %&gt;' OnSelectedIndexChanged="drpRights_SelectedIndexChanged"&gt;
  &lt;asp:ListItem Text="Select" Value="0"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text="View" Value="1"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text="View &amp; Insert" Value="2"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text="View &amp; Insert &amp; Edit" Value="3"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text="View &amp; Insert &amp; Edit &amp; Delete" Value="4"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text="View &amp; Insert &amp; Edit &amp; Delete &amp; Report" Value="5"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text="Report" Value="6"&gt;&lt;/asp:ListItem&gt;
  &lt;/asp:DropDownList&gt;
  &lt;asp:HiddenField ID="hdnParGridId" runat="server" Value='&lt;%#Eval("menu_id")%&gt;' /&gt;     
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  
  
  &lt;asp:TemplateField ItemStyle-BorderColor="White" ItemStyle-BorderStyle="None" ItemStyle-BorderWidth="0px"&gt;
  &lt;ItemTemplate&gt;
  &lt;tr &gt;
  &lt;td colspan="100%"&gt;
  
  
  &lt;div id="div&lt;%# Eval("menu_id") %&gt;" style="display: none; position: relative; left: 28px; overflow: auto" class="table-responsive"&gt;
  
  
  &lt;asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Solid" BorderColor="#000" BorderWidth="1px" GridLines="None" Width="70%"&gt;
      &lt;HeaderStyle BackColor="#76C4ED" Font-Bold="true" ForeColor="White" /&gt;
      &lt;HeaderStyle BackColor="#76C4ED" Font-Bold="true" ForeColor="White" /&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="menu_id" HeaderText="Menu Id" HeaderStyle-HorizontalAlign="Left" Visible="false" /&gt;
          &lt;asp:BoundField DataField="menu_desc" HeaderText="Menu Name" HeaderStyle-HorizontalAlign="Left" /&gt;
  
          &lt;asp:TemplateField HeaderText="Rights"&gt;
              &lt;ItemTemplate&gt;
  
                  &lt;asp:DropDownList ID="drpChildRights"  runat="server" Width="90%" SelectedValue='&lt;%# Bind("rights") %&gt;'&gt;
                      &lt;asp:ListItem Text="Select" Value="0"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="View" Value="1"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="View &amp; Insert" Value="2"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="View &amp; Insert &amp; Edit" Value="3"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="View &amp; Insert &amp; Edit &amp; Delete" Value="4"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="View &amp; Insert &amp; Edit &amp; Delete &amp; Report" Value="5"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="Report" Value="6"&gt;&lt;/asp:ListItem&gt;
  
                  &lt;/asp:DropDownList&gt;
                      &lt;asp:HiddenField ID="hdnChildGridId" runat="server" Value='&lt;%#Eval("menu_id")%&gt;' /&gt; 
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  &lt;%--    &lt;br /&gt;--%&gt;
  &lt;/div&gt;
  
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  
  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>C# Code here :</p>
  
  <pre><code> protected void drpRights_SelectedIndexChanged(object sender, EventArgs e)
          {
  
  
                  try
                  {
                      int index = ((sender as DropDownList).NamingContainer as GridViewRow).RowIndex;
                      DropDownList drp = (DropDownList)gvParentMenu.Rows[index].FindControl("drpRights");
                      GridView gvChild = (GridView)gvParentMenu.Rows[index].FindControl("gvChildGrid");
  
                      gvChild.Visible = true;
                      foreach (GridViewRow row in gvChild.Rows)
                      {
                          if (row.RowType == DataControlRowType.DataRow)
                          {
                              DropDownList drpChild = (DropDownList)row.FindControl("drpChildRights");
  
                              if (drp.SelectedItem.Value == "0")
                              {
                                  drpChild.SelectedItem.Value = "0";
                              }
                              else if (drp.SelectedItem.Value == "1")
                              {
                                  drpChild.SelectedItem.Value = "1";
                              }
                              else if (drp.SelectedItem.Value == "2")
                              {
                                  drpChild.SelectedItem.Value = "2";
                              }
                              else if (drp.SelectedItem.Value == "3")
                              {
                                  drpChild.SelectedItem.Value = "3";
                              }
                              else if (drp.SelectedItem.Value == "4")
                              {
                                  drpChild.SelectedItem.Value = "4";
                              }
                              else if (drp.SelectedItem.Value == "5")
                              {
                                  drpChild.SelectedItem.Value = "5";
                              }
                              else if (drp.SelectedItem.Value == "6")
                              {
                                  drpChild.SelectedItem.Value = "6";
                              }
  
                          }
                      }
  
  
                  }
                  catch (Exception ex)
                  {
  
                  }
  
          }
  </code></pre>
  
  <p>my screeshot:</p>