我有一個webform,一旦完成後將數據存儲在一個字符串中,我需要它添加存儲到另一個字符串的列表框中的文本(單獨顯示頁面比網頁表格)。添加兩個字符串並顯示在列表框中
我對如何做到這一點很不明智(我知道它需要被調用和添加,但我不知道如何做)。
繼承人是針對Web窗體類:
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();
}
}