|
Hi I am trying to create a LinkButton in Page_Load event, then attach a method to response to its Click event.
Here is the code:
private void Page_Load(object sender, System.EventArgs e)
{
// if (!IsPostBack)
{
LinkButton lb = new LinkButton();
lb.ID = "lb";
lb.Text = "lb";
Page.FindControl("Form1").Controls.Add(lb);
lb.Click +=new EventHandler(lb_Click);
}
}
private void lb_Click(object sender, EventArgs e)
{
Response.Write("abc");
}
It works fine if I comment out this line:
if (!isPostBack)
When I click the link button, "abc" is written on top of the page.
But when I include this line:
if (!isPostBack)
When I click the link button, "abc" is not written and the link button itself is missing.
What happened? I need the if(!isPostBack) because I need to implement some complicated processing the first time the page is loaded. How can I accomplish the requirement?
Thanks in advance.
|
|
|
The problem is a common one. ASP.NET is stateless, as with any web application.
EVERY SINGLE TIME your page is accessed, the following steps occur:
1. Controls are initialized. This includes the Page class itself, plus any "static" controls (those controls you put on the form at design time).
2. The StateBag is created and the ViewState object created. This occurs only on PostBack.
3. Form Post data is processed out of the HTTP Post Headers, and tied to appropriate controls that exist in the control hiearchy. Occurs only on PostBack.
4. The Load event occurs.
5. Raise the PostBack events. This is where your "control events" occur. Only on PostBack.
6. Save ViewState
7. Render the HTML, pass it back to ISS and thence to the user.
This happens EVERY TIME. So where are all of your controls while the user is viewing the page? Nowhere. They do not exist. They are created all over again in step 1.
In your code, you create a control in Step 3. Plus, you are trying to create it only the first time. When you are in a PostBack, you code to create the control doesn't run, so the control doesn't exist in that Page cycle. No control, no event. No event, no event handler. No event handler, no "Reponse.Write()".
You need to create your control regardless of a PostBack or not. If you wait to do it in the PageLoad, then the viewstate and post data will be out of sync, since those steps occur prior to the Load.
You need to create your control in the Init stage. Put your code to create the LinkButton inside the "InitializeComponent()" method.
Also, you should set the control's properties AFTER you add it to the form collection:
LinkButton lb = new LinkButton();
Page.FindControl("Form1").Controls.Add(lb);
lb.ID = "lb";
lb.Text = "lb";
lb.Click +=new EventHandler(lb_Click);
This is because you want your control "firmly seated" in the control hierarchy. Then you can safely add properties.
The point is... you have to do this EVERY TIME, because as soon as the server code is finished, everything "disappears".
Thomas D. Greer
www.tgreer.com
<Added>
"In your code, you create a control in Step 3." Should be "Step 4", Page_Load.
|
|
|
|
|
Thanks for all the detail! Now I am clear.
|
|
|
I have a follow-on question to this discussion. I have a button click event that fires and as soon as the code steps into the click subroutine, I no longer have values for most of my objects (in Locals window). Any ideas as to why this would happen? All values are there during Page_Load event, but as soon as I enter the click event handler, all values go away. The value in the Locals window is "error: cannot obtain value". Any help would be greatly appreciated.
|
|
|
|
|
|
|
|