Postback Events Not Raised

G

Guest

Hi,

I have a user control embedded in another user control that is embedded in a
web form

Page.aspx < Module.ascx < ProblemHere.ascx

In ProblemHere.ascx I have a regular <asp:Button and a <asp:LinkButton and
am attempting to raise the Command event when one or both are clicked.
Unfortunately the Command event is not being raised on postback even though
the event handler is wired up. The Page_Load event is raised when either is
clicked and when I debugged Page_Load I found that __EVENTTARGET contains the
ID of the LinkButton when it posts back, so that seems to be working
correctly. I even put the event handler wire up in the Button's and
LinkButton's declaration:

<asp:Button onCommand="btnUpdate_Command"

but that didn't help. Does anyone know why the page would post back and
raise Page_Load but not the Command event? I don't have any Repeaters or
DataBind commands that would erase it on postback either and EnableViewState
= True. Any help would be appreciated! Thanks!
 
G

Guest

Hi Mark,

Not sure if this is what you are after, but I wonder if you are digging deep
enough to assign the event handler for the button. I put the code in
page.aspx but have to go through a couple of hoops (actually user controls)
to get a reference to the button.

Anyway, the code that I came up with below works, but maybe doesn't do what
you want. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

' page.aspx
<%@ Page Language="VB" %>
<%@ Register TagPrefix="uc0" TagName="Module" Src="Module.ascx" %>
<script runat="server">

Sub Page_Load

dim usr1 as usercontrol
dim usr2 as usercontrol
dim btn as button
usr1=page.findcontrol("UserControl1")
if not isnothing(usr1) then
usr2=usr1.findcontrol("UserControl1")
if not isnothing(usr2) then
btn=usr2.findcontrol("Button1")
if not isnothing(btn) then
AddHandler btn.click, AddressOf ButtonClick
end if
end if
end if
End sub

Sub ButtonClick(sender As Object, e As System.EventArgs)
dim btn as button
btn=ctype(sender,button)
response.write(btn.commandargument)
End sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<uc0:Module id="UserControl1" runat="server"></uc0:Module>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
<!-- Insert content here -->
</p>
</form>
</body>
</html>


' Module.ascx
<%@ Control Language="VB" %>
<%@ Register TagPrefix="uc0" TagName="ProblemHere" Src="ProblemHere.ascx" %>
<uc0:problemHere id="UserControl1" runat="server"></uc0:problemHere>


' ProblemHere.ascx
<%@ Control Language="VB" %>
<asp:Button id="Button1" CommandName="ButtonClicked"
CommandArgument="WasClicked" runat="server" Text="Button"></asp:Button>
 
G

Guest

Ken,

Sorry about taking so long to get back to you. I've checked that your code
works and am working on putting together a sample based on it that
illustrates the problem I'm having. I'll post it probably over the next day
or two. Thanks for your help!

Ken Cox said:
Hi Mark,

Not sure if this is what you are after, but I wonder if you are digging deep
enough to assign the event handler for the button. I put the code in
page.aspx but have to go through a couple of hoops (actually user controls)
to get a reference to the button.

Anyway, the code that I came up with below works, but maybe doesn't do what
you want. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

' page.aspx
<%@ Page Language="VB" %>
<%@ Register TagPrefix="uc0" TagName="Module" Src="Module.ascx" %>
<script runat="server">

Sub Page_Load

dim usr1 as usercontrol
dim usr2 as usercontrol
dim btn as button
usr1=page.findcontrol("UserControl1")
if not isnothing(usr1) then
usr2=usr1.findcontrol("UserControl1")
if not isnothing(usr2) then
btn=usr2.findcontrol("Button1")
if not isnothing(btn) then
AddHandler btn.click, AddressOf ButtonClick
end if
end if
end if
End sub

Sub ButtonClick(sender As Object, e As System.EventArgs)
dim btn as button
btn=ctype(sender,button)
response.write(btn.commandargument)
End sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<uc0:Module id="UserControl1" runat="server"></uc0:Module>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
<!-- Insert content here -->
</p>
</form>
</body>
</html>


' Module.ascx
<%@ Control Language="VB" %>
<%@ Register TagPrefix="uc0" TagName="ProblemHere" Src="ProblemHere.ascx" %>
<uc0:problemHere id="UserControl1" runat="server"></uc0:problemHere>


' ProblemHere.ascx
<%@ Control Language="VB" %>
<asp:Button id="Button1" CommandName="ButtonClicked"
CommandArgument="WasClicked" runat="server" Text="Button"></asp:Button>



Solel Software said:
Hi,

I have a user control embedded in another user control that is embedded in a
web form

Page.aspx < Module.ascx < ProblemHere.ascx

In ProblemHere.ascx I have a regular <asp:Button and a <asp:LinkButton and
am attempting to raise the Command event when one or both are clicked.
Unfortunately the Command event is not being raised on postback even though
the event handler is wired up. The Page_Load event is raised when either is
clicked and when I debugged Page_Load I found that __EVENTTARGET contains the
ID of the LinkButton when it posts back, so that seems to be working
correctly. I even put the event handler wire up in the Button's and
LinkButton's declaration:

<asp:Button onCommand="btnUpdate_Command"

but that didn't help. Does anyone know why the page would post back and
raise Page_Load but not the Command event? I don't have any Repeaters or
DataBind commands that would erase it on postback either and EnableViewState
= True. Any help would be appreciated! Thanks!
 
S

Steven Cheng[MSFT]

Hi Mark,

Thanks for your followup. If you have any further questions or need any
assistance, please feel free to post here or open a new thread in this
group.

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

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
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top