codetoad.com
Home||ASP|ASP.Net|C++/C#|DHTML|HTML|Java|Javascript|Perl|VB|XML||CodeToadPlus!||Forums||RAM
Search Site:
Search Forums:
Adding controls to the page in Page_Load event jasony001 at 19:27 on Wednesday, April 06, 2005

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.


Re: Adding controls to the page in Page_Load event tgreer at 14:49 on Thursday, April 07, 2005

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.


Re: Adding controls to the page in Page_Load event jasony001 at 15:10 on Thursday, April 07, 2005

Thanks for all the detail! Now I am clear.

Re: Adding controls to the page in Page_Load event dpoindexter at 21:53 on Friday, July 22, 2005

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.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
matrix addition
Re: Storing data from HTML to Excel or TXT
Re: function within loop problem
Re: Ô‡´ò¥¯¥é¥Ö¤Ï ¥Æ©`¥é©`¥á¥¤¥É£ò£±£±¥¢¥¤¥¢¥ó ¤Î£··¬ ¤Ç¤¹
Re: Replace
Re: タイトリスト AP2アイアン 712�情�
Re: SMS from Perl using HTTP request
Re: Charl Schwartzel
Re: Adhyayan - Annual Student Conference and Online Coding Festival


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


Copyright codetoad.com 2001-2013