Asp.net动态生成表单
            时间:2014-05-16 08:36:25  
            收藏:0  
            阅读:362
        
        
        control.ascx
| 1 2 3 4 | <%@ Control Language="C#"AutoEventWireup="true"CodeBehind="control.ascx.cs"Inherits="WebApplication1.control"%>  <asp:Panel ID="Panel1"runat="server"></asp:Panel>  <input id="Submit1"type="submit"value="submit"/> | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespaceWebApplication1{    publicpartialclasscontrol : System.Web.UI.UserControl    {        protectedvoidPage_Load(objectsender, EventArgs e)        {            GetFormData();                AddFormTable();                                }        privatevoidAddFormTable()        {            Table tb = newTable();            for(inti = 0; i < 10; i++)            {                TableRow tr = newTableRow();                TableCell tc1 = newTableCell();                TextBox txtbox = newTextBox();                txtbox.ID = "AutoPageControl_"+ i.ToString();                tc1.Controls.Add(txtbox);                tr.Cells.Add(tc1);                tb.Rows.Add(tr);            }            Panel1.Controls.Add(tb);        }        publicvoidGetFormData()        {            string[] names = Request.Form.AllKeys;            List<string> lst = newList<string>();            foreach(varitem innames)            {                if(item.Contains("control"))                {                    lst.Add(item);                }            }            //遍历发送的key值            for(inti = 0; i < lst.Count; i++)            {                string[] arr = lst[i].Split(‘_‘);                stringattribute = arr[arr.Length - 1];                //@TODO获取数据库中数据模型,判断是否必填项和是否是确认项                stringvalue = Request[lst[i]];                //@TODO增加数据到数据库            }                   }    }} | 
页面调用
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%@ Page Language="C#"AutoEventWireup="true"CodeBehind="AutoPage.aspx.cs"Inherits="WebApplication1.AutoPage"%><%@ Register Src="~/control.ascx"TagPrefix="uc1"TagName="control"%><!DOCTYPE html><head runat="server"><meta http-equiv="Content-Type"content="text/html; charset=utf-8"/>    <title></title></head><body>    <form id="form1"runat="server">        <uc1:control runat="server"id="control"/>    </form></body></html> | 
            评论(0)
        
        
        