首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >If / else语句ASP.NET C#

If / else语句ASP.NET C#
EN

Stack Overflow用户
提问于 2012-01-27 04:44:11
回答 3查看 2.9K关注 0票数 2

到目前为止,我正在尝试创建一个ASP.NET页面,它将列出一个类别中的图书,在一个列表框中,根据你选择的类别按钮,然后我有另外两个按钮(一个用于下单和一个下单)。现在的问题是,当我点击虚构按钮并填充列表框后单击ASC或DESC按钮时,它会擦除列表框,并将我抛回pageload事件。

我曾尝试将填充项目移动到页面加载事件,当我完成所有操作时,一切都很完美,但由于某些原因,当通过其他按钮单击来执行此操作时,它就不起作用了。

我是ASP.NET的新手,所以非常欢迎简单的或者“新的友好”的解释和代码示例/修复!

提前感谢!

下面的代码

代码语言:javascript
运行
复制
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中的数据。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-27 04:48:03

您需要检查这是否是Page_Load中的回发:

代码语言:javascript
运行
复制
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中存储的内容呈现。

票数 3
EN

Stack Overflow用户

发布于 2012-01-27 04:54:18

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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2012-01-27 04:54:51

尝试将Page_PreRender中的代码放入Page_Load中

代码语言:javascript
运行
复制
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();  
    }  
}

对于sort-metjhod添加数据绑定

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9025055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档