动态创建的按钮不会重新设置标签的值

时间:2013-11-25 20:48:58

标签: c# asp.net button dynamic-controls

我编写了以下代码,动态地在页面上创建一个按钮,一旦用户点击它,就会设置Label的文本值。通过在Dynamiclbutton_Click中放置断点,我可以看到正在调用click函数并且正在设置标签文本,但是在页面上没有为Label设置值。

我尝试通过拖放工具箱中的按钮并双击它来创建由VS 2010创建的onlick函数,并且当用户单击该声明Button时页面正在获取刷新并显示Label的新值。

我需要为动态Button做一些额外的事情,让它表现得像一个声明的行为?我需要用户可以点击动态按钮,标签获得新值。缺少什么?

public partial class CPIAbstracting : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {

        placeHolder2.Controls.Add(new LiteralControl("</br>"));
        Button DynamicButton = new Button();
        DynamicButton.Text = "test";
        DynamicButton.Click += new EventHandler(Dynamiclbutton_Click);
        placeHolder2.Controls.Add(DynamicButton);        
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Dynamiclbutton_Click(object sender, EventArgs e)
    {
        Label1.Text = "Dynamic button is clicked";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Static button is clicked";
    }
}

ASPX:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="CPIAbstracting.aspx.cs" Inherits="MED2020.WinRecs.Web.Sandbox.CPIAbstracting" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
          .accordion1 {
        }  

        .accordionHeader1 {  
            border: 1px solid #2F4F4F;  
            color: white;  
            background-color: #2E4d7B;  
            font-family: Arial, Sans-Serif;  
            font-size: 14px;  
            font-weight: bold;  
            padding: 5px;  
            margin-top: 5px;  
            cursor: pointer;  
        }  

        .accordionHeaderSelected1 {  
            border: 1px solid #2F4F4F;  
            color: white;  
            background-color: #5078B3;  
            font-family: Arial, Sans-Serif;  
            font-size: 12px;  
            font-weight: bold;  
            padding: 5px;  
            margin-top: 5px;  
            cursor: pointer;  
        }  

        .accordionContent1 {  
            background-color: #D3DEEF;  
            border: 1px dashed #2F4F4F;  
            border-top: none;  
            padding: 5px;  
            padding-top: 10px;  
             font-family: Arial, Sans-Serif;  
            font-size: 10px;  
            font-weight: bold;  
        }  
        .ApplicationDefault {font-family: Verdana; font-size: 10px;}
        .Title {text-align: center; font-size: 12px;  font-family: 
                        Verdana; font-weight: bold;}
        .AuthorInformation {text-align: center; color:green; margin-top: 5px}
        .MainContent { 
text-align: center;font-family: Verdana;  font-size: small;

          }
        .Copyright {margin-top: 10px; color: Gray; font-weight:bold; width: 100%; 
                            float: left; text-align: center;}
        .ErrorText {font-family: Verdana; font-weight: bold; color:Maroon;}
        .BoldText {font-family: Verdana; font-weight: bold;}
          .style1
          {
              width: 100%;
          }
          .style3
          {
              width: 240px;
          }
          .style4
          {
              width: 882px;
          }
          .style10
          {
              height: 168px;
          }
          .style11
          {
              width: 882px;
              height: 168px;
          }
          </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ApplicationContent" runat="server">

    <br />
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:Accordion ID="Accordion1" CssClass="accordion1"  
        HeaderCssClass="accordionHeader1"   
        HeaderSelectedCssClass="accordionHeaderSelected1"  ContentCssClass="accordionContent1"   
    runat="server" Height="232px" Width="402px"> </asp:Accordion>
     <br />
    <br />





<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

</ContentTemplate>

</asp:UpdatePanel>


<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="False" 
    UpdateMode="Conditional">
<ContentTemplate>

<asp:PlaceHolder id="placeHolder2" runat="server"></asp:PlaceHolder>
</ContentTemplate>

</asp:UpdatePanel>


<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">

<ContentTemplate>
<asp:PlaceHolder id="ph3" runat="server"></asp:PlaceHolder>
</ContentTemplate>

</asp:UpdatePanel>



    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>

1 个答案:

答案 0 :(得分:0)

问题是您的动态添加按钮位于UpdatePanel内,而Label位于其外部。我不知道你的场景是什么,但是你需要在UpdatePanel中有两个控件并正确设置触发器。

请参阅此相关问题:

Change text in TextBox outside of UpdatePanel