1

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

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

<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>

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

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 all return -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 获取数据,将来在重新构建和重新组织页面上的内容时可能会发生变化。无论如何,我认为它应该在这里输入,因为这是我重写它的原因。

如上所述,欢迎所有评论!谢谢!

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

4

2 回答 2

2

@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>
于 2011-04-07T12:07:56.923 回答
0

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

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

我希望这有帮助!

于 2011-04-07T09:45:19.840 回答