KyLim
KyLim

Reputation: 476

c# 2nd dropdownlist fail retain the value when submit

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {

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

}

i think there something wrong with the page load,my 2nd dropdownlist is depend on 1st dropdownlist selection.

<div class="form-group">
    <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>

everytime i submit pass data to database,the value for the 2nd dropdownlist always the 1st value.

 public void filldropdown(string item)
{

        int loggedUserID = Convert.ToInt32(Session["loggedUserID"]);
        List<BOL.UserInfo> userslist = new UserInfos().List();
        BOL.UserInfo loggeduser = userslist.Where(x => x.UserID == loggedUserID).FirstOrDefault();
        //  int ID = 10;

        List<e_request> role = new e_requests().dropdownlistG(loggeduser.SUBSIDIARY_CD, item);
        Code.DataSource = role;
        Code.DataTextField = "CAT_DESC";
        Code.DataValueField = "SUB_CAT";
        Code.DataBind();

}

Upvotes: 1

Views: 66

Answers (5)

KyLim
KyLim

Reputation: 476

add OnSelectedIndexChanged="dllselection_SelectedIndexChanged" to my 1st dropdownlist.

 protected void dllselection_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dllselection.SelectedIndex == 0)
        {
            Code.Enabled = false;
        }
        else
        {
            Code.Enabled = true;
            //fill Code
            filldropdown(dllselection.SelectedValue);

        }
    }

Upvotes: 0

Arun Prasad E S
Arun Prasad E S

Reputation: 10145

  <asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList> 

protected void Page_Load(object sender, EventArgs e) {

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

}

Upvotes: 0

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

Try to Load Method in

if (!Page.IsPostBack)
    {

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

Update:

you need to fill the second dropdown on SelectedIndexChange of dllselection and need to set AutoPostBack = true of dllselection .

Upvotes: 0

Adil
Adil

Reputation: 148180

You can filling drop down on postback which you should not if you want to keep selection. use !Page.IsPostBack instead of Page.IsPostBack

Change

if (Page.IsPostBack)
{

To

if (!Page.IsPostBack)
{

On more thing you may need to put condition out side !Page.IsPostBack as you would need it to be executed on postback

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

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

Also note you may need to fill the second dropdown on SelectedIndexChange of dllselection and need to set AutoPostBack of dllselection true.

Upvotes: 2

Snehlata Shaw
Snehlata Shaw

Reputation: 97

Try this:

if (!IsPostBack) {

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

Upvotes: 1

Related Questions