我有一个webform,一旦完成将数据存储在字符串中,我需要它将存储的内容添加到另一个字符串中,该字符串在列表框中显示文本(在与webform不同的页面上)。
我对如何实现这一点非常无能为力(我知道它需要被调用和添加,但我不知道该怎么做)。
Heres是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();
}
}
答案 0 :(得分:0)
您不需要使用ViewState来完成您想要完成的任务,而我(我认为)将新输入的图书发布到另一个显示该类别中所有图书的页面。您可以使用“提交”按钮的PostBackUrl属性,以便表单不会“返回”到同一页面,而是发布到另一个页面(在您的情况下为partin.aspx)。您可以在html中编写按钮属性,如下所示:
<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>();
}
希望这可以帮助您解决问题。