How To Call Child user Control Events on Parent Pages
I have two User Controls. And one aspx page which is loading the controls. Code is given Below
Home.aspx:
<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="MYApp.Home" %>
<asp:Panel ID="pnlTabs" runat="server"></asp:Panel>
Home.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
base.LoadUserControl(UC_Tabs, pnlTabs);
}
Tabs.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_Tabs.ascx.cs" Inherits="MyApp.UserControls.UC_Tabs" %> <asp:Button ID="btnPhotoTab" runat="server" Text="Photos" CssClass="tab_style" style="border-right:none;" onclick="btnPhotoTab_Click" />
<asp:Panel ID="pnlPhotos" runat="server"></asp:Panel>
Tabs.ascx.cs:
protected void btnActivityFeed_Click(object sender, EventArgs e)
{
Home HomePage = (Home)this.Page;
LoadUserControl("UC_PhotoSearch.ascx", pnlPhotos);
}
public Control LoadUserControl(string ControlName, Control Container)
{
Control UControl = null;
if (ControlName != null)
{
UControl = this.LoadControl("~/UserControls/" + ControlName);
开发者_如何学C UControl.ID = UControl.GetType().Name;
Container.Controls.Add(UControl);
}
return UControl;
}
UC_PhotoSearch.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_PhotoSearch.ascx.cs" Inherits="MyApp.UserControls.UC_PhotoSearch" %>
<input id="txtSearch" name="txtSearch" type="text" runat="server" />
<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click" />
<span id="PhotoSearchedData" runat="server"></span>
UC_PhotoSearch.ascx.cs:
protected void btnSearch_Click(object sender, EventArgs e)
{
//Photo Search Code here
}
The above is all my code. Now I am loading the Tabs control on Home page and
Photo search Control on Tabs control
Now I click on Search Button But the
btnSearch_Click in not firing and even the page load event is also not firing.
How can I able to Access the btnSearch_Click event from home page.
If I used the following on Tabs control then the button click is working fine.
<%@ Register Src=”UC_PhotoSearch.ascx” TagName=”PhotoSearch” TagPrefix=”uc1″ %>
<uc1:PhotoSearch ID=”PhotoSearch″ runat=”server” />
So what is wrong with dynamic loading of controls.
Please Help me
Not sure if I understand you quite, but I think if you load your controls in the Page's PreInit event and not Page_load your events will fire.
精彩评论