-
Page Load [RESOLVED]
I'm not sure if this is a question even worth asking here, but coming from Window's Forms apps, to Web applications is a bit strange....
I got a LoadBorderStyles method, that loop through all the members in the BorderStyle (TreeView) enumeration, convert each member to string and pop it into a listbox. Obviously I only want this method to run once.
Then I got a Treeview, and apply the borderstyle the user click in the listbox to the treeview (covert the sting back to enumeration member).
This all work fine, but each time I click a borderstyle, it is applied correctly, but the the LoadBorderStyles method, which I call from the Page_Load is excecuted again.
When I add the following line to the LoadBorderStyles method : lstBorderStyles.Items.Clear(); i get a "Object reference not set to an instance of an object" error
Here is my code
Code:
public partial class TestWhosTreeView17 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myTreeView.TreeNodeSrc = "xml/treenode.xml";
myTreeView.DataBind();
myTreeView.Height = lstBorderStyles.Height;
myTreeView.Width = lstBorderStyles.Width;
LoadBorderStyles();
}
private void LoadBorderStyles()
{
lstBorderStyles.Items.Clear();
//loop through all the borderstyle enumeration members, convert to
//string and add to listbox
foreach (string s in Enum.GetNames(typeof(System.Web.UI.WebControls.BorderStyle)))
{
lstBorderStyles.Items.Add(s);
}
}
protected void lstBorderStyles_SelectedIndexChanged(object sender, EventArgs e)
{
//convert the selected item's text to a borderstyle enumeration member
//apply the borderstyle member to the treeview's borderstyle property
myTreeView.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), lstBorderStyles.SelectedItem.Text);
}
}
-
Re: Page Load
protected void Page_Load(object sender, EventArgs e)
{
myTreeView.TreeNodeSrc = "xml/treenode.xml";
myTreeView.DataBind();
myTreeView.Height = lstBorderStyles.Height;
myTreeView.Width = lstBorderStyles.Width;
if(!this.Page.IsPostBack)
LoadBorderStyles();
}
now the method LoadBorderStyle will only be run once, and that is when the page is loaded, not if it is just post back ;-)
-
Re: Page Load
aaaaah....I've seen that word in other pages, but never thougth about it twice...
Cool, will do that and also read up on what it does!
Thanx hey!
-
Re: Page Load