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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

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

  1. #1
    Join Date
    Sep 2005
    Posts
    3

    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:

    Code:
    <%@ 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:

    Code:
    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:

    Code:
    <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:

    Code:
    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

  2. #2
    Join Date
    Nov 2005
    Posts
    1

    I would love to know the answer to this question

    I am having the same problem.

  3. #3
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    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
    Sri

Similar Threads

  1. Click Event in User Control
    By Carlos in forum .NET
    Replies: 2
    Last Post: 04-24-2002, 12:21 PM
  2. Access 97 to 2000 Conversion Error
    By Michelle in forum VB Classic
    Replies: 8
    Last Post: 04-19-2002, 06:01 PM
  3. SQL server vs Access
    By Samantha in forum Database
    Replies: 6
    Last Post: 01-25-2002, 03:15 PM
  4. VB6 with MSSQL Server 7 or other
    By Peter Mount in forum VB Classic
    Replies: 10
    Last Post: 10-12-2001, 04:42 PM
  5. Replies: 0
    Last Post: 11-20-2000, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center