By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Can EventHandler fire in Page_PreRender event?

P: n/a
I have some dynamic controls (LinkButton) that I am adding to my pages.

When the link button is clicked, I am getting an ID from the
LinkButton.CommandArguement value.

I am then using that ID to recreate some dynamic LinkButtons.

My problems is I need the CommandEventHandler to fire before the controls
are build so I can grab the ID that I will use to build the other

Ideas, or comments?


Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a

Thanks for posting here. As for this issue I notice that it is the same
problem related to another post titled
"Trouble with sequence of page build"
in the group, yes?

As I've mentioned in the previous reply. The main problem is that you get
the ID information in the (first created ) LinkButton's click event , and
then build the new LinkButtons and wire up the event handler , then, since
all the controls (on a web page) need to be created( wire up event handler)
before the page processing its controls's post back event. So we'd create
and add them in Page's Init or Load event. If we create them in other
controls' post back event or Page_Render.. that'll be too late.

As for your situation, I think you can consider the following means:
1. Use Normal HyperLink instead of LInkbuttons for those menu links. Then,
whne the hyperlink is clicked, use javascript to set the Id infomation in a
input hidden field(<input type="hidden" >) and then, in the Page_Load
retrieve this hiddenfiled's value and create the certain Linkbuttons(and
register event handler for it) according to the value. How do you think of

For convenitent refernce, here is a test page I've made, you can also have
a try on your side:
==========aspx page==============
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
<script language="javascript">
function link_Click(id)
document.getElementById("topic_id").value = id;
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<table width="100%" align="left">
<td><a href="#" onclick="link_Click('1');">Topic One</a></td>
<td><a href="#" onclick="link_Click('2');">Topic Two</a></td>
<td><a href="#" onclick="link_Click('3');">Topic Three</a></td>
<td><FONT face="">
<asp:PlaceHolder id="phMain"
<td><input type="hidden" runat="server" id="topic_id" value="0"></td>

===============code behind===============
public class dynamiclink : System.Web.UI.Page
protected System.Web.UI.WebControls.PlaceHolder phMain;
protected System.Web.UI.HtmlControls.HtmlInputHidden topic_id;

private void Page_Load(object sender, System.EventArgs e)

protected void CreateLinkButton()
int tid = int.Parse(topic_id.Value);
if(tid>0 && tid<4)
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + tid;
lnk.Text = "LinkButton" + tid;
lnk.Click +=new EventHandler(lnk_Click);

protected void lnk_Click(object source, EventArgs argument)

Response.Write("<br>" + ((LinkButton)source).ID + " is clicked!" );

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);


Hope helps. Thanks.


Steven Cheng
Microsoft Online Support

Get Secure!
(This posting is provided "AS IS", with no warranties, and confers no

Get Preview at ASP.NET whidbey

Nov 18 '05 #2

P: n/a

Thank you very much for the example. It was very helpful in my solution.

That did the trick and I can get back to coding again.

Thanks again.


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.