简体   繁体   English

动态下拉列表始终返回第一个值,而不是所选值

[英]Dynamic Dropdownlist is always returning the first value, not the selected value

i have an issue i have been battling with for sometime now, my dynamic DropDownList always return to the first item from database, i have done a few researches on how to resolve this including enabling Viewstate for the page and DropDownList and placing the code on if(!Page.IsPostback) but the issue persists! 我有一段时间以来一直在与之抗争的问题,我的动态DropDownList总是从数据库返回到第一项,我对如何解决此问题进行了一些研究,包括为页面和DropDownList启用Viewstate以及将代码放置在(!Page.IsPostback),但问题仍然存在!

Here is my code: 这是我的代码:

if (!Page.IsPostBack)
{
    string appType2 = Session["applicationType"].ToString();
    if (appType2 == "nur")
    {

        FetchApplicationFeeFromDB("NURSERY");
        this.ddlAppClass.Items.Clear();
        this.ddlApplicationType.Items.Insert(0, "NURSERY SCHOOL");

        string query = string.Format("SELECT [Teaching Room Category] ,[Description] FROM [Corona Schools_ Trust Council$Teaching Room Level] WHERE [Teaching Room Category] = '{0}'", "NURSERY");
        BindDropDownList(this.ddlAppClass, query, "Description", "Teaching Room Category", "---Select---");
    }

}

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
    string conString = ConfigurationManager.ConnectionStrings["DatabaseDemo Database NAV (8-0)1"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            con.Open();
            ddl.DataSource = cmd.ExecuteReader();
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
            con.Close();
        }
    }
    ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}

protected void ddlAppClass_SelectedIndexChanged(object sender, EventArgs e){ 
    if (this.ddlAppClass.SelectedIndex != 0) { 
        txtDateOfBirth.Enabled = true; 
    } else { 
        txtDateOfBirth.Enabled = false; 
    } 
} 

My DropdownList Source code: 我的DropdownList源代码:

<asp:DropDownList ID="ddlAppClass" runat="server" AutoPostBack="True" Height="20px" Width="188px" OnSelectedIndexChanged="ddlAppClass_SelectedIndexChanged" EnableViewState="true"></asp:DropDownList>

OnSelectedIndexChanged code: OnSelectedIndexChanged代码:

protected void ddlAppClass_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (this.ddlAppClass.SelectedIndex != 0) 
    { 
        txtDateOfBirth.Enabled = true; 
    } 
    else 
    {
        txtDateOfBirth.Enabled = false;
    }
}

You need to bind the items each time the page loads, not just when !PostBack. 您需要在每次页面加载时绑定项目,而不仅仅是在!PostBack时。 When you post back, the page is rebuilt from the aspx and selected values are (attempted) set in code but in your case, the dropdownlist will be empty because you are not calling BindDropDownList so its selected value cannot be set and you get the zero. 当您回发时,页面将根据aspx重建,并在代码中设置(尝试)选定的值,但是在这种情况下,下拉列表将为空,因为您没有调用BindDropDownList,因此无法设置其选定的值,并且得到零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM