Question

As of right now I am trying to create an ASP.NET page which will list books from a category, in a listbox based upon which category button you choose and then I have another two buttons (one for DESC order and one for ASC order). Now the issue is when I click on the ASC or DESC button after hitting the fiction button and populating the list box it wipes the list box and throws me back to the pageload event.

I have tried moving the populating items to the pageload event and when I have everything has worked perfect, but for some reason when doing it through the other button clicks it just won't work.

I'm pretty new to ASP.NET so simple or "newb-friendly" explanations and code-examples/fixes are very welcome!

Thanks in advance!

Code Below

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



}

EDIT: Thanks for the posts, it no longer goes straight back to the pageload event and keeps the change in the label to say "Fiction" but it still is reseting the data in the ListBox when I click the ASD or DESC button.

Was it helpful?

Solution

You need a check if this is a postback or not in your 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 fires every time. You usually put your page initialization logic there, but for postbacks (like button clicks), you don't want that initial code to be re-run. So put a check in there to if the page is in a postback state (IsPostback==true); if it isn't, then initialize the page. Otherwise, let the page render during the postback based upon what's stored in ViewState.

OTHER TIPS

Try putting the code from Page_PreRender in 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();  
    }  
}

and for the sort-metjhod add databinding

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

Check the page load and Sort Command modified event

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top