我在 TabContainers TabPanel 中有一个动态创建的 UpdatePanel。我还在新创建的 UpdatePanel 中创建了一个按钮。问题是,新按钮的点击事件不会被触发。这是 .aspx 和 .cs 的示例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NestedUpdatePanels.aspx.cs"
Inherits="ArminTestStranica.NestedUpdatePanels" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
Design time created controls
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn1" runat="server" Text="Des btn1" OnClick="btn1_click" />
<asp:TextBox ID="txt1" runat="server" Width="300px"></asp:TextBox>
<asp:TabContainer ID="TabContainer1" runat="server">
<asp:TabPanel ID="Tab1" runat="server" HeaderText="Tab 1">
<ContentTemplate>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn2" runat="server" Text="Des btn2" OnClick="btn2_click" />
<asp:TextBox ID="txt2" runat="server" Width="300px"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Tab 2">
<ContentTemplate>
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn3" runat="server" Text="Des btn3" OnClick="btn3_click" />
<asp:TextBox ID="txt3" runat="server" Width="300px"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn3" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
<br />
<br />
Runtime designed controls
<div>
<asp:UpdatePanel ID="UpdatePanel4" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="rbtn1" runat="server" Text="Run btn1" OnClick="rbtn1_click" />
<asp:TextBox ID="rtxt1" runat="server" Width="300px"></asp:TextBox>
<asp:TabContainer ID="TabContainer2" runat="server">
</asp:TabContainer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rbtn1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
namespace ArminTestStranica
{
public partial class NestedUpdatePanels : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_click(object sender, EventArgs e)
{
txt1.Text = "You have clicked at: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
}
protected void btn2_click(object sender, EventArgs e)
{
txt2.Text = "You have clicked at: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
}
protected void btn3_click(object sender, EventArgs e)
{
txt3.Text = "You have clicked at: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
}
protected void rbtn1_click(object sender, EventArgs e)
{
rtxt1.Text = "You have clicked at: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
TabContainer2.Tabs.Clear();
TabPanel firstNewTabPanel = new TabPanel();
firstNewTabPanel.ID = "runFirstTabPanel";
firstNewTabPanel.HeaderText = "Run Tab 1";
UpdatePanel firstNewUpdatePanel = new UpdatePanel();
firstNewUpdatePanel.ID = "runFirstUpdatePanel";
firstNewUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
Button firstNewButton = new Button();
firstNewButton.ID = "runFirstButton";
firstNewButton.ClientIDMode = ClientIDMode.Static;
firstNewButton.Text = "Run btn 2";
firstNewButton.Click += new EventHandler(firstNewButton_Click);
firstNewUpdatePanel.ContentTemplateContainer.Controls.Add(firstNewButton);
AsyncPostBackTrigger firstNewTrigger = new AsyncPostBackTrigger();
firstNewTrigger.ControlID = "runFirstButton";
firstNewTrigger.EventName = "Click";
firstNewUpdatePanel.Triggers.Add(firstNewTrigger);
firstNewTabPanel.Controls.Add(firstNewUpdatePanel);
TabContainer2.Tabs.Add(firstNewTabPanel);
TabContainer2.ActiveTabIndex = 0;
}
void firstNewButton_Click(object sender, EventArgs e)
{
rtxt1.Text = "You have clicked at Runtime Button 2: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
}
}
}