views:

999

answers:

3

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
}
+1  A: 

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.

Cory Larson
When you register your user control, you can only see a single instance of thatcontrol in the webpage. However, if you use the reference declarative, you can have as many instances as you like (slot = (Slot)LoadControl("~/UserControl/Slot.ascx")). that is why i used references. thanks
If you use LoadControl you don't even need to reference it. And I can register a control and use it however many times I want (e.g. a custom Button or Hyperlink control). It would be pretty useless if you couldn't do that.
Cory Larson
A: 

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.............*/

}

A: 

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.............*/

 }