这篇文章主要讲解了“viewstate和datatable动态录入数据的方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“viewstate和datatable动态录入数据的方法”吧!
创新互联公司是一家集网站建设,隆化企业网站建设,隆化品牌网站建设,网站定制,隆化网站建设报价,网络营销,网络优化,隆化网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。复制代码 代码如下:
<%@ Page Language="C#" EnableViewState="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"/tupian/20230522/<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 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>
感谢各位的阅读,以上就是“viewstate和datatable动态录入数据的方法”的内容了,经过本文的学习后,相信大家对viewstate和datatable动态录入数据的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!
本文标题:viewstate和datatable动态录入数据的方法-创新互联
标题来源:https://www.cdcxhl.com/article42/cepghc.html
成都网站建设公司_创新互联,为您提供用户体验、网站设计公司、虚拟主机、企业网站制作、网页设计公司、企业建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联