asp.net 动态管理(增加、删除、读取)控件

呵呵,废话少说了,上代码。

一、页面代码

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button3" runat="server" Text="赋值" OnClick="Button3_Click" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="取值" />
        <table width="280px">
            <tr>
                <td>
                    <asp:Panel ID="Panel1" runat="server">
                    </asp:Panel>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

 

二、后台代码

 

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["num"] = 3;
        }

        LoadCtrl(int.Parse(ViewState["num"].ToString()));
    }

    /// <summary>
    /// 设置值
    /// </summary>
    private void SetTxtValue()
    {
        ViewState["num"] = 5;
        LoadCtrl(int.Parse(ViewState["num"].ToString()));

        for (int i = 0; i < 5; i++)
        {
            TextBox txt = Panel1.FindControl("txt" + i.ToString()) as TextBox;
            txt.Text = i.ToString();
        }
    }

    /// <summary>
    /// 加载控件
    /// </summary>
    /// <param name="num"></param>
    private void LoadCtrl(int num)
    {
        Panel1.Controls.Clear();

        for (int i = 0; i < num; i++)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt" + i.ToString();
            txt.Text = Request.Form[txt.ClientID];

            LinkButton lbtn = new LinkButton();
            lbtn.ID = "lbtn" + i.ToString();
            lbtn.CommandArgument = i.ToString();
            lbtn.Text = "删除";
            lbtn.Click += new EventHandler(btnDel_Click);

            Literal ltl = new Literal();
            ltl.ID = "ltl" + i.ToString();
            ltl.Text = "<br/>";

            Panel1.Controls.Add(txt);
            Panel1.Controls.Add(lbtn);
            Panel1.Controls.Add(ltl);
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = sender as LinkButton;
        string id = lbtn.CommandArgument;
        TextBox txt = Panel1.FindControl("txt" + id.ToString()) as TextBox;
        Literal ltl = Panel1.FindControl("ltl" + id.ToString()) as Literal;

        Panel1.Controls.Remove(txt);
        Panel1.Controls.Remove(lbtn);
        Panel1.Controls.Remove(ltl);
        ViewState["num"] = int.Parse(ViewState["num"].ToString()) - 1;

        SetControlsID();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["num"] = int.Parse(ViewState["num"].ToString()) + 1;
        LoadCtrl(int.Parse(ViewState["num"].ToString()));
    }

    /// <summary>
    /// 取值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Text = string.Empty;

        for (int i = 0; i < (int)ViewState["num"]; i++)
        {
            Label1.Text += ((TextBox)(Panel1.FindControl("txt" + i.ToString()))).Text;
        }
    }

    /// <summary>
    /// 重新设置控件ID
    /// </summary>
    private void SetControlsID()
    {
        int i = 0;

        foreach (Control ctl in Panel1.Controls)
        {
            if (ctl.GetType().Name.ToLower() == "textbox")
            {
                ctl.ID = "txt" + i.ToString();
            }
            if (ctl.GetType().Name.ToLower() == "linkbutton")
            {
                ctl.ID = "lbtn" + i.ToString();
            }
            if (ctl.GetType().Name.ToLower() == "literal")
            {
                ctl.ID = "ltl" + i.ToString();

                i++;
            }
        }
    }

    /// <summary>
    /// 赋值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
        SetTxtValue();

    }
}

 

posted @ 2010-08-21 22:12  andy1016  阅读(633)  评论(0编辑  收藏  举报