我有一个GridView。我在此GV中显示系统中的所有用户,我有一个按钮,通过将表中已删除的列更新为1来删除用户。
<asp:GridView ID="GridView1" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"
PageSize="20" runat="server" OnRowCommand="GridView1_OnRowCommand"
DataKeyNames="Id, Email" EnableViewState="false" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="buttonDelete" runat="server" CommandName="Remove" Text="Delete"
CommandArgument='<%# Eval("Id") + ";" +Eval("Email")%>'
OnClientClick='return confirm("Are you sure you want to delete this user?");' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
string cmdName = e.CommandName;
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ';' });
string UserRowID = commandArgs[0];
string Email = commandArgs[1];
string sql = "UPDATE USERS " +
"SET [Deleted]= 1" +
"WHERE ROWID= " + UserRowID;
DataTable table = PublicClass.ExeSql_Table(sql, "Utility");
GridView1.DataBind();
}
当我点击删除时,它会更新数据库,但不会删除GV中的行。我需要刷新页面或单击两次以删除该行。我怎么能一键完成呢?
答案 0 :(得分:6)
删除行时,请使用数据重新绑定网格。
答案 1 :(得分:5)
您只需再次加载DataSource并将其绑定到GridView:
DataTable table = PublicClass.ExeSql_Table(sql, "Utility");
GridView1.DataSource = table; // <-- you've forgotten this
GridView1.DataBind();
答案 2 :(得分:0)
在linkbutton“buttonDelete”
中指定OnCommand字段示例:
OnCommand = "Delete_Record"
在aspx.cs中编写以下代码
protected void Delete_Record(object sender, CommandEventArgs e)
{
GridView1.DataSource = table;
GridView1.DataBind();
}