这篇文章主要介绍了viewstate和datatable动态录入数据示例,需要的朋友可以参考下
代码如下:<%@ Page Language="C#" EnableViewState="true" %><%@ Import Namespace="System.Data" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> 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"] =新闻热点
疑难解答