如何动态生成服务器控件!


Posted on April 28, 2007


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
    <form runat=server>
         <asp:Table id="Table1" runat="server"    CellPadding=10     GridLines="Both"    HorizontalAlign="Center">
 </asp:Table>
         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <br />
    </form>
</body>
</html>

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 4; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < 4; j++)
            {
                TableCell tc = new TableCell();
                Button bt = new Button();
                bt.Width = 50;
                bt.Text = Convert.ToString(i * 4 + j + 1);
                bt.CommandArgument = bt.Text;
                bt.Click += new EventHandler(this.GreetingBtn_Click);
                tc.Controls.Add(bt);
                tr.Controls.Add(tc);
            }
            this.Table1.Controls.Add(tr);
        }
    }
    void GreetingBtn_Click(Object sender,EventArgs e)
    {
        this.Label1.Text = ((Button)sender).CommandArgument.ToString();
    }
}

标签:N/A

 

在线学习答案查询入口
微信扫码联系
微信扫码联系