2012-01-26 322 views
2

截至目前,我正在嘗試創建一個ASP.NET頁面,該頁面將列出來自類別的書籍,列表框中的基於您選擇哪個類別的按鈕,然後我有另外兩個按鈕(一個用於DESC訂單,另一個用於ASC訂單)。現在的問題是,當我點擊小說按鈕並點擊了ASC或DESC按鈕並填充列表框時,它將清除列表框並將我引回頁面加載事件。如果/ else語句ASP.NET C#

我已經嘗試將填充項移動到頁面加載事件,並且當我擁有所有內容時都工作得很完美,但由於某種原因,通過其他按鈕點擊時它不起作用。

我對ASP.NET非常陌生,非常歡迎所以這麼簡單或「新手友好」的解釋和代碼示例/修復程序!

在此先感謝!

下面的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class partin : System.Web.UI.Page 
{ 
private List<String> books = new List<String>(); 

public void Page_PreRender() 
{ 
    Item_Listbox.DataSource = books; 
    Item_Listbox.DataBind(); 
} 

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) 
{ 
    Header_Label.Text = "Welcome! Please select a book category."; 


} 



protected void Fiction_Click(object sender, EventArgs e) 
{ 
    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"); 

} 


protected void Non_Fiction_Click(object sender, EventArgs e) 
{ 
    Header_Label.Text = "Non-Fiction Section"; 



} 
protected void Self_Help_Click(object sender, EventArgs e) 
{ 
    Header_Label.Text = "Self Help Section"; 



} 

protected void Sort_Command(object sender, CommandEventArgs e) 
{ 
    if (e.CommandName == "Sort") 
    { 
     switch (e.CommandArgument.ToString()) 
     { 
      case "ASC": 
       books.Sort(SortASC); 
       break; 
      case "DESC": 
       books.Sort(SortDESC); 
       break; 
     } 
    } 
} 



} 

編輯:謝謝你的職位,不再徑直回到了頁面加載事件,並保持在標籤的變化說「小說」,但它仍然是正在重置的數據當我點擊ASD或DESC按鈕時列表框。

回答

3

你需要檢查,如果這是一個回發或不是在你的Page_Load:

if(!IsPostBack){ 
    Header_Label.Text = "Welcome! Please select a book category"; 
    //put your prerender logic in here, too...to populate the list of books. 
} 

的Page_Load觸發每次。您通常會將頁面初始化邏輯放在那裏,但對於回發(如按鈕單擊),您不希望重新運行初始代碼。因此,如果頁面處於回髮狀態(IsPostback == true),請在該處進行檢查;如果不是,則初始化頁面。否則,根據存儲在ViewState中的內容讓頁面在回發期間呈現。

0

檢查頁面加載和sort命令修改的事件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class partin : System.Web.UI.Page 
{ 
private List<String> books = new List<String>(); 

public void Page_PreRender() 
{ 
    Item_Listbox.DataSource = books; 
    Item_Listbox.DataBind(); 
} 

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."; 
    Item_Listbox.DataSource = books; 
    Item_Listbox.DataBind(); 

} 

} 



protected void Fiction_Click(object sender, EventArgs e) 
{ 
    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"); 

} 


protected void Non_Fiction_Click(object sender, EventArgs e) 
{ 
    Header_Label.Text = "Non-Fiction Section"; 



} 
protected void Self_Help_Click(object sender, EventArgs e) 
{ 
    Header_Label.Text = "Self Help Section"; 



} 

protected void Sort_Command(object sender, CommandEventArgs e) 
{ 
    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

嘗試把代碼Page_PreRender在Page_Load中

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     Header_Label.Text = "Welcome! Please select a book category."; 

     Item_Listbox.DataSource = books; 
     Item_Listbox.DataBind(); 
    } 
} 

和排序,metjhod添加數據綁定

protected void Sort_Command(object sender, CommandEventArgs e) 
{ 
    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

謝謝尋求幫助!特別是回發問題,使頁面不再重新加載到頁面加載事件中列出的內容,但是當我使用ASC或DESC按鈕時,它仍然正在清除列表框。 – user1062411

+0

當您在排序方法中放置斷點時,「書籍」列表可能爲空。您可以將該列表存儲在SessionState中以避免此問題。 – Koen