【问题标题】:DropDownList populate method affects postback behaviour?DropDownList 填充方法会影响回发行为吗?
【发布时间】:2012-08-31 15:56:15
【问题描述】:

我已开始在现有项目中重写代码并更改了填充两个dropdownlists 的方式。当使用DataBind 时,SelectedValue 在回发后在Page_Load 中具有正确的值。但是当使用AddRange 时,SelectedValue 始终是下拉列表中的第一个值。

下拉列表的填充方式如何影响回发行为?是的,页面中也有 java 脚本,但我看不出它们会如何干扰。

工作方式(SelectedValuePage_Load 中是正确的):

protected void Page_Init(object sender, EventArgs e)
{
    IList<ListItem> list = new List<ListItem>();
    list.Add(new ListItem(Resources.Site.OriginalStructure, "0"));
    list.Add(new ListItem("5", "5"));
    list.Add(new ListItem("10", "10"));
    list.Add(new ListItem("15", "15"));
    list.Add(new ListItem("20", "20"));
    list.Add(new ListItem("25", "25"));

    DropDownList1.DataSource = list;
    DropDownList1.DataTextField = "Text";
    DropDownList1.DataValueField = "Value";
    DropDownList1.DataBind();

    DropDownList2.DataSource = list;
    DropDownList2.DataTextField = "Text";
    DropDownList2.DataValueField = "Value";
    DropDownList2.DataBind();   
}

不起作用的方式(SelectedValue 始终是Page_Load 中ddl 中的第一个值):

protected void Page_Init(object sender, EventArgs e)
{
    var numberQuestionsPerPageDdlValues = new[]
    {
        new ListItem(Resources.Site.OriginalStructure, "0"),
        new ListItem("5", "5"),
        new ListItem("10", "10"),
        new ListItem("15", "15"),
        new ListItem("20", "20"),
        new ListItem("25", "25")
    };
    DropDownList1.Items.AddRange(numberQuestionsPerPageDdlValues);
    DropDownList2.Items.AddRange(numberQuestionsPerPageDdlValues);
}

【问题讨论】:

  • 什么意思 "正确的 SelectedValue" 因为你没有设置 SelectedIndexSelectedValue。你甚至没有使用constructor of ListItem,它接受第三个参数enabled,这意味着在DropDownList中被选中。
  • With "correct SelectedValue" 我的意思是用户在页面上选择的值。当我测试它时,我选择了值“5”。我不认为我必须设置SelectedIndex --> MSDN: “默认值为0,它选择列表中的第一项。”
  • Page_Load 中我可以看到Request.Form[DropDownList1.UniqueId] 包含选定的值,但DropDownList1.SelectedValue""
  • 你能同时显示 DropDownList 的 aspx 标记吗?

标签: c# asp.net


【解决方案1】:

“正确的 SelectedValue”是指由 页面上的用户

您在每次回发时将DropDownList 绑定到它的DataSource。你应该只这样做if(!Page.IsPostBack)。否则不会触发事件,用户的SelectedValue会被覆盖。

我还建议改用page_load

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        IList<ListItem> list = new List<ListItem>();
        list.Add(new ListItem(Resources.Site.OriginalStructure, "0"));
        list.Add(new ListItem("5", "5"));
        list.Add(new ListItem("10", "10"));
        list.Add(new ListItem("15", "15"));
        list.Add(new ListItem("20", "20"));
        list.Add(new ListItem("25", "25"));

        DropDownList1.DataSource = list;
        DropDownList1.DataTextField = "Text";
        DropDownList1.DataValueField = "Value";
        DropDownList1.DataBind();

        DropDownList2.DataSource = list;
        DropDownList2.DataTextField = "Text";
        DropDownList2.DataValueField = "Value";
        DropDownList2.DataBind(); 
    }   
}  

【讨论】:

  • 我已经尝试过这两种填充方法,但都不起作用。
猜你喜欢
  • 1970-01-01
  • 2021-11-03
  • 2014-11-25
  • 2021-08-11
  • 2013-06-23
  • 2021-01-09
  • 1970-01-01
  • 2019-07-08
  • 2013-05-24
相关资源
最近更新 更多