Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello it's urgent pls,

I have 2 dropdown lists:
1- ddlCity
2-ddlBranch

the purpose is to display all branches per city. so once i select a city, related branches should be selected.

now once i select a city , the related branches are uploaded into the ddlBranch dropdownlist but the problem is that i cannot recupere the SelectedValue of the selected branch.

Here is the code

1-asp.net form:

ASP.NET
  <asp:DropDownList ID="ddlCity" runat="server" Width="120px" 
        DataSourceID="tblCity" DataTextField="city" DataValueField="cityID" AutoPostBack="true"
        onselectedindexchanged="ddlCity_SelectedIndexChanged">
    </asp:DropDownList>

<asp:DropDownList ID="ddlBranch" runat="server" Width="120px"  
        onselectedindexchanged="ddlBranch_SelectedIndexChanged"  >
    </asp:DropDownList>

<asp:TextBox ID="TextBox1" runat="server"  AutoPostBack="True" Visible="true"></asp:TextBox>

<asp:SqlDataSource ID="tblCity" runat="server" 
              ConnectionString="<%$ ConnectionStrings:SmartBookingEngineConn %>" 
              SelectCommand="SELECT [cityID],[city] FROM [tblCity]"></asp:SqlDataSource>
          <asp:SqlDataSource ID="tblBranch" runat="server" 
              ConnectionString="<%$ ConnectionStrings:SmartBookingEngineConn %>" 
              SelectCommand="SELECT [BranchID],[Branch] FROM [tblBranch]">
          </asp:SqlDataSource>


2-C# code:
 protected void Page_Load(object sender, EventArgs e)
 {
    if (!IsPostBack)
            {
                

                //dropdown branches per city
                ddlCity.DataBind();
                dtbranch = null;
                string selectedIDCity = ddlCity.SelectedValue;
                string mysqlSelectBranch = "select Branch,BranchID from     viewBranchPerCity where CityID=" + selectedIDCity;
                dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
                ddlBranch.DataSource = dtbranch;
                ddlBranch.DataValueField = "BranchID";
                ddlBranch.DataTextField = "Branch";
                ddlBranch.DataBind();
              

               TextBox1.Text = ddlBranch.SelectedValue;
            }

 }


 protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
           
               
                //loading branches per city
                ddlBranch.DataBind();
                dtbranch = null;
                string selectedIDCity = ddlCity.SelectedValue;
                string mysqlSelectBranch = "select Branch,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
                dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
                ddlBranch.DataSource = dtbranch;
                ddlBranch.DataValueField = "BranchID";
                ddlBranch.DataTextField = "Branch";
                ddlBranch.DataBind();
                

        }

protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)

        {
         TextBox1.Text = ddlBranch.SelectedValue;          
        }



the problem is that Textbox1 diplays the right branch id only while loading the page ; after that this value is not changeable.

thanks alot for your help.
Posted
Updated 4-Nov-12 20:22pm
v5
Comments
Abhishek Pant 4-Nov-12 7:18am    
I think your question also belongs to Asp.net so just tag this also.

1 solution

Change the property of AutoPostBack of ddlBranch to true.

Change this:
XML
<asp:DropDownList ID="ddlBranch" runat="server" Width="120px"
        onselectedindexchanged="ddlBranch_SelectedIndexChanged"  >
    </asp:DropDownList>

to this:
XML
<asp:DropDownList ID="ddlBranch" runat="server" Width="120px" AutoPostBack="true"
        onselectedindexchanged="ddlBranch_SelectedIndexChanged"  >
    </asp:DropDownList>
 
Share this answer
 

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