自动回退后,列表框选择项不显示



我知道这不是第一次关于这个问题的问题被张贴在这里,但我还没有设法找到我的问题的答案。

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

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

问题是selectedinindexes都返回-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)是空的,直到用户在第一个中选择了他的选择。这样做会导致第一个列表框中所选节点的子节点加载到第二个列表框中。同样的事情发生在列表框3 (ListBox3Val1)上。在第二个框中选择一个节点,然后填充第三个框。

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

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

我猜(但不确定确切的名称)下拉框应该有一个SelectedIndexChanged事件,在那里你应该考虑采取新的选择。

我希望这对你有帮助!

最新更新