2012-01-27 2 views
1

現在、私は、カテゴリのブックをリストするASP.NETページを作成しようとしています。リストボックスでは、選択したカテゴリボタンに基づいて、別の2つのボタンDESC順序とASC順序のために)。今問題は、フィクションボタンを押してリストボックスを消去した後に、ASCまたはDESCボタンをクリックしたときです。ASP.NETにリストボックスを設定する問題

私は以前同様の質問を掲載しましたが、いくつかの修正がありましたが、ASCまたはDESCボタンを押したときでもリストボックスを消去しています。

私はASP.NETには新しく、簡単な「newb-friendly」説明とコード例/修正が大歓迎です!

ありがとうございます!

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

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"); 

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

} 


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



} 

ASPXコードの下

コード:

&nbsp;<asp:Panel 
     ID="Navigation" runat="server" Height="276px" Width="197px" 
     CssClass="Navigation" BorderColor="Black" BorderStyle="Double"> 
    <asp:Button ID="Fiction" runat="server" Text="Fiction" Width="145px" 
      CssClass="Nav_buttons" onclick="Fiction_Click" /> 
    <asp:Button ID="Non_Fiction" runat="server" Text="Non-Fiction" Width="145px" 
      CssClass="Nav_buttons" onclick="Non_Fiction_Click" /> 
    <asp:Button ID="Self_Help" runat="server" Text="Self Help" Width="145px" 
      CssClass="Nav_buttons" onclick="Self_Help_Click" /> 
    <asp:Button ID="New_Item" runat="server" Text="Add New Item" Width="145px" CssClass="Nav_buttons" />   
</asp:Panel> 
<asp:Panel ID="Books_Panel" runat="server" CssClass="Books_Panel" Height="409px" 
     BorderStyle="Double"> 
     <asp:Label ID="Header_Label" runat="server" 
      style="top: 79px; left: 693px; position: absolute; height: 19px; width: 234px" 
      Text="Label"></asp:Label> 

     <asp:ListBox ID="Item_Listbox" runat="server" 


      style="top: 204px; left: 443px; position: absolute; height: 136px; width: 732px" 
      AutoPostBack="True"> 
     </asp:ListBox> 

     <asp:Button ID="Ascending_Button" runat="server" 
      style="top: 375px; left: 723px; position: absolute; height: 26px; width: 169px" 
      Text="Ascending Order" CommandName="Sort" CommandArgument="ASC" 
      OnCommand="Sort_Command" /> 

     <asp:Button ID="Descending_Button" runat="server" 
      style="top: 405px; left: 723px; position: absolute; height: 26px; width: 169px" 
      Text="Descending Order" CommandName="Sort" CommandArgument="DESC" 
      OnCommand="Sort_Command" /> 

     <asp:DropDownList ID="Cat_Menu" runat="server"> 
     </asp:DropDownList> 

    </asp:Panel> 
+0

あなたのaspxマークアップを投稿できますか? – ShankarSangoli

+0

元の投稿に追加 – user1062411

答えて

1

あなたはそれがハンドラをソートするために行くのリストをソートするASCまたはDESC]ボタンをクリックしますが、ここでbooksページポストバックの空であるので、空のデータソースがリストにバインドされています。

ソースを再バインドするか、ページのポストバックで使用できるようにビューステートで維持する必要があります。

このようなものを試してみてください。

private List<String> books 
{ 
    get{ 
     if(ViewState["books"] == null){ 
      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"); 
      ViewState["books"] = books; 
     } 
     return new List<String>((String[])ViewState["books"]); 
    } 
    set{ 
     ViewState["books"] = value; 
    } 
} 

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

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

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

私は初心者だと言っていたので、私がそれを歩いたときにビューステートが意味をなさないと理解したところで、データがどこにも行き渡っていないようでした。私はあなたが提案したものを試してみましたが、今このエラーを受け取っています: – user1062411

+0

Invaild Cast Exception – user1062411

+0

私の編集した答えを試してみて、キャスト中に 'string'を' String'に変更してみてください。 – ShankarSangoli

関連する問題