cannot get the selected value of a dropdownlist which is the result of a selection of another dropd

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello its urgent pls,<br/>
<br/>
I have 2 dropdown lists:<br/>
1- ddlCity <br/>
2-ddlBranch<br/>
<br/>
the purpose is to display all branches per city. so once i select a city, related branches should be selected.<br/>
<br/>
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.<br/>
<br/>
Here is the code <br/>
<br/>
1-asp.net form:
<pre class="prettyprint <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>[/code]
<br/>
2-C# code:<br/>
<pre class="prettyprint 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;
}[/code]
<br/>
<br/>
<br/>
the problem is that Textbox1 diplay the right id of the branch only in the load page after that this value is not changeable.<br/>
<br/>
thanks alot for your help.

<br/>

View the full article
 
Back
Top