Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 dropdownlists.2nd dropdownlist text and value control by dropdownlist 1.
C#
protected void Page_Load(object sender, EventArgs e)
  {
      if (Page.IsPostBack)
      {

          filldropdown(dllselection.SelectedValue);
          Code.Enabled = true;
          if(dllselection.SelectedValue=="")
          {
              Code.Enabled = false;
          }
      }
  }

ASP.NET
<div class="form-group">
          <label for="inputEmail3" class="col-sm-2 control-label"  style="color:black" >Main Category</label>
          <div class="col-sm-3">
              <asp:DropDownList ID="dllselection" runat="server" CssClass="form-control" AutoPostBack="true" required>
                  <asp:ListItem Text="Please Select" Value=""></asp:ListItem>
                  <asp:ListItem Text="HR" Value="M_1"></asp:ListItem>
                  <asp:ListItem Text="IT" Value="M_2"></asp:ListItem>
                 <asp:ListItem Text="Maintenance" Value="M_3"></asp:ListItem>
              </asp:DropDownList>
          </div>
          </div>

        <div class="form-group">
         <label for="Training" style="color:black" class="col-sm-2 control-label">Sub Category</label>
          <div class="col-sm-3">
              <asp:DropDownList ID="Code" Enabled="false" onchange="javascript:return dropdown(this);" runat="server" CssClass="form-control"  ValidationGroup="G1" required></asp:DropDownList>
          </div>
          </div>

C#
i keep fail to get dropdownlist value for 2nd dropdrownlist. the value i get is always 1st value and text.

C#
everytime I select any item from the 2nd ddl it posts back and the first item would be selected instead the item that I select.


What I have tried:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Posted
Updated 31-Jul-16 20:04pm

1 solution

Change if (Page.IsPostBack) to if (!Page.IsPostBack).
 
Share this answer
 
v2
Comments
KyLim0211 1-Aug-16 2:14am    
if i change to !postback,2nd dropdownlist can get any data
KyLim0211 1-Aug-16 2:17am    
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Code.Enabled = true;
if (dllselection.SelectedValue == "")
{
Code.Enabled = true;
}

}
if(!IsPostBack)
{
filldropdown(dllselection.SelectedValue);
}

}
So, the concept is that if you have any code which binds your drodown, that should go inside !IsPostBack. Otherwise, it will keep binding on every postback and you will be always getting the first value as SelectedValue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900