简体   繁体   中英

Help needed on nested web User Control event in ASP.NET

Scenario: I have a nested web user controls in my asp web page, and I have a button inside the child user control.

Web Page: references a parent control called “slot” which dynamically loads

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

<%@ Reference Control="~/UserControl/Slot.ascx" %>

Parent User Control: references a child control called “ray” which also dynamically loads

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Slot.ascx.cs"    
 Inherits="light.Slot" %>

<%@ Reference Control="~/UserControl/Ray.ascx" %>

Child User Control: has a button called “ButtonRay” which i want it to run on its click event.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Ray.ascx.cs" 
 Inherits="light.Ray" %>

<div id="AddRay" runat="server">

<asp:Button ID="ButtonRay" runat="server" Text="Ray" 
onclick="ButtonRay_Click"/>


</div>

Problem: The button does not fire its click event (ButtonRay_Click) from the server side

protected void ButtonComment_Click(object sender, EventArgs e)
{
    // this the code that i want to be executed
}

Maybe it's just me, but I've never seen controls referenced and loaded like that. Here's what I would try doing it this way (which works for me):

In your Home page, register your Slot control like this instead of referencing it like your were before:

<%@ Register TagName="Slot" TagPrefix="cc" Src="~/Slot.ascx" %>

And then add the control to your page with:

<cc:Slot ID="SlotControl" runat="server" />

Now in Slot.ascx, register and include the Ray control:

<%@ Register TagName="Ray" TagPrefix="cc" Src="~/Ray.ascx" %>

<cc:Ray ID="RayControl" runat="server" />

And now in your Ray control, add your button:

<asp:Button ID="ButtonRay" Text="Ray" OnClick="ButtonRay_OnClick" runat="server" />

And in the Ray.ascx.cs code-behind, add your event handler:

protected void ButtonRay_OnClick(object sender, EventArgs e)
{

}

When I add a break-point on this method, run the app, and click the button, the OnClick event is caught like it should be. I think what's happening is that merely referencing your controls doesn't wire up the events. If you were dynamically loading your controls, you have to use Page.LoadControl() so that it wires up correctly.

Let me know if you need more explanation.

EDIT:

I just noticed that your button click method name was not the same as the one your were asking it to call: you have the ButtonRay button doing ButtonRay_Click, but the name of your method is ButtonComment_Click.

in aspx Page.ie: Home.aspx.cs

override protected void OnInit(EventArgs e)

{
    InitializeComponent();
    base.OnInit(e);
}

private Button GetButton()

{


    Button Btn = (Button)SlotControl.FindControl("RayControl").FindControl("ButtonRay");
    return Btn;
}



private void InitializeComponent()
{


    /*   For button Event Initialization */

    Button Btnclk = GetButton();

    Btnclk.Click += new EventHandler(Btnclk_TextChanged);

}

void Btnclk_TextChanged(object sender, EventArgs e)

{

/* write ur code here.............*/

 }

in aspx Page.

override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private Button GetButton() { Button Btn = (Button)SlotControl.FindControl("RayControl").FindControl("ButtonRay"); return Btn; } private void InitializeComponent() { /* For button Event Initialization */

    Button Btnclk = GetButton();

    Btnclk.Click += new EventHandler(Btnclk_TextChanged);

} void Btnclk_TextChanged(object sender, EventArgs e)

{

/* write ur code here.............*/

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM