473,395 Members | 1,418 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,395 software developers and data experts.

Consuming usercontrol events in a webform

I am trying to capture an buttonclick on a usercontrol in the webform the usercontrol resides in. After researching this issue, I've come to believe that the best way of handling this is by raising an event in the control and consuming the event in the webform. I tried the following, unfortunatly while the usercontrol raises the event the webform method does not react.

UserControl (Named WebUserControl1 contains one button)

Public Delegate Sub MyEvtHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Event SaveClick As MyEvtHandler

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent SaveClick(sender, e)
End Sub

WebForm

Protected WithEvents ctrl As WebUserControl1

Private Sub handleclick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctrl.SaveClick
Response.Write("Handled")
End Sub
Is there anything else that needs to be set to cause the webform to consume the click event. I am new to ASP so my apologies in advance if the solution is simple.

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #1
2 1379
This has worked for me.
-Control Code Behind
Public Class Upload : Inherits System.Web.UI.UserControl
'...
Public Event UploadDone(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn1.Click
RaiseEvent UploadDone(Me, e)
End Sub
'...
End Class
-Page Code Behind
Public Class ImportEntryFromDelivery : Inherits System.Web.UI.Page
Private WithEvents _Upload1 As Upload
'...
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'...
_Upload1 = CType(Me.FindControl("Upload1"), Upload)
'...
End Sub
Private Sub _Upload1_UploadDone(ByVal sender As Object, ByVal e As
System.EventArgs) Handles _Upload1.UploadDone
'I handle the event
End Sub
'...
End Class

Hope this helps.
--
Gopal Rangaswamy
Microsoft Certified Solutions Developer
FMS, Inc.
<http://www.fmsinc.com/consulting>
<http://www.fmsinc.com/dotnet/SourceBook/>

"Craig Douthitt via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:dd******************************@DotNetMonste r.com...
I am trying to capture an buttonclick on a usercontrol in the webform the usercontrol resides in. After researching this issue, I've come to believe
that the best way of handling this is by raising an event in the control and
consuming the event in the webform. I tried the following, unfortunatly
while the usercontrol raises the event the webform method does not react.
UserControl (Named WebUserControl1 contains one button)

Public Delegate Sub MyEvtHandler(ByVal sender As Object, ByVal e As EventArgs) Public Event SaveClick As MyEvtHandler

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click RaiseEvent SaveClick(sender, e)
End Sub

WebForm

Protected WithEvents ctrl As WebUserControl1

Private Sub handleclick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctrl.SaveClick Response.Write("Handled")
End Sub
Is there anything else that needs to be set to cause the webform to consume the click event. I am new to ASP so my apologies in advance if the
solution is simple.
--
Message posted via http://www.dotnetmonster.com

Nov 19 '05 #2
Thanks for the response. After rewiewing your snippet, the only functional difference was that your code was assigning the actual usercontrol instance in the webform's page_load method so I added the following to my page_load method

ctrl = CType(Me.FindControl("WebUserControl11"), WebUserControl1)

Alas, no luck - the method is still not firing.

(I double-checked to make sure the findcontrol was successful)

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Iain | last post by:
Hi I have page (testCal.aspx) that contains a usercontrol (custCalendar.ascx) - see below signature for code. The UC contains a linkbutton which which when clicked, posts back and displays a...
9
by: Peter | last post by:
I have an Usercontrol on a WebForm, on this Usercontrol is a button, I want to save data that's residing on the WebForm when user clicks on the button that's on the UserControl. How do I call a...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
1
by: Kepler | last post by:
I have a custom control that is thrown onto a UserControl that is thrown onto a WebForm. Basically, I've got a scenario where if my UserControl sets an attribute on the custom control in the ascx,...
5
by: Xaviero | last post by:
I have a webform that contains a placeholder control that is filled with various usercontrols. I have an error/info message label on the webform (for interface clenliness). Sometimes the the...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
1
by: Apu Nahasapeemapetilon | last post by:
Hello and thank you in advance for your help. Can anyone think of a reason why this code would work properly on one PC, but not another? I've got a System.Windows.Forms.UserControl that...
10
by: Benton | last post by:
Hi there, I have a UserControl with a couple of textboxes and a couple of buttons ("Save" and "Cancel"). The Click event for this buttons is in the UserControl's codebehind of course, so here's...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.