无法获得下拉列表的选定值,这是选择另一个下拉列表的结果 [英] cannot get the selected value of a dropdownlist which is the result of a selection of another dropdownlist

查看:65
本文介绍了无法获得下拉列表的选定值,这是选择另一个下拉列表的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,请紧迫,

我有2个下拉列表:
1- ddlCity
2-ddlBranch

目的是显示每个城市的所有分支机构.因此,一旦我选择了城市,就应该选择相关的分支机构.

现在,一旦我选择了一个城市,相关的分支机构便会上载到ddlBranch下拉列表中,但问题是我无法调出所选分支机构的SelectedValue.

这是代码

1-asp.net形式:

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: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#代码:



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;          
        }




问题是Textbox1仅在加载页面时才显示正确的分支ID;之后,该值不可更改.




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.

推荐答案

ConnectionStrings:SmartBookingEngineConn%>" span> SelectCommand =" < > < asp:SqlDataSource ID =" runat 服务器" ConnectionString =" <%
ConnectionStrings:SmartBookingEngineConn %>" SelectCommand="SELECT [cityID],[city] FROM [tblCity]"></asp:SqlDataSource> <asp:SqlDataSource ID="tblBranch" runat="server" ConnectionString="<%


ConnectionStrings:SmartBookingEngineConn%>" span> SelectCommand =" < /asp:SqlDataSource >
ConnectionStrings:SmartBookingEngineConn %>" SelectCommand="SELECT [BranchID],[Branch] FROM [tblBranch]"> </asp:SqlDataSource>



2-C#代码:



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;          
        }




问题是Textbox1仅在加载页面时才显示正确的分支ID;之后,该值不可更改.




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.


将ddlBranch的AutoPostBack属性更改为true.

更改此内容:
Change the property of AutoPostBack of ddlBranch to true.

Change this:
<asp:DropDownList ID="ddlBranch" runat="server" Width="120px"

        onselectedindexchanged="ddlBranch_SelectedIndexChanged"  >
    </asp:DropDownList>


对此:


to this:

<asp:DropDownList ID="ddlBranch" runat="server" Width="120px" AutoPostBack="true"

        onselectedindexchanged="ddlBranch_SelectedIndexChanged"  >
    </asp:DropDownList>


这篇关于无法获得下拉列表的选定值,这是选择另一个下拉列表的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆