C#动态创建控件之事件响应(刚刚答复提问,正好一并录之)

ASP:前台添加PlaceHolder

<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        欢迎使用 ASP.NET!
    </h2>
    <p>
        若要了解关于 ASP.NET 的详细信息,请访问 <a href="http://www.asp.net/cn" title="ASP.NET 网站">www.asp.net/cn</a>。
    </p>
    <p>
        您还可以找到 <a href="http://go.microsoft.com/fwlink/?LinkID=152368"
            title="MSDN ASP.NET 文档">MSDN 上有关 ASP.NET 的文档</a>。
    </p>
    
     <asp:PlaceHolder ID="PlaceHolder1" runat="server"><p></p></asp:PlaceHolder>
</asp:Content>

后台代码:

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

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loopshowCourse();
        }
        protected void loopshowCourse()
        {
                    Button mybutton = new Button();
                    mybutton.Text = "More";
                    mybutton.Click += new EventHandler(mybutton_Click);

                    PlaceHolder1.Controls.Add(mybutton);
        }
        protected void mybutton_Click(object sender, EventArgs e)
        {
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "tixin", "alert('该文件不存在或路径错误!')", true);
        }
    }
}

WindowsForms的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsForms测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button mybutton = new Button();
            mybutton.Text = "More";
            mybutton.Click += new EventHandler(mybutton_Click);
            mybutton.Parent = this;
        }
        protected void mybutton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        }
    }


        Form 升起提示框 = new Form();
        升起提示框.FormClosed += new FormClosedEventHandler(升起提示框_关闭);
        void 升起提示框_关闭(object sender, EventArgs e)
        {
            MessageBox.Show("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值