net动态控件按保存时取值问题
编辑功能: net 在后台动态添加控件,然后把给控件赋值,呈现到界面,然后修改控件的值,按保存。这个时候,得不到控件修改后的值,总是最开始赋值时候的值。这要怎么做呢? 动态控件的id 可以得到,查看源文件时,id的值都是最初赋值时候的值。 为什么点保存的时候,得不到修改后的值呢?
TextBox txtXuhao { get; set; }
DropDownList dptea { get; set; }
TextBox txtBegin { get; set; }
TextBox txtEnd { get; set; }
TextBox txtCode { get; set; }
//动态生成控件
public void CreateControl()
{
if (ViewState["CreateControl"] == null) return;
int claId = Convert.ToInt32(Request.QueryString["claID"]);
DataSet dsDeta = tb.ShowClassDetail(claId);
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
Label la = new Label();
la.Text = "班次序号";
la.Width = 82;
Label la2 = new Label();
la2.Text = "老师";
la2.Width = 125;
Label la3 = new Label();
la3.Text = "上课开始时间";
la3.Width = 210;
Label la4 = new Label();
la4.Text = "下课结束时间";
la4.Width = 210;
Label la5 = new Label();
la5.Text = "课程代码";
la5.Width = 200;
cell1.Controls.Add(la);
cell1.Controls.Add(la2);
cell1.Controls.Add(la3);
cell1.Controls.Add(la4);
cell1.Controls.Add(la5);
row1.Cells.Add(cell1);
HolderTable.Rows.Add(row1);
for (int i = 0; i < dsDeta.Tables[0].Rows.Count; i++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
txtXuhao = new TextBox();
txtXuhao.Width = 78;
txtXuhao.ID = "txtXuhao" + i;
dptea = new DropDownList();
dptea.ID = "dptea" + i;
dptea.Width = 120;
dptea.DataSource = teab.showTeacherProFile();
dptea.DataTextField = "Teacher_Name";
dptea.DataValueField = "Teacher_ID";
dptea.DataBind();
txtBegin = new TextBox();
txtBegin.ID = "txtBegin" + i;
txtBegin.Width = 200;
txtEnd = new TextBox();
txtEnd.ID = "txtEnd" + i;
txtEnd.Width = 200;
txtCode = new TextBox();
txtCode.ID = "txtCode" + i;
txtCode.Width = 180;
cell.Controls.Add(txtXuhao);
cell.Controls.Add(dptea);
cell.Controls.Add(txtBegin);
cell.Controls.Add(txtEnd);
cell.Controls.Add(txtCode);
row.Cells.Add(cell);
HolderTable.Rows.Add(row);
txtXuhao.Text = dsDeta.Tables[0].Rows[i]["Class_Sequence"].ToString();
txtBegin.Text = dsDeta.Tables[0].Rows[i]["Class_Time_Begin"].ToString();
txtEnd.Text = dsDeta.Tables[0].Rows[i]["Class_Time_End"].ToString();
txtCode.Text = dsDeta.Tables[0].Rows[i]["Course_Code"].ToString();
}
}
static TextBox txXuhao;
static DropDownList dpte;
static TextBox txtBe;
static TextBox txten;
static TextBox txtco;
public void save()
{
CreateControl();
for (int i = 0; i < dsDeta.Tables[0].Rows.Count; i++)
{
txXuhao = this.form1.FindControl("txtXuhao"+i) as TextBox;
int a =Convert.ToInt32(txXuhao.Text.ToString());
dpte = this.form1.FindControl("dptea" + i) as DropDownList;
int b =Convert.ToInt32(dpte.SelectedValue);
txtBe = this.form1.FindControl("txtBegin"+i) as TextBox;
DateTime c = Convert.ToDateTime(txtBe.Text);
txten = this.form1.FindControl("txtEnd" + i) as TextBox;
DateTime d = Convert.ToDateTime(txten.Text);
txtco = this.form1.FindControl("txtCode" + i) as TextBox;
string e = txtco.Text;
}
}
[解决办法]
public void save()
{
CreateControl();
save方法 还调用 CreateControl?