Event Ordering

R

Ron

I have single page in which I utlize user controls to populate the
page. One control loads a menu. I use the clicked event on the menu
to determine what content I load into place holder control.


Dim objPageSkin As Control = Nothing
objPageSkin = LoadControl(myControl.ascx)
PlaceHolder1.Controls.Add(objPageSkin)


The content is loaded from 1 to n user controls depending on the item
clicked. This works well and the appropriate page is loaded.
However, if the page has a web control that I need to react to an
event, the event does not fire (for instance a link button). Any help
would be appreciated

Ron
 
J

Jeffrey Tan[MSFT]

Hi Ron,

Based on my understanding, you dynamically load your user control depending
on your menu selection, but the web control in your usercontrol does not
fire event.

Is your Menu executed at server side? Is its click event at server side?

If it is server side event, it may disappear when next postback. This is
because your usercontrol is added ONLY in this event, once another postback
occurs, the usercontrol will not be loaded again, and the usercontrol will
disappear.

I am strange you did not mention this problem.

Normally, you should load your usercontrol in Page.Load event. In the
Page.Load event, you may get the menu selection through Page.Response.Form
property.

Once you succeed add the usercontrol in Page.Load event, the webcontrol
event will also work well.

============================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
R

ronharman

Thank you for your help. I checked to make sure menu user control is
set to run at server and it is. I made some other modifications and
there is progress but it created a new problem. In my default
PageLoad event, I have the followoing:

PlaceHolder1.Controls.Add(LoadControl("menu.ascx"))
PlaceHolder2.Controls.Add(LoadControl("content.ascx"))

Both lines work and the appropriate informaiton is rendered on teh
default page. When a menu item is clicked, a post back occurs and the
sequence is:

default page load
menu page load
content page load.

After these load, the menu click event fires. But it is too late. I
need to know what menu item was clicked so when the content user
control loads, it will load the correct content.

I could not figure out how to dertermine what item was clicked using
the page.response.

If I try to load the content from the render event, it wroks because
the menu click event has fired but the link button click event in the
content control no longer works. I am stuck.
 
J

Jeffrey Tan[MSFT]

Hi Ron,

Thanks very much for your feedback.

Yes, you can view the lifecycle of asp.net web control like this:
"Control Execution Lifecycle"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconControlExecutionLifecycle.asp

Through this lifecycle, if you load control after
IPostBackEventHandler.RaisePostBackEvent method(Such as in PreRender
event), your control's event will not fire. So you should load it in
Page.Load event.

Also, sorry, you should refer to Page.Request.Form property, not
Page.Response.Form property, I think I have a typo error. Now, let me tell
you how to determine the postback through Page.Reqest.Form property.

In asp.net, the postback are forced through 2 ways(These are all client
side html and javascript behavior):
1. Explicitly invoke Form.Submit() method in javascript.
2. Render out a <input> html element with "submit" type. (That is a submit
button)

Normally, Button web control will render through the second way. While most
other controls such as TextBox(with AutoPostBack=true), LinkButton control
etc, are all using the first approach.

For the first approach, they all will render
"javascript:__doPostBack('unique id attribute','')" for their event
handler. While "__doPostBack" is a rendered javascript function, like this:

<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>

In the function, "__EVENTTARGET" and "__EVENTARGUMENT" are "theform"'s 2
hidden fileds. Like this:
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />

So you will see that Asp.net will use this 2 hidden fileds to store the
postback causer's unique id, and related event parameter.

Then you may get the data at server side, through
Page.Request.Form["__EVENTTARGET"], the result value should be the "unique
id". Take "LinkButton" for example, it value should be "LinkButton1".

For the first approach, because it is a submit type <input> element, you
can get it at server side through: Page.Request.Form["button's unique id"],
the result value should be the "value" html attribute of that <input>
element(The default should be "Button")

For all my statement, you can determine through "View Source" of the
rendered result page.

At this point, I think I have explained clearly, the key point is the type
of your menu, are they linkbuttons? Or are they some other web controls?

If you use linkbuttons, they should have different unique names, you may do
like this to determine which linkbuttons are clicked:

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.Response.Write(this.Request.Form["__EVENTTARGET"]);
}
}

=====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
R

ronharman

Thank you for the information. Witth cod eand respouirce, I should
be able to handle it from here. OInce again, I apprecaite your help.


Hi Ron,

Thanks very much for your feedback.

Yes, you can view the lifecycle of asp.net web control like this:
"Control Execution Lifecycle"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconControlExecutionLifecycle.asp

Through this lifecycle, if you load control after
IPostBackEventHandler.RaisePostBackEvent method(Such as in PreRender
event), your control's event will not fire. So you should load it in
Page.Load event.

Also, sorry, you should refer to Page.Request.Form property, not
Page.Response.Form property, I think I have a typo error. Now, let me tell
you how to determine the postback through Page.Reqest.Form property.

In asp.net, the postback are forced through 2 ways(These are all client
side html and javascript behavior):
1. Explicitly invoke Form.Submit() method in javascript.
2. Render out a <input> html element with "submit" type. (That is a submit
button)

Normally, Button web control will render through the second way. While most
other controls such as TextBox(with AutoPostBack=true), LinkButton control
etc, are all using the first approach.

For the first approach, they all will render
"javascript:__doPostBack('unique id attribute','')" for their event
handler. While "__doPostBack" is a rendered javascript function, like this:

<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>

In the function, "__EVENTTARGET" and "__EVENTARGUMENT" are "theform"'s 2
hidden fileds. Like this:
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />

So you will see that Asp.net will use this 2 hidden fileds to store the
postback causer's unique id, and related event parameter.

Then you may get the data at server side, through
Page.Request.Form["__EVENTTARGET"], the result value should be the "unique
id". Take "LinkButton" for example, it value should be "LinkButton1".

For the first approach, because it is a submit type <input> element, you
can get it at server side through: Page.Request.Form["button's unique id"],
the result value should be the "value" html attribute of that <input>
element(The default should be "Button")

For all my statement, you can determine through "View Source" of the
rendered result page.

At this point, I think I have explained clearly, the key point is the type
of your menu, are they linkbuttons? Or are they some other web controls?

If you use linkbuttons, they should have different unique names, you may do
like this to determine which linkbuttons are clicked:

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.Response.Write(this.Request.Form["__EVENTTARGET"]);
}
}

=====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Ron,

I am glad I can help you :), you are welcome!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top