473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Can EventHandler fire in Page_PreRender event?

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
2 1954
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dominosly | last post by:
Okay, this is a rather complicated problem, so here is the short of it: I am using a custom designed user control that simply writes out a plain old html submit button to the page. When the...
5
by: JMWilton | last post by:
Is there a way to determine what has been added to the eventhandler list? I have code that uses += and -= to turn event handling code on and off. Apparently, it is possible to add the same event...
3
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the...
9
by: Christopher Weaver | last post by:
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form. So far,...
13
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
1
by: Alessandro Rossi | last post by:
Hi, I am having this problem: I have developed a composite component which has 2 components: a textbox and a button. I need to add an eventhandler to a button click. I have added the eventhandler,...
0
by: tafpin | last post by:
I have an application with a datagrid. In the IDE I have 2 template columns. The first has an image button and the second contains a link button. According to the results that I get back I must...
7
by: OfurGørn | last post by:
At runtime I generate 8 pictureboxes, when the user click on a picturebox I fire an onclick eventhandler. Because these pictureboxes are generate at runtime I do not have direct access to the their...
0
by: tshad | last post by:
In VS 2008, I have 2 pages, one aspx page and one ascx page. The PreRender event is not firing on the Contol page. The aspx page starts out: <%@ Page Language="VB" AutoEventWireup="true"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.