locked
Capture button click event from user control RRS feed

  • Question

  • User541108374 posted

    Hi,

    I tried some implementation on how to capture a button's click event on a user control inside the page that holds that user control.

    This is the code for the user control:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="testUC.ascx.cs" Inherits="custom_events_testUC" %>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

       

    public partial class custom_events_testUC : System.Web.UI.UserControl
    {
        public event EventHandler T;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            T(this, e);
        }
    }


    And this is the code for the webform:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="eventtest.aspx.cs" Inherits="custom_events_eventtest" %>

    <%@ Register Src="testUC.ascx" TagName="testUC" TagPrefix="uc1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <uc1:testUC id="TestUC1" runat="server">
            </uc1:testUC><br /><br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
        </form>
    </body>
    </html>

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class custom_events_eventtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TestUC1.T += new EventHandler(TestUC1_T);
        }

        void TestUC1_T(object sender, EventArgs e)
        {
            Label1.Text = "No imagination at this point...";
        }
    }

    This code was created with C# in .NET 2.0 but should also work in .NET 1.1.


    Note however that in .NET 1.1 you must first make sure the code behind file knows about the user control because this isn't done automatically for you.
    This can be done with writing:

    protected TestUC TestUC1;

    Grz, Kris.

    Wednesday, November 2, 2005 1:48 PM

All replies

  • User1822650777 posted

    Hi,

    I am using .net 1.1 and I am writing this line of code in code behind file ----

    protected genCode genCode1;

    Here genCode is the TagName that I give in webform (.....i am not sure whether tagname is what I should use to declare usercontrol in code behind)

    And now it is giving this error --

    The type or namespace name 'genCode' could not be found (are you missing a using directive or an assembly reference?)

    what should I do to get it run..

    Sunday, November 6, 2005 11:57 PM
  • User541108374 posted

    Hi,

    it should be the classname of the user control. You can find the correct name of that in the code behind file of your user control.

    Grz, Kris.

    Monday, November 7, 2005 12:36 PM
  • User1634598542 posted
    Great example!! I've been working on something similar, but I may as well ditch it and use this. Thanks.
    Sunday, November 20, 2005 9:17 AM
  • User595895286 posted

    Hi, i have implemented the same example to my projects, it shows error, i am using asp.net 1.1

     'System.Web.UI.UserControl' does not contain a definition for 'T'

     

    can u give me the suggestion to correct it 


     

    Friday, July 6, 2007 3:14 AM
  • User541108374 posted

    Hi, 

    can u give me the suggestion to correct it

    can you provide the relevant pieces of code? I'm not using Visual Studio 2003 anymore so I can only suggest what I think but I can try. Is the usercontrol defined in the codebehind as a control of the type of that usercontrol like described in my original post?

    Grz, Kris.

    Friday, July 6, 2007 6:02 AM
  • User-572120483 posted

     Is that possible to capture the button click from  user control inside an row edit template of the grid,

     there is no problem in getting it outside the row edit template

    expecting an  reply 

    regards

    prakash 

    Friday, August 17, 2007 12:27 AM