簡體   English   中英

通過單擊按鈕ASP.NET C#更新gridview

[英]Update gridview by clicking a button asp.net c#

我試圖在單擊asp:net按鈕時自動更新gridview。 我的gridview包含等待管理員驗證的帳戶。 gridview包含一個選擇鏈接按鈕。 當管理員選擇鏈接按鈕並單擊asp:net按鈕時,假定自動將“待處理”更新為“已批准”。 然后,它將刷新gridview並自動刪除已批准的待處理帳戶。

我用這種方法response.redirect方法

Response.Redirect("AdminVerify.aspx");

但它立即刷新了我的整個頁面,並忽略了我的ajax scriptmanager

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>

我添加了。使用腳本管理器,我認為這不是刷新整個頁面的前提。 因此,我想知道如何讓按鈕自動被單擊時可能在3秒后自動更新gridview。 我嘗試輸入此html代碼,但即使沒有單擊任何按鈕,它也會每5秒自動刷新一次頁面

<meta http-equiv="refresh" content="5" >

源代碼 :

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin.Master" AutoEventWireup="true" CodeBehind="AdminVerify.aspx.cs" Inherits="AdminWebApp.AdminVerify" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<p>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    Unverified Officer&#39;s Account Information<br />
    <asp:GridView ID="GVVerify" runat="server" BackColor="#CCCCCC" BorderColor="#999999" Width="100%" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" AutoGenerateSelectButton="True">
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
        <RowStyle BackColor="White" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    Officer ID :
    <asp:Label ID="lblOID" runat="server"></asp:Label>
&nbsp;will be verify upon activation<br />
    <br />
    <asp:Label ID="lblMsg" runat="server"></asp:Label>
    <br />
    <br />
    <asp:Button ID="btnVerify" runat="server" OnClick="btnVerify_Click" Text="Verify" />

    <asp:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server"
        TargetControlID="btnVerify"
        ConfirmText="Are you sure you would like to verify this police officer?"
        OnClientCancel="CancelClick" />

</ContentTemplate>
    </asp:UpdatePanel>
    </p>
</asp:Content>

后端代碼:

public partial class AdminVerify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source =localhost;" +
                "Initial Catalog = project; Integrated Security = SSPI";
            conn.Open();

            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter("SELECT policeid, password, email, nric, fullname, contact, address, location From LoginRegisterPolice where pending='pending'", conn);
            da.Fill(ds);

            GVVerify.DataSource = ds;
            GVVerify.DataBind();

            conn.Close();

        }
}

    protected void GVVerify_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblOID.Text = GVVerify.SelectedRow.Cells[1].Text;
    }

    protected void btnVerify_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("Update LoginRegisterPolice set pending='approved' where policeid='"+lblOID.Text+"'", con);
        cmd.ExecuteNonQuery();

        lblMsg.Text = "The following officer has been verified.";
        Response.Redirect("AdminVerify.aspx");

    }
}
}

您可以使用單獨的方法來加載gridview數據。 在首次頁面加載中,您可以調用此方法,並且在完成數據更改后,可以通過調用此方法來重新加載網格。

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadGrid();
        }

    }

    private void LoadGrid()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source =localhost;" +
            "Initial Catalog = project; Integrated Security = SSPI";
        conn.Open();

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("SELECT policeid, password, email, nric, fullname, contact, address, location From LoginRegisterPolice where pending='pending'", conn);
        da.Fill(ds);

        GVVerify.DataSource = ds;
        GVVerify.DataBind();

        conn.Close();
    }

    protected void btnVerify_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("Update LoginRegisterPolice set pending='approved' where policeid='" + lblOID.Text + "'", con);
        cmd.ExecuteNonQuery();

        lblMsg.Text = "The following officer has been verified.";
        LoadGrid();

    }

據我觀察, GridViewID.DataBind()將更新服務器上的網格,但是不會反映瀏覽器(客戶端)上的更改。 要在不調用Page_Load情況下反映更改,只需將網格包裹在這樣的UpdatePanel ,並將其模式更改為Conditional

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:GridView ID="GVVerify" runat="server">
         ....
         ....
         ....
      </asp:GridView>
   </ContentTemplate>
</asp:UpdatePanel>

然后,無論您將數據綁定到網格的什么位置,都在其后添加UpdatePanel1.Update()

C#

    ....
    ....
    GVVerify.DataSource = ds;
    GVVerify.DataBind();
    UpdatePanel1.Update(); //this will reflect the changes on client-side

暫無
暫無

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

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