2012-01-28 84 views
-2

我有一个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(); 
    } 
} 

回答

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>(); 
     } 

希望这可以帮助你解决你的问题。

相关问题