自动回发后列表框选择不显示

发布于 2024-10-30 16:44:35 字数 2370 浏览 4 评论 0原文

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

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

<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 全部返回 -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的回答):期望的行为 我有一组三个列表框,用于在树形图中导航和进行选择。第一个框 (ListBox1Val1) 直接从数据库填充。第二个 (ListBox2Val1) 是空的,直到用户在第一个中选择了他的选择。这样做会导致第一个列表框中所选节点的子节点加载到第二个列表框中。第三个列表框 (ListBox3Val1) 也是如此。在第二个框中选择一个节点,然后填充第三个节点。

I know that this is not the first time a question about this issue has been posted here, but I haven't managed to find an answer to my question.

I have a number of ListBoxes on my page:

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

These boxes are linked together in a sense, the choice in the first box is used to populate the second one and so fourth. In order to get the information from them I use this code snippet:

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    
}

The problem is that the SelectedIndexes all returns -1, which obviously is not what I need.

I have been googleing like crazy for a solution to this problem. All clues or leads are welcome. Thanks in advance!

Update:
Maybe this can be of any clue to anyone, my predecessor (who I have not been able to reach unfortunately) implemented this rather strange code that actually works. Or should I say sort of works. The thing is we wanted some kind of more reliable code so I set out to re-write it.

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

This solution is not desirable since it gets the data using hardcoded html id's, which may be subject to change when rebuilding and reorganizing stuff on the page in the future. Anyhow I thought it should be entered here since it is the reason for me to rewrite it.

As stated above, all comments are welcome! Thanks!

Update ii (answer to @Deeptechtons): Desired behaviour
I have a group of three ListBoxes used to navigate and make choices from a tree graph. The first box (ListBox1Val1) is populated directly from a database. The second (ListBox2Val1) is empty until the user has selected his choice in the first. Doing so causes the children of the selected node in the first listbox to load into the second. Same thing goes for listbox number three (ListBox3Val1). Select a node in the second box and the third one is populated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软的没边 2024-11-06 16:44:35

@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 Here is the code you need on the Cs file

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

i had just generated some random numbers to be binded to the listbox.

Below is the aspx file code,

<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>
街道布景 2024-11-06 16:44:35

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

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

我希望这有帮助!

Actually, in ASP.NET, control events happen after a page load in the page life-cycle: ASP.NET Page Life Cycle Overview.

I guess (but not sure about the exact name) the drop-down should have a SelectedIndexChanged event, where you should think of taking the new selection.

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文