Вопрос

I wrote the following code to create a button on page dynamically and once the user click on it it set the text value of a Label. By putting a breakpoint inside the Dynamiclbutton_Click I can see that click function is being called and label text is being set, but on page no value is set for the Label.

I tried this by dragging and dropping a button from toolbox and double clicking on it for the onlick function to be created by VS 2010, and when the user click on that declarative Button the page is getting refreshed and the new value of Label is being shown.

What additional thing I need to do for dynamic Button for it to behave like a declarative one? I need the user can click on dynamic button and label gets new value. what is missing?

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>
Это было полезно?

Решение

The problem is that your dynamically added button is inside an UpdatePanel, while the Label is outside of it. I don't know what your scenario is, but you need to have both controls inside UpdatePanel(s) and set the triggers correctly.

See this related question:

Change text in TextBox outside of UpdatePanel

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top