欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net 2.0中一次性更新所有GRIDVIEW的记录】,下面是详细的分享!
asp.net 2.0中一次性更新所有GRIDVIEW的记录
<%@PageLanguage="C#"%>
<scriptrunat="server">
voidButton1_Click(objectsender,EventArgse)
{
for(inti=0;i<GridView1.Rows.Count;i++)
{
GridViewRowrow=GridView1.Rows[i];
SqlDataSource1.UpdateParameters[0].DefaultValue=((TextBox)row.Cells[0].FindControl("TextBox2")).Text;
SqlDataSource1.UpdateParameters[1].DefaultValue=((TextBox)row.Cells[1].FindControl("TextBox3")).Text;
SqlDataSource1.UpdateParameters[2].DefaultValue=GridView1.DataKeys[i].Value.ToString();
SqlDataSource1.Update();
}
}
</script>
<htmlxmlns="
<headrunat="server">
<title>UntitledPage</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"Runat="server"DataSourceID="SqlDataSource1"DataKeyNames="CustomerID" AutoGenerateColumns="False">
<Columns>
<asp:TemplateFieldSortExpression="CustomerID"HeaderText="CustomerID">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("CustomerID")%>'ID="TextBox1"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldSortExpression="CompanyName"HeaderText="CompanyName">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("CompanyName")%>'ID="TextBox2"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldSortExpression="ContactName"HeaderText="ContactTitle">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("ContactTitle")%>'ID="TextBox3"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSourceID="SqlDataSource1"Runat="server"
SelectCommand="SELECT[CustomerID],[CompanyName],[ContactName],[ContactTitle]FROM[Customers]"
UpdateCommand="UPDATE[Customers]SET[CompanyName]=@CompanyName,[ContactTitle]=@ContactTitleWHERE[CustomerID]=@CustomerID"
ConnectionString="<%$ConnectionStrings:AppConnectionString1%>">
<UpdateParameters>
<asp:ParameterType="String"Name="CompanyName"></asp:Parameter>
<asp:ParameterType="String"Name="ContactTitle"></asp:Parameter>
<asp:ParameterType="String"Name="CustomerID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:ButtonID="Button1"Runat="server"Text="Button"OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Text"%>
<%@ImportNamespace="System.Data.SqlClient"%>
<scriptrunat="server">
voidButton1_Click(objectsender,EventArgse)
{
StringBuilderquery=newStringBuilder();
for(inti=0;i<GridView1.Rows.Count;i++)
{
GridViewRowrow=GridView1.Rows[i];
stringvalue1=((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("'","''");
stringvalue2=((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("'","''");
stringvalue3=GridView1.DataKeys[i].Value.ToString();
query.Append("UPDATE[Customers]SET[CompanyName]='")
.Append(value1).Append("',[ContactTitle]='")
.Append(value2).Append("'WHERE[CustomerID]='")
.Append(value3).Append("';\n");
}
SqlConnectioncon=newSqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommandcommand=newSqlCommand(query.ToString(),con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
voidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
SqlConnectioncon=newSqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommandcommand=newSqlCommand("SELECT[CustomerID],[CompanyName],[ContactName],[ContactTitle]FROM[Customers]",con);
con.Open();
GridView1.DataSource=command.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>UntitledPage</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"Runat="server"DataKeyNames="CustomerID"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateFieldSortExpression="CustomerID"HeaderText="CustomerID">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("CustomerID")%>'ID="TextBox1"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldSortExpression="CompanyName"HeaderText="CompanyName">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("CompanyName")%>'ID="TextBox2"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldSortExpression="ContactName"HeaderText="ContactTitle">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("ContactTitle")%>'ID="TextBox3"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ButtonID="Button1"Runat="server"Text="Button"OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
以上所分享的是关于asp.net 2.0中一次性更新所有GRIDVIEW的记录,下面是编辑为你推荐的有价值的用户互动:
相关问题:ASP.NET中GridView更新数据问题
答:if (!IsPostBack) { GridView1数据绑定的时候加上这句。 } >>详细
相关问题:c# asp.net GridView中如何删除一条记录后刷新页面
答:我想你的问题是想说在 GridView 中删除一个行数据后。GridView 被刷新下。而不是全局页面被刷新。 对GridView删除一条记录有两种方法。一是直接对GridView的行删除。二呢。是对数据源的删除。 例如。 ------------------------------ 1.删除数据... >>详细
相关问题:如何在gridview中一次性批量更新多行数据
答:假定有一个Product表,字段有(Id,Name,Quantity,...)我们要一次批量更新Quantity的值 首先在Gridview中,Quantity列以TemplateField显示,其他的列属性设为只读,把显示格式设为TextBox 在GridView下面添加一个Button控件,定义onclick方法为u... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
