Aps.Net Nested UpdatePanel's Button Click Event does not



我有一个动态创建的UpdatePanel内部TabContainers TabPanel。我还在新创建的UpdatePanel中创建了一个按钮。问题是,新按钮的click事件没有被触发。下面是一个例子。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");
        }
    }
}

可以改成ChildrenAsTriggers="true" RenderMode="Block"这句话

请点击下面的链接

这个链接将帮助你

我在创建程序时也遇到了同样的问题,我在按钮的点击事件上放置了改变的标签

在你的代码中试试这个片段,也可以把按钮放在updatepanel

之外

SET UseSubmitBehavior="False"的按钮试试这个,让我知道

相关内容

  • 没有找到相关文章

最新更新