扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:廖煜嵘 来源:天极网 2007年11月7日
关键字: Windows
<script runat="server"> void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < Gridview1.Rows.Count; i++) { GridviewRow row = 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> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Gridview ID="Gridview1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False"> <Columns> <asp:TemplateField SortExpression="CustomerID" HeaderText="CustomerID"> <ItemTemplate> <asp:TextBox Runat="server" Text=’<%# Bind("CustomerID") %>’ ID="TextBox1"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField SortExpression="CompanyName" HeaderText="CompanyName"> <ItemTemplate> <asp:TextBox Runat="server" Text=’<%# Bind("CompanyName") %>’ ID="TextBox2"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField SortExpression="ContactName" HeaderText="ContactTitle"> <ItemTemplate> <asp:TextBox Runat="server" Text=’<%# Bind("ContactTitle") %>’ ID="TextBox3"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:Gridview> <asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID" ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind"> <UpdateParameters> <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter> <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter> <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter> </UpdateParameters> </asp:SqlDataSource> <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html> |
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString); SqlCommand command = new SqlCommand("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]", con); con.Open(); Gridview1.DataSource = command.ExecuteReader(); Gridview1.DataBind(); con.Close(); } } protected void Button1_Click(object sender, EventArgs e) { StringBuilder query = new StringBuilder(); for (int i = 0; i < Gridview1.Rows.Count; i++) { GridviewRow row = Gridview1.Rows[i]; string value1 = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("’", "’’"); string value2 = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("’", "’’"); string value3 = Gridview1.DataKeys[i].Value.ToString(); query.Append("UPDATE [Customers] SET [CompanyName] = ’").Append(value1).Append("’ , [ContactTitle] = ’") .Append(value2).Append("’ WHERE [CustomerID] = ’").Append(value3).Append("’;\n"); } SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString); SqlCommand command = new SqlCommand(query.ToString(), con); con.Open(); command.ExecuteNonQuery(); con.Close(); } } |
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=LIAO;Initial Catalog=Northwind;User ID=sa;Password=xxxx" providerName="System.Data.SqlClient"/> </connectionStrings> |
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者