Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to access click event of server control within user control?

I am having a very difficult time understanding how to access (and code) click events from a server control that is loaded onto the base page inside a user control.

My page layout is basically:

Base page
User control containing navigation links (using LinkButtons)
Page content displayed depending on which LinkButton is clicked
End Base page

Here is my aspx_ex.aspx file:

<%@ Page Language="VB" Debug="True"%>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Register TagPrefix="MyMenu" TagName="A_Menu" Src="aspx_menu.ascx" %>
<%@ Reference Control="firstlink.ascx" %>
<%@ Reference Control="something.ascx" %>
<%@ Reference Control="other_one.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Aspx Example</title>
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form Runat="Server">
<div id="menu">
<MyMenu:A_Menu
ID="myMenu"
Runat="Server" />
</div>
<div id="maincontent">
<p><asp:PlaceHolder id="mycontent" runat="server" /></p>
</div>
</form>
</body>
</html>

Here is my code-behind file (aspx_ex.aspx.vb) for the .aspx file:

Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Here is the user control file aspx_menu.ascx:

<asp:LinkButton id="firstlink" Text="Home" OnClick="myClick" runat="server" />
<asp:LinkButton id="something" Text="Something" OnClick="myClick" runat="server" />
<asp:LinkButton id="other_one" Text="Other Thing" OnClick="myClick" runat="server" />

And the code-behind (aspx_menu.ascx.vb) for the user control:

Option Explicit
Option Strict
Imports System
Imports System.Web.UI
Public Class menuClick
Public Event myClick as EventHandler
Private Sub myClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim myStringBuilder as StringBuilder
Dim myCtl as UserControl
Dim junk as string
junk = sender.ClientID.ToString()
myStringBuilder = New StringBuilder(junk)
myStringBuilder.Append(".ascx")
junk = myStringBuilder.ToString()
myCtl = LoadControl(junk)
mycontent.Controls.Add(myCtl)
End Sub
End Class

I know this is pretty messed up. If I have all the LinkButton and the "myClick" event all on the .aspx page, it works just fine. As soon as I separate things out, I get this error message:
BC30456: 'myClick' is not a member of 'ASP_.aspx_menu_ascx'.

I have searched and searched many forums looking for an answer. I know the answer is out there, but all posts that I've found are too generic, like "you need to link a public event handler to the controls private class and you should be fine". They don't contain specific code with info on where to place that code.

Ultimately, what I want to do is have the output from the LinkButton load the user control into a placeholder on the hosting page. When new content is called for by clicking another LinkButton, the new content should replace the old. There should be default content in the placeholder when first accessing the page.

I feel that once I understand how to do this, it will get me far down the road in my understanding of ASP.NET programming. I have read all I can from "ASP.NET Programming Step-by-Step using Visual Basic .NET" and "ASP.NET Unleashed" but neither deals with this specific issue.

I will appreciate any (very specific) help given.

Thank you!

Bruce
[4182 byte] By [bwwhite] at [2007-11-11 6:49:59]
# 1 Re: How to access click event of server control within user control?
I am having the same problem.
BigRollTide at 2007-11-11 23:14:10 >
# 2 Re: How to access click event of server control within user control?
Declare a event in the usercontrol like this
public event CommandEventHandler PageClick;

Add attributes CommandArgument="linkNamesomethingunique" OnCommand="lnkClick" in the user control to each link

In method lnkClick do something like this
if (PageClick != null)
{
string lnkClicked = e.CommandArgument.ToString();
CommandEventArgs args = new CommandEventArgs("PageClicked", lnkClicked);
PageClick(this, args);
}

This will ensure when a event is raised by the link button it identifies the link and raises it on to the page as an event on the user control with additional information of the commandargument

In your page in the page load add an event handler as below
userControlID.PageClick += new System.Web.UI.WebControls.CommandEventHandler(uc_PageClick);

The signature of the uc_PageCLick is :
private void uc_PageClick(object sender, System.Web.UI.WebControls.CommandEventArgs e)

In this method you can get the link which was clicked and then evaluate what you want to do based on the e.CommandArgument

Guess this should help
srinivas_s at 2007-11-11 23:15:10 >