C# 如何更新gridview中的值?
我在.aspx页面中有一个网格,其中有三个文本字段,如C# 如何更新gridview中的值?,c#,asp.net,sql-server-2005,webforms,C#,Asp.net,Sql Server 2005,Webforms,我在.aspx页面中有一个网格,其中有三个文本字段,如name、id、role,还有一个编辑按钮,单击该按钮,此行的值将显示在txtname、txtid、txtroll中 我要更新此值并将其插入数据库 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Update") { string uname = "";
name
、id
、role
,还有一个编辑按钮,单击该按钮,此行的值将显示在txtname、txtid、txtroll中
我要更新此值并将其插入数据库
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
string uname = "";
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
TextBox txtbox1_val = (TextBox)row.FindControl("TxtChangeActive");
uname = Server.HtmlDecode(row.Cells[1].Text.ToString());
string upd_query = "update login set active='" + txtbox1_val.Text + "' where uname='" + uname + "' ";
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmd = new SqlCommand(upd_query, con);
cmd.ExecuteNonQuery();
string query_login = "select * from Login";
SqlDataAdapter da_login = new SqlDataAdapter(query_login, con);
DataSet ds_login = new DataSet();
da_login.Fill(ds_login);
GridView1.DataSource = ds_login;
GridView1.DataBind();
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
在设计页面中
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"
AlternatingRowStyle-BackColor="#006699"
AlternatingRowStyle-ForeColor="#FFFFFF" onrowupdating="GridView1_RowUpdating">
<Columns >
<asp:BoundField HeaderText="Name" DataField="uname" />
<asp:BoundField HeaderText="Pass" DataField="upass"/>
<asp:TemplateField>
<HeaderTemplate>Active</HeaderTemplate>
<ItemTemplate >
<asp:TextBox ID="TxtChangeActive" runat="server" Text='<%#Eval("active")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
活跃的
在设计页面中
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"
AlternatingRowStyle-BackColor="#006699"
AlternatingRowStyle-ForeColor="#FFFFFF" onrowupdating="GridView1_RowUpdating">
<Columns >
<asp:BoundField HeaderText="Name" DataField="uname" />
<asp:BoundField HeaderText="Pass" DataField="upass"/>
<asp:TemplateField>
<HeaderTemplate>Active</HeaderTemplate>
<ItemTemplate >
<asp:TextBox ID="TxtChangeActive" runat="server" Text='<%#Eval("active")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
活跃的
我知道代码更新代码bt我无法在同一页面中获取文本feild中的值,无法更新详细信息:我必须在aspx页面中输入用户名、密码和提交按钮。提交后,它将在aspx页面中显示。网格中有更新按钮,如果单击相同的aspx,将显示哪个hv用户名,密码feild如果您在此处更改值,将显示值thn该值将在数据库中更新这是我的工作如果您可以plzz向我发送代码我知道代码更新代码bt我无法在同一页面中获取文本feild中的值,也无法更新详细信息:我必须像userid一样在aspx页面中输入,密码和提交按钮提交后,将在aspx页面中显示更新按钮,如果您单击相同的aspx,将显示哪个hv用户ID,密码字段,如果您在此处更改值,将显示该值。如果您可以向我发送代码,则该值将在数据库中更新,这是我的工作