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"] = 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();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:gridview id="gridview_list" runat="server" showfooter="true" autogeneratecolumns="false"
onrowdatabound="gridview_list_rowdatabound">
<emptydatatemplate>
<table style="border-collapse: collapse; width: 100%" cellspacing="0" rules="all"
border="1">
<tr>
<th scope="col">
选择1
</th>
<th scope="col">
选择2
</th>
<th scope="col">
输入文字
</th>
</tr>
<tr><td>
<asp:dropdownlist id="x1" runat="server">
<asp:listitem>l0</asp:listitem>
<asp:listitem>l1</asp:listitem>
<asp:listitem>l2</asp:listitem>
<asp:listitem>l3</asp:listitem>
</asp:dropdownlist>
</td><td>
<asp:dropdownlist id="x2" runat="server">
<asp:listitem>10#</asp:listitem>
<asp:listitem>20#</asp:listitem>
<asp:listitem>30#</asp:listitem>
<asp:listitem>40#</asp:listitem>
</asp:dropdownlist>
</td><td>
<asp:textbox id="x3" runat="server"></asp:textbox>
</td></tr>
</table>
</emptydatatemplate>
<columns>
<asp:templatefield headertext="选择1">
<itemtemplate>
<asp:dropdownlist id="x1" runat="server" autopostback="true" onselectedindexchanged="x1_selectedindexchanged">
<asp:listitem>l0</asp:listitem>
<asp:listitem>l1</asp:listitem>
<asp:listitem>l2</asp:listitem>
<asp:listitem>l3</asp:listitem>
</asp:dropdownlist>
</itemtemplate>
<footertemplate>
<asp:dropdownlist id="x1" runat="server">
<asp:listitem>l0</asp:listitem>
<asp:listitem>l1</asp:listitem>
<asp:listitem>l2</asp:listitem>
<asp:listitem>l3</asp:listitem>
</asp:dropdownlist>
</footertemplate>
</asp:templatefield>
<asp:templatefield headertext="选择2">
<itemtemplate>
<asp:dropdownlist id="x2" runat="server" autopostback="true" onselectedindexchanged="x2_selectedindexchanged">
<asp:listitem>10#</asp:listitem>
<asp:listitem>20#</asp:listitem>
<asp:listitem>30#</asp:listitem>
<asp:listitem>40#</asp:listitem>
</asp:dropdownlist>
</itemtemplate>
<footertemplate>
<asp:dropdownlist id="x2" runat="server">
<asp:listitem>10#</asp:listitem>
<asp:listitem>20#</asp:listitem>
<asp:listitem>30#</asp:listitem>
<asp:listitem>40#</asp:listitem>
</asp:dropdownlist>
</footertemplate>
</asp:templatefield>
<asp:templatefield headertext="输入文字">
<itemtemplate>
<asp:textbox id="x3" runat="server" autopostback="true" ontextchanged="x3_textchanged"></asp:textbox>
</itemtemplate>
<footertemplate>
<asp:textbox id="x3" runat="server"></asp:textbox>
</footertemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<asp:linkbutton id="linkbutton1" runat="server" text="添加内容" onclick="linkbutton1_click"></asp:linkbutton>
<asp:linkbutton id="linkbutton2" runat="server" text="删除内容" onclick="linkbutton2_click"></asp:linkbutton>
</form>
</body>
</html>
上一篇: Python入门_条件控制(详解)
下一篇: Android下的EXIF是什么