簡體   English   中英

更新數據庫后未更新GridView

[英]GridView Not Updating After Updating The Database

我有一個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中刪除該行。 我需要刷新頁面或單擊兩次以刪除該行。 一鍵怎么辦?

刪除行時,將網格與數據重新綁定。

您只需要再次加載DataSource並將其綁定到GridView:

DataTable table = PublicClass.ExeSql_Table(sql, "Utility");
GridView1.DataSource = table; //  <-- you've forgotten this
GridView1.DataBind();

在鏈接按鈕“ buttonDelete”中指定OnCommand字段

范例:

OnCommand = "Delete_Record"

並在aspx.cs中編寫以下代碼

  protected void Delete_Record(object sender, CommandEventArgs e)
{
       GridView1.DataSource = table; 
       GridView1.DataBind();    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM