【问题标题】:Creating dynamic button and its event handler创建动态按钮及其事件处理程序
【发布时间】:2015-03-02 11:50:48
【问题描述】:

我正在尝试使用动态按钮和事件。当我单击静态按钮并显示动态按钮时。但是当我单击动态按钮时,我没有处理 dinamikButon_Click 事件。我怎么了?对不起我的语言。提前谢谢。

Default.aspx.cs 如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebApplication
{
public partial class _Default : Page
{
    int i = 1;
    Button dinamikButon;
    protected void Page_Load( object sender, EventArgs e )
    {

    }

    protected void btnStatik_Click( object sender, EventArgs e )
    {
        dinamikButon = new Button
        {
            Text = "Dinamik" + i,
            ID = "btnDinamik" + i,
            CommandArgument = "commandArgument",
            CommandName = "commandName"

        };
        dinamikButon.Click += dinamikButon_Click;
        panel1.Controls.Add( dinamikButon );
        i++;
    }

    void dinamikButon_Click( object sender, EventArgs e )
    {
        Label1.Text = "Merhaba dinamik butondan geliyorum.";
    }

}
}

【问题讨论】:

    标签: asp.net dynamic eventhandler


    【解决方案1】:

    那是因为当页面回发时按钮不存在。您必须在页面 Load 或 PreInit 上创建按钮。微软建议PreInitYou can dynamically set a master page or a theme for the requested page, and create dynamic controls.

        int i = 1;
        Button dinamikButon;
        private void Page_PreInit(object sender, EventArgs e)
        {
            if(Page.IsPostBack)
            {
                CreateButton();
            }
        }
    
        protected void btnStatik_Click( object sender, EventArgs e )
        {
            CreateButton();
        }
    
        private void CreateButton()
        {
            dinamikButon = new Button
            {
                Text = "Dinamik" + i,
                ID = "btnDinamik" + i,
                CommandArgument = "commandArgument",
                CommandName = "commandName"
    
            };
            dinamikButon.Click += dinamikButon_Click;
            panel1.Controls.Add( dinamikButon );
            i++;
        }
    

    更新:

    现在按照你的要求去做,我们必须指定按钮是使用视图状态、查询字符串或会话创建的。

    在本例中,我将使用会话:

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Page.IsPostBack)
            {
                if (Session["Created"] != null)
                {
                    CreateButton();
                }
            }
        }
    
        private void CreateButton()
        {
            dinamikButon = new Button
            {
                Text = "Dinamik" + i,
                ID = "btnDinamik" + i,
                CommandArgument = "commandArgument",
                CommandName = "commandName"
    
            };
    
            Panel1.Controls.Add(dinamikButon);
            dinamikButon.Click += dinamikButon_Click;
            i++;
            Session["Created"] = "true";
        }
    
        private void dinamikButon_Click(object sender, EventArgs e)
        {
            //your action here
        }
    

    【讨论】:

    • 首先谢谢,但它不能正常工作。因为当我运行程序时,屏幕上会显示静态和动态按钮。然后当我点击静态按钮时,它会创建并显示其他动态按钮。
    • 感谢@prospector。很快,我会试试这个。
    【解决方案2】:

    使用动态按钮在“Label1”控件中填充值

    默认.aspx

                    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicCtrl._Default" %>
    
                    <!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>
    
                        <script type="text/javascript" language="javascript">
                            function dynamicevnt() {
                                document.getElementById("Label1").innerHTML = "Merhaba dinamik butondan geliyorum.";
                                return false;
                            }
                        </script>
    
                    </head>
                    <body>
                        <form id="form1" runat="server">
                        <div>
                            <asp:Button ID="btnStatik" runat="server" Text="Click" OnClick="btnStatik_Click" />
                            <asp:Label ID="Label1" runat="server"></asp:Label>
                            <asp:Panel ID="panel1" runat="server">
                            </asp:Panel>
    
                        </div>
                        </form>
                    </body>
                    </html>
    

    Default.aspx.cs

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Web;
                using System.Web.UI;
                using System.Web.UI.WebControls;
    
                namespace DynamicCtrl
                {
                    public partial class _Default : System.Web.UI.Page
                    {
    
                        protected void Page_Load(object sender, EventArgs e)
                        {
    
                        }
                        protected void btnStatik_Click(object sender, EventArgs e)
                        {
                            CreateButton();
                        }
                        private void CreateButton()
                        {
                            int i = 1;
                            Button dinamikButon = new Button();
                            dinamikButon.Text = "Dinamik" + i;
                            dinamikButon.ID = "btnDinamik" + i;
                            dinamikButon.OnClientClick = "return dynamicevnt();";
                            dinamikButon.Click += new EventHandler(dinamikButon_Click);
                            panel1.Controls.Add(dinamikButon);
                            i++;
                        }
                        protected void dinamikButon_Click(object sender, EventArgs e)
                        {
                            Label1.Text = "Merhaba dinamik butondan geliyorum.";
                        }
                    }
                }
    

    【讨论】:

    • 我知道如何使用 javascript。但是我一定不能使用js。谢谢。
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多