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
TPS
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
Linkbuttons.

Ideas, or comments?

Thanks.

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


P: n/a
Hi TPS,

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
this?

For convenitent refernce, here is a test page I've made, you can also have
a try on your side:
==========aspx page==============
<HTML>
<HEAD>
<title>dynamiclink</title>
<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"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function link_Click(id)
{
document.getElementById("topic_id").value = id;
document.forms[0].submit();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<table width="100%" align="left">
<tr>
<td><a href="#" onclick="link_Click('1');">Topic One</a></td>
</tr>
<tr>
<td><a href="#" onclick="link_Click('2');">Topic Two</a></td>
</tr>
<tr>
<td><a href="#" onclick="link_Click('3');">Topic Three</a></td>
</tr>
</table>
</td>
<td><FONT face="">
<asp:PlaceHolder id="phMain"
runat="server"></asp:PlaceHolder></FONT></td>
</tr>
<tr>
<td><input type="hidden" runat="server" id="topic_id" value="0"></td>
</tr>
</table>
</form>
</body>
</HTML>

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

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

phMain.Controls.Add(lnk);
}
}
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.
//
InitializeComponent();
base.OnInit(e);
}

/// <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);

}
#endregion
}
============================================

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx


Nov 18 '05 #2

P: n/a
TPS
Steven,

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.

TPS

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.