动态的DropDownList不保留在回发ASP.net C#值 [英] dynamically DropDownList does not retain value on postback ASP.net c#

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

问题描述

修改3:修复!

我终于找到了问题所在。
其中一个asp的每个控件:表,我为了得的EnableViewState =真这个表能够保持值回发后

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

感谢大家的答案!

首先,原谅我,如果我的英文不是很好,但我会努力成为最precise越好。
我与从昨天起我的问题,以及战斗一直在寻找所有网站上的答案。

First of all, excuse me if my english is not perfect but I'll try to be the most precise as possible. I'm fighting with my problem since yesterday and have been searching all over the web for an answer..

我有做成创建新的配置文件的形式。在这种形式下,我有一些DropDownLists和文本框,我的问题是关于DropDownLists。

I have a form which is made to "Create a new profile". In this form I have some DropDownLists and TextBoxes, my problem is about DropDownLists.

4的下拉是我的网页上。

4 DropDown are on my page.

让我们专注于去年2下拉:

Let's focus on the 2 last 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

1 DDL:

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

第二DDL:

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

填充方法:

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"));

}

当我在第二个DDL和一个回发时(即使它来自其他dropdownlists我之前mentionned)选择一个项目,将成为的SelectedValue的第一个值。 (Selectionnez ...)

When I Select an item in my 2nd ddl and that a PostBack occurs (even if it comes from others dropdownlists i mentionned before), the SelectedValue becomes the first value. ("Selectionnez...")

这似乎是我的第二个下拉会在每次回发界,即使是因为我的第一个下拉的的SelectedIndexChanged不..
本的SelectedIndexChanged第一下拉总是叫上回发,所以它抛出populateDdl()在每一个回发(如果选择的值)。

It seems like my 2nd DropDown is bounded on every postback even if it's not because of SelectedIndexChanged of my 1st DropDown.. The SelectedIndexChanged the 1st DropDown 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 register a blank value in my database.

我是什么失踪?

EDIT1:

下面是pageLoad的:

Here is PageLoad :

    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

        }
    }

和这里是第一个DDL的SelectedIndexChanged:

and here is 1st ddl SelectedIndexChanged :

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


    }

编辑2:

请参阅下面的图片:

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

您可以看到我的第二DDL(Fonction),填写正确,但是当我点击提交按钮:值变为空值(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!

推荐答案

您是人口DD1在每一个回发

you are population DD1 in every post back

要避免这种使用

if(!IsPostBack)    
 {
   populateDdl();
 }

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

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