【问题标题】:ListBox selections does not show up after autopostback自动回发后列表框选择不显示
【发布时间】:2011-04-06 08:57:38
【问题描述】:

我知道这不是第一次在此处发布有关此问题的问题,但我还没有设法找到我的问题的答案。

我的页面上有许多列表框:

<tr>
    <td class="loqhArea2Area">
        <asp:ListBox ID="ListBox1Val1" class="InputItem" runat="server" AutoPostBack="true"></asp:ListBox>
    </td>
    <td class="loqhArea3Area">
        <asp:ListBox ID="ListBox2Val1" class="InputItem" runat="server" AutoPostBack="true"></asp:ListBox>
    </td>
    <td class="loqhArea4Area">
        <asp:ListBox ID="ListBox3Val1" class="InputItem" runat="server"></asp:ListBox>
    </td>
</tr>

这些框在某种意义上是链接在一起的,第一个框中的选择用于填充第二个框和第四个框。为了从他们那里获取信息,我使用以下代码 sn-p:

protected override void OnInit(EventArgs e)
{
// Do some other stuff ...

    if (!IsPostBack)
    {
        // Fill the boxes on initial load
    }
    else
    {
        // INeedTheData takes an ID-string (in this case "Val1")
        // and the selected indexes as ints
        INeedTheData("Val1",
                      ListBox1Val1.SelectedIndex,
                      ListBox2Val1.SelectedIndex,
                      ListBox3Val1.SelectedIndex);

    }
    // Some error handling    
}

问题是 SelectedIndexes 都返回-1,这显然不是我需要的。

我一直在疯狂搜索这个问题的解决方案。欢迎提供所有线索或线索。提前致谢!

更新: 也许这对任何人都有任何线索,我的前任(不幸的是,我无法联系到他)实现了这个相当奇怪的代码,它确实有效。或者我应该说某种作品。问题是我们想要一些更可靠的代码,所以我开始重写它。

INeedTheData("Val1"
    , Request.Form.AllKeys.Contains("ctl01$ListBox1Val1") ? Request.Form["ctl01$ListBox1Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox1Val1"]) : 0
    , Request.Form.AllKeys.Contains("ctl01$ListBox2Val1") ? Request.Form["ctl01$ListBox2Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox2Val1"]) : 0
    , Request.Form.AllKeys.Contains("ctl01$ListBox3Val1") ? Request.Form["ctl01$ListBox3Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox3Val1"]) : 0);

此解决方案不可取,因为它使用硬编码的 html id 获取数据,将来在重新构建和重新组织页面上的内容时可能会发生变化。无论如何,我认为它应该在这里输入,因为这是我重写它的原因。

如上所述,欢迎所有 cmets!谢谢!

更新 ii(对@Deeptechtons 的回答):期望的行为 我有一组三个 ListBox,用于从树形图中导航和做出选择。第一个框 (ListBox1Val1) 直接从数据库中填充。第二个 (ListBox2Val1) 是空的,直到用户在第一个中选择了他的选项。这样做会导致第一个列表框中所选节点的子节点加载到第二个列表框中。第三个列表框 (ListBox3Val1) 也是如此。在第二个框中选择一个节点,然后填充第三个。

【问题讨论】:

  • 你为什么在 OnInit 而不是 PageLoad 中这样做?
  • 其实我也不知道。公平地说,我是该项目的新手,也是 asp.net 开发的新手。我会尝试移动代码!
  • @Lazarus:它给出了相同的行为。 :(
  • @Lazarus @dotmartin 你们能否在这些列表框上启用视图状态,然后重建项目并进行测试。 &lt;asp:ListBox ID="ListBox1Val1" class="InputItem" runat="server" EnableViewState="True" AutoPostBack="true"&gt;&lt;/asp:ListBox&gt;
  • @Deeptechtons:遗憾的是问题仍然存在。

标签: c# asp.net listbox autopostback


【解决方案1】:

@dotmartin 这是你在 Cs 文件中需要的代码

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBox1.DataSource = GetList();
            ListBox1.DataBind();
        }
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox2.DataSource = GetSecondList(ListBox1.SelectedIndex);
        ListBox2.DataBind();
    }

    protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox3.Items.Add(new ListItem(ListBox1.SelectedValue + "-" + ListBox2.SelectedValue, "Wippie"));
    }

    private ListItemCollection GetList()
    {
        ListItemCollection lstNumbers = new ListItemCollection();
        lstNumbers.Add(new ListItem("1", "One"));
        lstNumbers.Add(new ListItem("2", "Two"));
        lstNumbers.Add(new ListItem("3", "Three"));
        lstNumbers.Add(new ListItem("4", "Four"));
        lstNumbers.Add(new ListItem("5", "Five"));
        return lstNumbers;
    }

    private ListItemCollection GetSecondList(int iSelectedIndex)
    {
        ListItemCollection lstRandom = new ListItemCollection();
        System.Random RandNum = new System.Random();
        for (int i = 0; i < 10; i++)
        {
            lstRandom.Add(new ListItem(RandNum.Next(ListBox1.SelectedIndex, i + 1).ToString(), "random"));
        }
        return lstRandom;
    }

我刚刚生成了一些要绑定到列表框的随机数。

下面是aspx文件代码,

<form id="form1" runat="server">
        <asp:ScriptManager id="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel id="UpdatePanel1" runat="server" updatemode="Conditional" childrenastriggers="true">
                <ContentTemplate>
                    <div>
                        <asp:ListBox id="ListBox1" autopostback="true" runat="server" onselectedindexchanged="ListBox1_SelectedIndexChanged"
                            width="200"></asp:ListBox></div>
                    <div>
                        <asp:ListBox id="ListBox2" autopostback="true" runat="server" onselectedindexchanged="ListBox2_SelectedIndexChanged"
                            width="200"></asp:ListBox></div>
                    <div>
                        <asp:ListBox id="ListBox3" autopostback="true" runat="server" width="200"></asp:ListBox>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>

【讨论】:

  • 像魅力一样工作。感谢您的巨大努力!我们之前使用的实现的问题是代码的可读性和结构不够。你的例子澄清了很多事情。再次感谢您帮助我!非常感谢。
  • @dotmartin 欢迎您!当你走到尽头时,把你的问题堆在这里;D
【解决方案2】:

实际上,在 ASP.NET 中,控件事件在页面生命周期中的页面加载后发生:ASP.NET Page Life Cycle Overview

我猜(但不确定确切名称)下拉列表应该有一个SelectedIndexChanged 事件,您应该考虑在其中进行新选择。

我希望这会有所帮助!

【讨论】:

  • 感谢您的提示。有趣的链接,帮助我了解了很多我一直想知道的关于页面生命周期过程的事情。
猜你喜欢
  • 1970-01-01
  • 2012-10-12
  • 2014-11-15
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多