<%@ 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"] = 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();
}