viewstate和datatable动态录入数据示例
本文导语: 代码如下: private DataTable stoveTable = null; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //创建 EmptyDataTemplate this.GridView_list.DataBind(); } } protected void GridView_list_RowDataBound(object sender...
private DataTable stoveTable = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//创建 EmptyDataTemplate
this.GridView_list.DataBind();
}
}
protected void GridView_list_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String usage = DataBinder.Eval(e.Row.DataItem, "usage").ToString();
String steelKind = DataBinder.Eval(e.Row.DataItem, "steelKind").ToString();
String castingTon = DataBinder.Eval(e.Row.DataItem, "castingTon").ToString();
DropDownList x1 = e.Row.FindControl("x1") as DropDownList;
DropDownList x2 = e.Row.FindControl("x2") as DropDownList;
TextBox x3 = e.Row.FindControl("x3") as TextBox;
x3.Text = castingTon;
ListItem xx1 = x1.Items.FindByValue(usage);
if (xx1 != null) xx1.Selected = true;
ListItem xx2 = x2.Items.FindByValue(steelKind);
if (xx2 != null) xx2.Selected = true;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
DropDownList x1, x2;
TextBox x3;
if (GridView_list.Rows.Count == 0)
{
x1 = GridView_list.Controls[0].Controls[0].FindControl("x1") as DropDownList;
x2 = GridView_list.Controls[0].Controls[0].FindControl("x2") as DropDownList;
x3 = GridView_list.Controls[0].Controls[0].FindControl("x3") as TextBox;
}
else
{
GridViewRow r = GridView_list.FooterRow;
x1 = r.FindControl("x1") as DropDownList;
x2 = r.FindControl("x2") as DropDownList;
x3 = r.FindControl("x3") as TextBox;
}
if (ViewState["dt"] == null)
{
stoveTable = new DataTable();
stoveTable.Columns.Add("usage", typeof(String));
stoveTable.Columns.Add("steelKind", typeof(String));
stoveTable.Columns.Add("castingTon", typeof(String));
}
else
{
stoveTable = (DataTable)ViewState["dt"];
}
DataRow newRow = stoveTable.NewRow();
newRow["usage"] = x1.SelectedValue;
newRow["steelKind"] = x2.SelectedValue;
newRow["castingTon"] = x3.Text;
stoveTable.Rows.Add(newRow);
ViewState["dt"] = stoveTable;
this.GridView_list.DataSource = stoveTable;
this.GridView_list.DataBind();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
if (ViewState["dt"] == null)
{
return;
}
stoveTable = (DataTable)ViewState["dt"];
if (stoveTable.Rows.Count < 1) return;
stoveTable.Rows.RemoveAt(stoveTable.Rows.Count - 1);
ViewState["dt"] = stoveTable;
this.GridView_list.DataSource = stoveTable;
this.GridView_list.DataBind();
}
protected void x1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList x1 = sender as DropDownList;
GridViewRow r = x1.Parent.Parent as GridViewRow;
if (ViewState["dt"] == null)
{
Response.Write("Error");
return;
}
stoveTable = (DataTable)ViewState["dt"];
stoveTable.Rows[r.RowIndex]["usage"] = x1.SelectedValue;
ViewState["dt"] = stoveTable;
this.GridView_list.DataSource = stoveTable;
this.GridView_list.DataBind();
}
protected void x2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList x2 = sender as DropDownList;
GridViewRow r = x2.Parent.Parent as GridViewRow;
if (ViewState["dt"] == null)
{
Response.Write("Error");
return;
}
stoveTable = (DataTable)ViewState["dt"];
stoveTable.Rows[r.RowIndex]["steelKind"] = x2.SelectedValue;
ViewState["dt"] = stoveTable;
this.GridView_list.DataSource = stoveTable;
this.GridView_list.DataBind();
}
protected void x3_TextChanged(object sender, EventArgs e)
{
TextBox x3 = sender as TextBox;
GridViewRow r = x3.Parent.Parent as GridViewRow;
if (ViewState["dt"] == null)
{
Response.Write("Error");
return;
}
stoveTable = (DataTable)ViewState["dt"];
stoveTable.Rows[r.RowIndex]["castingTon"] = x3.Text;
ViewState["dt"] = stoveTable;
this.GridView_list.DataSource = stoveTable;
this.GridView_list.DataBind();
}
选择1
选择2
输入文字
L0
L1
L2
L3
10#
20#
30#
40#
L0
L1
L2
L3
L0
L1
L2
L3
10#
20#
30#
40#
10#
20#
30#
40#
您可能感兴趣的文章:
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。