2012-01-28 79 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>(); 
     } 

希望這可以幫助你解決你的問題。

相關問題