Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events from User Controls

Status
Not open for further replies.

jino

Programmer
Apr 1, 2004
41
0
0
CA
Hi There,

I have looked at various sites to solve my problem. The button click event of my user control does not fire. I have created a user control and dropped it into a web form.

I have added the user control as a member variable to the parent control. It is the wiring of the events that I am having issues with.

I found a few sites suggesting using the 'raising the event' to the parent control. Even though I understand the concept, I am confused by the examples. They do not clearly mention where the code goes into - User Control HTML or USer Control Code Behind or Parent control HTML or Code Behind?

Please take a look at the solution offered at this site:


The example is in c# and that adds to my confusion. Can anyone explain to me where the code fits into my solution? I also need the whole code translated into Vb.net.

Please let me know if you need further information about my situation. I really appreciate all of your help so far. Please keep in mind that I am very new to the VS.NET environment.

Thanks

jino
 
Jino,

Yes, what does your user control do? What do you need to happen when the button is clicked?

Have you considered handling the event within the control itself?

LL
 
This is one way, please excuse the long post.
User Control ascx
Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="UCEnterName.ascx.vb" Inherits="TestStoredProcs.UCEnterName" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] %>
Enter Name:<ASP:TEXTBOX id="txtName" Runat="server"></ASP:TEXTBOX></LABEL></LABEL>
<BR>
<ASP:BUTTON id="btnEnterName" Runat="Server" Text="Send Name"></ASP:BUTTON>
User Control ascx.vb
Code:
Public Class UCEnterName
    Inherits System.Web.UI.UserControl
    Public Event ButtonClickEvent As ButtonClickEventHandler

    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
    End Sub

    Private Sub btnEnterName_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnEnterName.Click
        Dim bceh As New ButtonClickEventArgs
        bceh.MyString = txtName.Text
        RaiseEvent ButtonClickEvent(Me, bceh)
    End Sub
End Class

Public Delegate Sub ButtonClickEventHandler(ByVal sender As Object, ByVal e As ButtonClickEventArgs)

Public Class ButtonClickEventArgs
    Inherits EventArgs
    Public strName As String

    Public Property MyString() As String
        Get
            Return strName
        End Get
        Set(ByVal Value As String)
            strName = Value
        End Set
    End Property
End Class
aspx of User Control Client
Code:
<%@ Register TagPrefix="uc1" TagName="UCEnterName" Src="UCEnterName.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<TITLE>UCClient</TITLE>
	</HEAD>
	<BODY>
		<FORM id="Form1" method="post" runat="server">
			<UC1:UCENTERNAME id="myUCEnterName" runat="server"></UC1:UCENTERNAME>
			<BR>
			Name from User Control: <ASP:LABEL ID="lblNameFromUCEnterName" Runat="server"></ASP:LABEL>
		</FORM>
	</BODY>
</HTML>
aspx.vb of User Control Client
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="UCClient.aspx.vb" Inherits="TestStoredProcs.UCClient"%>
Public Class UCClient
    Inherits System.Web.UI.Page
    Protected WithEvents myUCEnterName As UCEnterName
    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
    End Sub
    Sub myUCEnterName_ButtonClickEvent(ByVal sender As Object, ByVal e As ButtonClickEventArgs) Handles myUCEnterName.ButtonClickEvent
        lblNameFromUCEnterName.Text = (e.MyString)
    End Sub
End Class
I thought the link you posted took some things for granted.
Hope this helps,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top