【问题标题】:transfer listbox items from one asp.net page to another using session使用会话将列表框项目从一个 asp.net 页面转移到另一个页面
【发布时间】:2014-06-06 17:33:14
【问题描述】:

使用会话将列表框项目从一个 asp.net 页面转移到另一个页面。我想将列表框的项目从一个 asp.net 页面转移到另一个页面。代码以某种方式抛出错误。列表框中的项目也没有被检索。我想对复选框列表做同样的事情。希望列表框问题也能帮助我解决这个问题。请指教。

首页

<asp:ListBox ID="SelectedItems" runat="server" SelectionMode="Multiple"/>     
<asp:Button ID="sbmtButton" runat="server" Text="Submit" Width="152px" OnClick="sbmtButton_Click" />

首页的.cs

protected void sbmtButton_Click(object sender, EventArgs e)
{Session["wrd"] = SelectedItems;Server.Transfer("~/aftrSubmit.aspx";);}

第二页

第二页的.cs

protected void Page_Load(object sender, EventArgs e)
{if (!this.IsPostBack){Prescription_list = (ListBox)Session["wrd"];}}

【问题讨论】:

    标签: asp.net session listbox listboxitem


    【解决方案1】:

    首页 ASPX 代码:

    <asp:ListBox ID="SelectedItems" runat="server" SelectionMode="Multiple">
        <asp:ListItem Text="A"></asp:ListItem>
        <asp:ListItem Text="B"></asp:ListItem>
        <asp:ListItem Text="C"></asp:ListItem>
    </asp:ListBox>
    <asp:Button ID="sbmtButton" runat="server" Text="Submit" Width="152px" OnClick="sbmtButton_Click" />
    

    首页 C# 代码隐藏:

    protected void sbmtButton_Click(object sender, EventArgs e)
    {
        ListItem[] x = new ListItem[SelectedItems.Items.Count];
    
        SelectedItems.Items.CopyTo(x, 0);
    
        Session["wrd"] = x;
    
        Server.Transfer("~/aftrSubmit.aspx");
    }
    

    第二页 ASPX 代码:

    <asp:ListBox ID="Prescription_list" runat="server" SelectionMode="Multiple"/>
    

    第二页 C# 代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
       if (!this.IsPostBack)
       {
           ListItem[] x = (ListItem[])Session["wrd"];
    
           Prescription_list.Clear();
           Prescription_list.Items.AddRange(x);
        }
    }
    

    【讨论】:

    • 谢谢。但这并没有帮助。这些值仍未继续到下一页。我想将列表框的所有项目转移到下一页。
    • 我提供的原始代码只添加了列表框中的选定项目。我现在更新了我的代码以添加所有项目。
    • 还是不行。它没有抛出任何错误,但没有携带值。
    • 请发布所有更新的代码。我自己对此进行了测试,它对我有用。您需要同时显示 Page1 和 Page2 的 ASPX 页面。
    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 2022-06-27
    • 2013-05-16
    • 1970-01-01
    • 2019-05-09
    • 2010-09-18
    • 1970-01-01
    相关资源
    最近更新 更多