HEEN
HEEN

Reputation: 4721

Dropdownlist selected value is not working for binding data

I have a dropdownlist on selection of which i bind the other dropdownlist

In first dropdownlist,

If I select a value, some data should bind to the second dropdownlist. If I select second value them some other data should bind.

Here is my code which I tried so far

if (!IsPostBack)
    {   
        BindAdjustmentType();
        if (ddlAdjustmentType.SelectedValue == "9")
        {
            BindItemCode("select distinct REPLACE(Current_Item_Code  + ' '  + QUOTENAME(Current_Item_Desc,'()'), '*','') as ItemCode, " +
                          "Current_Item_Code from WMS_Storage_Bin where status='Confirmed'");
        }
        if (ddlAdjustmentType.SelectedValue == "10")
        {
            BindItemCodeaddStock("select distinct (Navision_Item_Id + ' ' + QUOTENAME(Item_short_desc, '()')) as AddStock, " +
                                  "Navision_Item_Id from Item_mst");
        }            
    }
}

Also see my dropdown aspx code

<asp:DropDownList ID="ddlAdjustmentType" runat="server" Style="width: 165px" CssClass="xy8">
                        <asp:ListItem Value="0" Selected="True">--- Select ---</asp:ListItem>
                    </asp:DropDownList>

I tried putting the debug point, but it was not hitting the code

UPDATED

public void BindItemCode(string query)
{
    DataTable dtitemcode = CF.ExecuteDT(query);
    ddlItemCode.DataTextField = "ItemCode";
    ddlItemCode.DataValueField = "Current_Item_Code";
    ddlItemCode.DataSource = dtitemcode;
    ddlItemCode.DataBind();
    ddlItemCode.Items.Insert(0, new ListItem("--Select--", "0"));
}

public void BindItemCodeaddStock(string query)
{
    DataTable dtadditemcode = CF.ExecuteDT(query);
    ddlItemCode.DataTextField = "AddStock";
    ddlItemCode.DataValueField = "Navision_Item_Id";
    ddlItemCode.DataSource = dtadditemcode;
    ddlItemCode.DataBind();
    ddlItemCode.Items.Insert(0, new ListItem("--Select--", "0"));
}

At the same time i have written in Jquery for that dropdownlist too for other functionalities.

Here it is

Upvotes: 0

Views: 2390

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21795

So the problem is you are binding the dropdown ddlAdjustmentType on the intial get request. Now when user makes any selection since the autopostback property of ddlAdjustmentType is not set nothing happens. Even if you set that property it won't work because you wrote your code inside:-

if (!IsPostBack)
{
   //Bind dropdown etc.  
}

You will have to add a Change event in dropdown and either ask the user to click the button or if you want the other dropwdown to be populated as soon as you select something, set the AutoPostBack property of dropdown to true:-

<asp:DropDownList ID="ddlAdjustmentType" runat="server" autopostback="true"
     onselectedindexchanged="ddlAdjustmentType_change" Style="width: 165px" ..

Then in code behind write the event method:-

protected void Student_type_dd_change(object sender, EventArgs e)
{  
    if (ddlAdjustmentType.SelectedValue == "9")
    {
        ..logic here
    }
    if (ddlAdjustmentType.SelectedValue == "10")
    {
        ...logic here
    }  
}

But IMHO if you are using jquery ajax already don't unnecassily do a postback, write a change event of ddlAdjustmentType and then make a ajax call and populate other dropdowns.

Upvotes: 1

Related Questions