Есть файлы:
Index.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication1.Index" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Dynamically created controls</title>
</head>
<body>
<form id="form1" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text="Control1" />
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click" Text="Control2" />
<br />
<br />
<asp:PlaceHolder ID="ControlsPlaceHolder" runat="server" />
</form>
</body>
</html>
Index.aspx.cs
using System;
using System.Web.UI;
namespace WebApplication1
{
public partial class Index : Page
{
protected const string defaultControlUrl = "~/UserControls/Control1.ascx";
protected string ControlPathViewStateKey = "ControlPath";
public string ControlPath
{
get
{
return (string)ViewState[ControlPathViewStateKey];
}
set
{
ViewState[ControlPathViewStateKey] = value;
}
}
protected void Page_PreLoad(object sender, EventArgs e)
{
RestoreControl(ControlsPlaceHolder, defaultControlUrl);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
DisplayControl("~/UserControls/Control1.ascx", ControlsPlaceHolder);
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
DisplayControl("~/UserControls/Control2.ascx", ControlsPlaceHolder);
}
#region · Special service-methods for dynamically created user controls ·
protected void RestoreControl(Control destinationControl, string defaultUrl)
{
DisplayControl((ControlPath != null) ? ControlPath : defaultUrl, destinationControl);
}
protected void DisplayControl(string virtualPath, Control destinationControl)
{
if (virtualPath == null || destinationControl == null)
return;
Control control = LoadControl(virtualPath);
if (destinationControl.Controls.Count != 0)
destinationControl.Controls.Clear();
destinationControl.Controls.Add(control);
ControlPath = virtualPath;
}
#endregion
}
}
Control1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Control1.ascx.cs" Inherits="WebApplication1.UserControls.Control1" %>
Control 1<br />
<asp:Label ID="LabelControl1" runat="server" /><br />
<asp:Button ID="Button1Control1" runat="server" Text="Button 1 on control 1" OnClick="Button1Control1_Click" /><br />
<asp:Button ID="Button2Control1" runat="server" Text="Button 2 on control 1" OnClick="Button2Control1_Click" />
Control1.ascx.cs
using System;
using System.Web.UI;
namespace WebApplication1.UserControls
{
public partial class Control1 : UserControl
{
protected void Button1Control1_Click(object sender, EventArgs e)
{
LabelControl1.Text = "Button1Control1_Click";
}
protected void Button2Control1_Click(object sender, EventArgs e)
{
LabelControl1.Text = "Button2Control1_Click";
}
}
}
Control2.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Control2.ascx.cs" Inherits="WebApplication1.UserControls.Control2" %>
Control 2<br />
<asp:Label ID="LabelControl2" runat="server" /><br />
<asp:Button ID="Button1Control2" runat="server" Text="Button 1 on control 2" OnClick="Button1Control2_Click" /><br />
<asp:Button ID="Button2Control2" runat="server" Text="Button 2 on control 2" OnClick="Button2Control2_Click" />
Control2.ascx.cs
using System;
using System.Web.UI;
namespace WebApplication1.UserControls
{
public partial class Control2 : UserControl
{
protected void Button1Control2_Click(object sender, EventArgs e)
{
LabelControl2.Text = "Button1Control2_Click";
}
protected void Button2Control2_Click(object sender, EventArgs e)
{
LabelControl2.Text = "Button2Control2_Click";
}
}
}
Дело в том что когда нажимаешь на линк батоны, динамически создается контрол, и когда нажимаешь на любую кнопку этого контрола первый раз - обработчик события не срабатывает, только после второго нажатия. В чём проблема?
Прикрепленный файл: sample.zip
|