第二次动态产生铵钮的Click事件

网页在第一次运行时,它动态产生了一个铵钮,此称为第一个铵钮,当用户用mouse点击这个铵钮时,会动态产生另一个铵钮,此称为第二个铵钮,当用户再用mouse点击第二个铵钮时,系统将处理另外的事情。此博文是实再第二个铵钮的Click事件。首先看看效果:

 

.aspx:

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
</head>
<body>
    <form id="form1" runat="server">   
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <fieldset>
                    <legend>网页运行时动态产生的Button</legend>
                    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </fieldset>
                <fieldset>
                    <legend>用Mouse点击第一个Button产生的Button</legend>
                    <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
                </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>   
    </form>
</body>
</html>

 

.apsx.cs:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button Button1 = new Button();
        Button1.Text = "I was the first button";
        Button1.Click += Button1_Click;
        this.PlaceHolder1.Controls.Add(Button1);

        if (ViewState["Click"] != null && (bool)ViewState["Click"])
            GenerateButtonControl();
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["Click"] == null)
        {
            ViewState["Click"] = true;
            GenerateButtonControl();
        }
    }

    private void GenerateButtonControl()
    {
        Button Button2 = new Button();
        Button2.Text = "I was the second button";
        Button2.Click += new EventHandler(Button2_Click);
        this.PlaceHolder2.Controls.Add(Button2);
    }

    private void Button2_Click(object sender, EventArgs e)
    {
        InsusJavascriptUtility objJs = new InsusJavascriptUtility();
        objJs.JsAlert("Hi, Good evening!");     
    }
}


 

posted @ 2012-10-09 22:02 Insus.NET 阅读(...) 评论(...) 编辑 收藏