【发布时间】:2016-09-05 17:50:11
【问题描述】:
我在gridview中动态添加了n个文本框并将数据绑定到它们。如果用户更改任何文本框中的值,我想要函数/方法后面的代码对 textboxtotal 中的 textbox1、textbox2 .....textboxn 求和。我无法使用这些文本框添加回发,因为在回发文本框被处理之后。
动态添加列到 Gridview 的代码
foreach (string a in crcl)
{
SqlCommand cmd1 = new SqlCommand("select qty from purchaseinstruction where project ='" + DropDownList1.SelectedItem.ToString() + "' AND circle = '" + a + "' AND item = 'BIFURCATION' AND material = '" + mat + "'", agr);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
string val = dr1[0].ToString();
if (val.Length > 0)
{
row[a] = val;
value = value + decimal.Parse(val);
}
else
{
row[a] = 0;
}
}
else
{
row[a] = 0;
}
dr1.Dispose();
row["TOTAL"] = value;
}
dt.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
在 rowbound 上添加 Textbox 控件:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = 3;
if (e.Row.RowType == DataControlRowType.DataRow)
{
crcl = (List<string>)ViewState["bdi2"];
foreach(string a in crcl)
{
TextBox TextBox101 = new TextBox();
TextBox101.ID=a;
TextBox101.Width = 60;
TextBox101.Text = (e.Row.DataItem as DataRowView).Row[a].ToString();
e.Row.Cells[i].Controls.Add(TextBox101);
i++;
}
TextBox TextBox102 = new TextBox();
TextBox102.ID = "TOTAL";
TextBox102.Width = 60;
TextBox102.Text = (e.Row.DataItem as DataRowView).Row["TOTAL"].ToString();
e.Row.Cells[i].Controls.Add(TextBox102);
}
}
【问题讨论】: