动态填充的DropDownList不会在回发时保留值 [英] dynamically filled DropDownList does not retain value on postback

查看:66
本文介绍了动态填充的DropDownList不会在回发时保留值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于创建新的个人资料"的表格.我的问题是关于DropDownLists.

I have a form which is made to "Create a new profile". My problem is about DropDownLists.

第一个DropDown在其值的函数中动态填充第二个.

The first DropDown dynamically populate the second one in function of its value.

看到这张照片:

http://image.noelshack.com/fichiers /2013/22/1369819471-picture-help.png

以及以下图片:

http://image.noelshack.com/fichiers/2013 /22/1369830738-help2.png

当我单击提交"按钮时,您可以看到我的第二个ddl(功能")已正确填充,但该值变为空值(Sélectionnez..."),因此我的RequiredFieldValidator使得该页面无效!

You can see that my 2nd ddl ("Fonction") is correctly filled BUT when I click on the submit button, the value becomes the null value ("Sélectionnez...") and so my RequiredFieldValidator makes the page not valid!

我的第二个DropDownList似乎在每次回发中都受限制,即使不是因为我的第一个DropDownList的SelectedIndexChanged也是如此. 第一个DropDownList的SelectedIndexChanged总是在回发时调用,因此它在每个回发时都会抛出"populateDdl()"(如果选择了一个值).

It seems like my 2nd DropDownList is bounded on every postback even if it's not because of SelectedIndexChanged of my 1st DropDownList. The SelectedIndexChanged of the 1st DropDownList is always called on postback and so it throws "populateDdl()" at every PostBack (if a value is selected).

当我单击提交"按钮时,它将在我的数据库中注册一个空白值.

When I click on submit button, it registers a blank value in my database.

我想念什么?

aspx代码:

<asp:DropDownList ID="ddlTypePN" runat="server" DataSourceID="SqlTypePN" EnableViewState="true" 
        DataTextField="libelle" DataValueField="valeur" AutoPostBack="true" OnSelectedIndexChanged="ddlTypePN_SelectedIndexChanged" 
        OnDataBound="ddlTypePN_DataBound" > </asp:DropDownList> 

<asp:DropDownList runat="server" ID="ddlFctPN" AppendDataBoundItems="false" OnDataBound="ddlFctPN_DataBound" > </asp:DropDownList> 

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlTypeProf.DataBind(); // don't care
        ddlSsoSrc.DataBind(); // don't care
        ddlTypePN.DataBind(); // The ddl that populate my 2nd ddl
    }
}

protected void ddlTypePN_SelectedIndexChanged(object sender, EventArgs e)
{
    string type = ddlTypePN.SelectedValue.ToString().Trim();
    // if PNT
    if (type.ToUpper().Trim().Equals("PNT"))
    {               
        ddlFctPN.Enabled = true;
        ddlTypeAv.Enabled = true;
        rfvTypeAv.Enabled = true;
        populateDdl();

    }
    else if (type.ToUpper().Trim().Equals("PNC"))
    {                
        ddlFctPN.Enabled = true;
        ddlTypeAv.Enabled = false;
        rfvTypeAv.Enabled = false;
        populateDdl();
    }     
}

void populateDdl()
{
    string val = "fct"+ddlTypePN.SelectedValue.ToString().Trim(); // Used for SELECT
    SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["My_DB"].ConnectionString);
    ddlFctPN.Items.Clear();
    DataTable subjects = new DataTable();
    try
    {
        SqlDataAdapter adapter = new SqlDataAdapter("My SELECT", sqlConn);
        adapter.Fill(subjects);

        ddlFctPN.DataSource = subjects;
        ddlFctPN.DataTextField = "libelle";
        ddlFctPN.DataValueField = "valeur";
        ddlFctPN.DataBind();
    }
    catch (Exception ex)
    {
        lblErr.Text = ex.Message;
    }
    ddlFctPN.Items.Insert(0, new ListItem("Sélectionnez...", "null"));
}

推荐答案

解决方案从问题中移出答案:

Solution moved to an answer from the question:

我终于找到了问题所在.每个控件都位于一个<asp:Table>中,我必须在此表上设置EnableViewState="true"以便能够在回发后保留该值.

I finally found what the problem was. Every control was in an <asp:Table>, and I had to set EnableViewState="true" on this table in order to be able to keep the value after postback.

这篇关于动态填充的DropDownList不会在回发时保留值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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