添加两个字符串并显示在列表框中

本文关键字:显示 串并 列表 字符串 字符 两个 添加 | 更新日期: 2023-09-27 18:21:38

我有一个webform,它完成后将数据存储在字符串中,我需要它将存储的内容添加到另一个字符串中,该字符串在列表框中显示文本(在与webform不同的页面上)。

我对如何实现这一点一无所知(我知道需要调用并添加它,但我不确定如何做到)。

这是网络表单的类:

public partial class _Default : System.Web.UI.Page
{
    string non_fiction;
    string fiction;
    string self_help;
    protected void Submit_btn_Click(object sender, EventArgs e)
    {
        if (Cat_DropDownList.SelectedIndex == 0)
        {
            fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
        }
        if (Cat_DropDownList.SelectedIndex == 1)
        {
            non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
        }
        if (Cat_DropDownList.SelectedIndex == 2)
        {
            self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
        }
    }
}

这是另一页的类,其中包含新书应该添加到的列表框:

public partial class partin : System.Web.UI.Page
{
    private List<String> books = new List<String>();
    int SortASC(string x, string y){ return String.Compare(x, y);}
    int SortDESC(string x, string y){ return String.Compare(x, y) * -1;}
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Header_Label.Text = "Welcome! Please select a book category.";
        }
    }
    protected void Fiction_Click(object sender, EventArgs e)
    {
        PopulateFiction();
    }
    protected void PopulateFiction()
    {
        Item_Listbox.Items.Clear();
        Header_Label.Text = "Fiction Section";
        books.Add("Title: The Old Man and The Sea | Decription: An epic novel. | Price: 10 USD | Quantity: 3");
        books.Add("Title: A Game of Thrones | Decription: A tale of fire and ice. | Price: 15 USD | Quantity: 6");
        books.Add("Title: Dracula | Decription: A book about vampires. | Price: 5 USD | Quantity: 7");
        books.Add("Title: Twilight | Decription: An awful book. | Price: Free | Quantity: 1000");
        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();
        ViewState["books"] = books;
    }
    protected void Non_Fiction_Click(object sender, EventArgs e)
    {
        Header_Label.Text = "Non-Fiction Section";
        Item_Listbox.Items.Clear();
        Header_Label.Text = "Fiction Section";
        books.Add("Title: zzzThe Old Man and The Sea | Decription: An epic novel. | Price: 10 USD | Quantity: 3");
        books.Add("Title: zzzA Game of Thrones | Decription: A tale of fire and ice. | Price: 15 USD | Quantity: 6");
        books.Add("Title: zzzDracula | Decription: A book about vampires. | Price: 5 USD | Quantity: 7");
        books.Add("Title: zzzTwilight | Decription: An awful book. | Price: Free | Quantity: 1000");
        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();
        ViewState["books"] = books;
    }
    protected void Self_Help_Click(object sender, EventArgs e)
    {
        Header_Label.Text = "Self Help Section";
    }
    protected void Sort_Command(object sender, CommandEventArgs e)
    {
        books = (List<string>)ViewState["books"];
        if (e.CommandName == "Sort")
        {
            switch (e.CommandArgument.ToString())
            {
                case "ASC":
                    books.Sort(SortASC);
                    break;
                case "DESC":
                    books.Sort(SortDESC);
                    break;
            }
        }
        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();
    }
}

添加两个字符串并显示在列表框中

您不需要使用ViewState来完成您想要完成的任务,即(我认为)将新输入的图书发布到另一个显示该类别中所有图书的页面。您可以使用Submit按钮的PostBackUrl属性,这样表单就不会"返回"到同一个页面,而是发布到另一个页面(在您的例子中是partin.aspx)

<asp:Button ID="Submit_btn" runat="server" onclick="Submit_btn_Click" PostBackUrl="~/partin.aspx" Text="Submit" />

然后,在您发布到的页面(partin.aspx)上,您将希望检索输入的值。您可以使用Request.Form属性来完成此操作,类似于以下方法:

private string GetBookAddedToForm()
{
    string bookDetails  = "Title: " + Request.Form["Titletxt"] + " | " + "Description: " + Request.Form["Descriptiontxt"] + " | " + "Price: " + Request.Form["Pricetxt"] + " | " + "Quantity: " + Request.Form["Quantitytxt"];
        return bookDetails;
}

这是默认页面的html。

<%@ Page Title="Home Page" Language="C#"  AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>StackOverflow Question</title></head>
<body>
<form id="Form1" runat="server">
    <p>
        <asp:Label ID="Label5" runat="server" Text="Category" />
        <asp:DropDownList ID="Cat_DropDownList" runat="server" Height="21px" 
            Width="161px"></asp:DropDownList>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Title" /><asp:TextBox ID="Titletxt" runat="server"></asp:TextBox>
        <br/>
        <asp:Label ID="Label2" runat="server" Text="Description" /><asp:TextBox ID="Descriptiontxt" runat="server"></asp:TextBox>
        <br/>
        <asp:Label ID="Label3" runat="server" Text="Price" /><asp:TextBox ID="Pricetxt" runat="server"></asp:TextBox>
        <br/>
        <asp:Label ID="Label4" runat="server" Text="Quantity" /><asp:TextBox ID="Quantitytxt" runat="server"></asp:TextBox>
    </p>
    <p>
        <asp:Button ID="Submit_btn" runat="server" onclick="Submit_btn_Click" PostBackUrl="~/partin.aspx"
            Text="Submit" />
    </p>
</form>
</body>
</html>

以下是Default.aspx页面的代码。您不需要submit_click事件中的任何代码,因为您可以在它发布到的页面中处理选择。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] categories = new string[] { "fiction", "non_fiction", "self_help" };
        Cat_DropDownList.DataSource = categories;
        Cat_DropDownList.DataBind();
    }
    protected void Submit_btn_Click(object sender, EventArgs e)
    {
    } 
}

现在是partin.aspx.的html

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="partin.aspx.cs" Inherits="WebApplication2.partin" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Header_Label" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:DropDownList ID="Category_DropDownList" runat="server" Height="16px" 
            Width="208px"></asp:DropDownList>
        <asp:ListBox ID="Item_Listbox" runat="server" Height="166px" Width="992px"></asp:ListBox>
    </div>
    </form>
</body>
</html>

ShowBooks方法确定上一页发布的图书类型,然后将其添加到partin.aspx页面的列表框中。它获取已经添加到存储库中的任何书籍,然后通过调用GetBookAddedToForm方法将新发布的书籍添加到其中。

public partial class partin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] categories = new string[] { "fiction", "non_fiction", "self_help" };
                Category_DropDownList.DataSource = categories;
                Category_DropDownList.DataBind();
                Header_Label.Text = "Welcome! Please select a book category.";
                string categorySelected = Request.Form["Cat_DropDownList"];
                ShowBooks(categorySelected);
            } 
        }
        private void ShowBooks(string category)
        {
            Item_Listbox.Items.Clear();
            List<string> books = null;
            switch(category)
            {
                case "fiction" :
                    Header_Label.Text = "Fiction Section";
                    books = GetFictionBooks();
                    break;
                case "non_fiction":
                    Header_Label.Text = "Non-Fiction Section";
                    books = GetNonFictionBooks();
                    break;
                case "self_help":
                    Header_Label.Text = "Self Help Section";
                    books = GetSelfHelpBooks();
                    break;
            }
            books.Add(GetBookAddedToForm());
            Item_Listbox.DataSource = books;
            Item_Listbox.DataBind(); 
        }
        private string GetBookAddedToForm()
        {
            string bookDetails  = "Title: " + Request.Form["Titletxt"] + " | " + "Description: " + Request.Form["Descriptiontxt"] + " | " + "Price: " + Request.Form["Pricetxt"] + " | " + "Quantity: " + Request.Form["Quantitytxt"];
            return bookDetails;
        }
        protected List<string> GetFictionBooks()
        {
            List<String> books = new List<String>(); 
            books.Add("Title: The Old Man and The Sea | Decription: An epic novel. | Price: 10 USD | Quantity: 3");
            books.Add("Title: A Game of Thrones | Decription: A tale of fire and ice. | Price: 15 USD | Quantity: 6");
            books.Add("Title: Dracula | Decription: A book about vampires. | Price: 5 USD | Quantity: 7");
            books.Add("Title: Twilight | Decription: An awful book. | Price: Free | Quantity: 1000");
            return books;
        } 
        private List<string> GetNonFictionBooks()
        {
            List<string> books = new List<string>();
            books.Add("Title: zzzThe Old Man and The Sea | Decription: An epic novel. | Price: 10 USD | Quantity: 3"); 
            books.Add("Title: zzzA Game of Thrones | Decription: A tale of fire and ice. | Price: 15 USD | Quantity: 6"); 
            books.Add("Title: zzzDracula | Decription: A book about vampires. | Price: 5 USD | Quantity: 7"); 
            books.Add("Title: zzzTwilight | Decription: An awful book. | Price: Free | Quantity: 1000");
            return books;
        }
        private List<string> GetSelfHelpBooks()
        {
            return new List<string>();
        }

希望这能帮你解决问题。