动态生成表单,点击按钮添加一行,行里面包括了绑定数据的ddl。
找了些例子不是很理想,这个功能代码不用js的话可以在后台实现吗?
例图:如果表单的生产可以在前台实现也可以,唯一需要提交的是ddl里面的数据,所以要求ddl是服务器端的控件。
找了些例子不是很理想,这个功能代码不用js的话可以在后台实现吗?
例图:如果表单的生产可以在前台实现也可以,唯一需要提交的是ddl里面的数据,所以要求ddl是服务器端的控件。
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" runat="server">
<asp:TableRow>
<asp:TableCell></asp:TableCell>
<asp:TableCell Text="Title1"></asp:TableCell>
<asp:TableCell>
<asp:Button runat="server" ID="Button2" Text="ADD"></asp:Button>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Button Button2;
private static int iCount=0;
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
AddControls();
}
}
private void AddControls()
{
for (int i=0;i<iCount;i++)
{
RadioButton rb = new RadioButton();
rb.GroupName="1";
DropDownList ddl = new DropDownList();
ddl.Items.Add(new ListItem(i.ToString(),i.ToString()));
Button bt = new Button();
bt.ID = "Button_"+i.ToString();
bt.Text = "Delete";
bt.Click += new System.EventHandler(this.ButtonX_Click);//注册事件
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
tc1.BorderWidth=1;
tc2.BorderWidth=1;
tc3.BorderWidth=1;
tc1.Controls.Add(rb);
tc2.Controls.Add(ddl);
tc3.Controls.Add(bt);
TableRow tr = new TableRow();
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
Table1.Rows.Add(tr);
}
} private void ButtonX_Click(object sender, System.EventArgs e)
{
int index = Convert.ToInt32((sender as Button).ID.Substring(7,(sender as Button).ID.Length-7));
Table1.Rows.RemoveAt(index);
iCount--;//计数器减1;
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion private void Button2_Click(object sender, System.EventArgs e)
{
iCount++;
}
}
}