前台页面:
<asp:Table ID="tabPriceList" runat="server"></asp:Table>//显示控件的表格
//添加服务器控件
<asp:TextBox ID="txtTabNum" runat="server" Width="30px">1</asp:TextBox>
<asp:LinkButton ID="lnkbtnAdd" runat="server" onclick="lnkbtnAdd_Click">设置</asp:LinkButton>
<span class="gray">输入1-9之间的数字</span>
后台页面:
//重建控件
public bool IsDynamicLoadControl
{
get
{
object addControl = ViewState["IsDynamicLoadControl"];
return addControl == null ? false : true;
}
set
{
ViewState["IsDynamicLoadControl"] = value;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (IsDynamicLoadControl)
{
int rowNum = 1;
if(ViewState["LoadControlNum"] != null)
{
rowNum = Convert.ToInt32(ViewState["LoadControlNum"]);
}
LoadAddControl(rowNum);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
LoadAddControl(3); //初始化服务器控件
}
}
//动态添加控件
protected void lnkbtnAdd_Click(object sender, EventArgs e)
{
int rowNum = 1;
string trNum = this.txtTabNum.Text.Trim();
string expression = @"^[1-9]{1}$";
Regex reg = new Regex(expression);
if (reg.IsMatch(trNum))
{
rowNum = Convert.ToInt32(trNum);
LoadAddControl(rowNum);
}
else
{
this.txtTabNum.Text = (this.tabPriceList.Rows.Count - 1).ToString();
PopupMsg("请输入1-9之间的数字");
}
}
private void LoadAddControl(int rowNum)
{
this.tabPriceList.Rows.Clear();
//添加头部
TableRow htr = new TableRow();
TableCell htd1 = new TableCell();
htd1.Text = "机票价格";
htd1.Height = 30;
TableCell htd2 = new TableCell();
htd2.Text = "有效期开始";
TableCell htd3 = new TableCell();
htd3.Text = "有效期结束";
TableCell htd4 = new TableCell();
htd4.Text = "附送保险";
TableCell htd5 = new TableCell();
htd5.Text = "机票舱位";
TableCell htd6 = new TableCell();
htd6.Text = "燃油附加税";
TableCell htd7 = new TableCell();
htd7.Text = "机场建设费";
TableCell htd8 = new TableCell();
htd8.Text = "退改签规定";
htd8.ColumnSpan=2;
htr.Cells.Add(htd1);
htr.Cells.Add(htd2);
htr.Cells.Add(htd3);
htr.Cells.Add(htd4);
htr.Cells.Add(htd5);
htr.Cells.Add(htd6);
htr.Cells.Add(htd7);
htr.Cells.Add(htd8);
this.tabPriceList.Rows.Add(htr);
//添加主体
for (int i = 1; i <= rowNum; i++)
{
TableRow tr = new TableRow();
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();
TableCell td3 = new TableCell();
TableCell td4 = new TableCell();
TableCell td5 = new TableCell();
TableCell td6 = new TableCell();
TableCell td7 = new TableCell();
TableCell td8 = new TableCell();
TableCell td9 = new TableCell();
//第一列:价格
TextBox txtPrice = new TextBox();
txtPrice.ID = "txtPrice" + i;
txtPrice.Width = 70;
td1.Controls.Add(txtPrice);
//第二列:有效期开始时间
TextBox txtDateStart = new TextBox();
txtDateStart.ID = "txtDateStart" + i;
txtDateStart.Width = 100;
txtDateStart.ToolTip = "单击选择日期";
txtDateStart.Attributes["onFocus"] = "WdatePicker({skin:'ext',minDate:'%y-%M-{%d}',maxDate:'#F{$dp.$D(http://www.cnblogs.com/zxjyuan/admin/file://'ctl00_AC_Content_txtDateEnd/" + i + "http://www.cnblogs.com/zxjyuan/admin/file://')||//'2020-10-01//'}'})";
if (i == 1)
{
txtDateStart.Text = DateTime.Today.ToString("yyyy-MM-dd");
}
td2.Controls.Add(txtDateStart);
//第三列:有效期结束时间
TextBox txtDateEnd = new TextBox();
txtDateEnd.ID = "txtDateEnd" + i;
txtDateEnd.Width = 100;
txtDateEnd.ToolTip = "单击选择日期";
txtDateEnd.Attributes["onFocus"] = "WdatePicker({skin:'ext',minDate:'#F{$dp.$D(/ /'ctl00_AC_Content_txtDateStart/" + i + "'%y-%M-{%d} / /'}',maxDate:'2020-10-01'" | |//'%y-%M-{%d}//'}',maxDate:'2020-10-01'})";
td3.Controls.Add(txtDateEnd);
//第四列:赠送保险
TextBox txtSafeAmt = new TextBox();
txtSafeAmt.ID = "txtSafeAmt" + i;
txtSafeAmt.Text = "0";
txtSafeAmt.Width = 70;
td4.Controls.Add(txtSafeAmt);
//第五列:机票舱位
DropDownList ddlFlySpace = new DropDownList();
ddlFlySpace.ID = "ddlFlySpace" + i;
ddlFlySpace.DataSource = BLLDict.GetDictList(1);
ddlFlySpace.DataTextField = "SysName";
ddlFlySpace.DataValueField = "DictID";
ddlFlySpace.DataBind();
td5.Controls.Add(ddlFlySpace);
//第六列:燃油税
TextBox txtOilTax = new TextBox();
txtOilTax.ID = "txtOilTax" + i;
txtOilTax.Text = "0";
txtOilTax.Width = 90;
td6.Controls.Add(txtOilTax);
//第七列:机场建设费
TextBox txtBuildTax = new TextBox();
txtBuildTax.ID = "txtBuildTax" + i;
txtBuildTax.Text = "50";
txtBuildTax.Width = 90;
td7.Controls.Add(txtBuildTax);
//第八列:退改签规定
td8.Text = " <span id=\"addFareRule" + i + "\" class=\"addFareRule\">填写规定</span>";
TextBox txtFareRule = new TextBox();
txtFareRule.ID = "txtFareRule" + i;
txtFareRule.Text = "0";
txtFareRule.TextMode = TextBoxMode.MultiLine;
txtFareRule.Width = 90;
txtFareRule.Style.Add("display", "none");
td9.Controls.Add(txtFareRule);
tr.Cells.Add(td1);
tr.Cells.Add(td2);
tr.Cells.Add(td3);
tr.Cells.Add(td4);
tr.Cells.Add(td5);
tr.Cells.Add(td6);
tr.Cells.Add(td7);
tr.Cells.Add(td8);
tr.Cells.Add(td9);
this.tabPriceList.Rows.Add(tr);
}
ViewState["LoadControlNum"] = rowNum;
IsDynamicLoadControl = true;
}
获得动态控件的值:
//验证并构建机票datatable
string txtDateStart = string.Empty;
string txtDateEnd = string.Empty;
string txtPrice = string.Empty;
DataTable dateTable = new DataTable("dateTable");
dateTable.Columns.Add("ID", typeof(int));
dateTable.Columns.Add("DateStart", typeof(string));
dateTable.Columns.Add("DateEnd", typeof(string));
dateTable.Columns.Add("Price", typeof(string));
int tabNum = this.tabPriceList.Rows.Count;
Regex regular = new Regex(@"^\d+(\.\d{1,2})?$");
for (int t = 1; t < tabNum; t++)
{
//获得动态控件中的值
txtDateStart = ((TextBox)(this.tabPriceList.Rows[t].FindControl("txtDateStart" + t))).Text.Trim();
txtDateEnd = ((TextBox)(this.tabPriceList.Rows[t].FindControl("txtDateEnd" + t))).Text.Trim();
txtPrice = ((TextBox)(this.tabPriceList.Rows[t].FindControl("txtPrice" + t))).Text.Trim();
if (string.IsNullOrEmpty(txtDateStart) || string.IsNullOrEmpty(txtDateEnd) || !regular.IsMatch(txtPrice))
{
PopupMsg("请选择有效期,机票价格为数字");
return;
}
else
{
dateTable.Rows.Add(t, txtDateStart, txtDateEnd, txtPrice);
}
}