【问题标题】:If / else statement ASP.NET C#if / else 语句 ASP.NET C#
【发布时间】:2012-01-26 20:44:11
【问题描述】:

截至目前,我正在尝试创建一个 ASP.NET 页面,该页面将根据您选择的类别按钮在列表框中列出一个类别中的书籍,然后我还有另外两个按钮(一个用于 DESC 顺序,一个用于ASC 订单)。现在的问题是,当我在点击虚构按钮并填充列表框后单击 ASC 或 DESC 按钮时,它会擦除​​列表框并将我返回到 pageload 事件。

我已经尝试将填充项目移动到页面加载事件,当我一切正常时,但由于某种原因,当通过其他按钮点击它时它就不起作用了。

我是 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 按钮时,它仍在重置 ListBox 中的数据.

【问题讨论】:

    标签: c# asp.net listboxitems


    【解决方案1】:

    您需要检查这是否是您的 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 中存储的内容呈现。

    【讨论】:

      【解决方案2】:

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

      对于排序方法添加数据绑定

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

      【讨论】:

      • 感谢您的帮助!特别是回发问题使得页面不再重新加载到页面加载事件中列出的内容,但是当我使用 ASC 或 DESC 按钮时它仍然在清除列表框。
      • 当您在排序方法中设置断点时,“书”列表可能为空。您可以将列表存储在 SessionState 中以避免此问题。
      【解决方案3】:

      检查页面加载和排序命令修改事件

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

      【讨论】: