您的位置:首页 > 编程语言 > ASP

asp.net小程序练习--自动生成指定的文本框数

2014-10-31 15:25 295 查看
.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="创建文本框.aspx.cs" Inherits="创建文本框" %>

<!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">
    <div>
    <p>请输入要创建的文本框数:
        <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
        <asp:Button ID="btn1"
            runat="server" Text="生成文本框" onclick="btn1_Click" /></p>
    
    
    </div>
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    </form>
</body>
</html>


.aspx.cs代码如下:

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

public partial class 创建文本框 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        int i = 0;
        try
        {
            i = Int16.Parse(txt1.Text);
        }
        catch { };
        TextBox txt;
        for (int _i = 0; _i < i; _i++) {
            txt = new TextBox();
            Panel1.Controls.Add(txt);
            txt.Text = (_i+1).ToString();

        }
    }
}
运行效果如图所示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐