简体   繁体   中英

Update gridview by clicking a button asp.net c#

I'm trying to auto-update my gridview upon clicking the asp:net button. My gridview contains accounts that are waiting to be verified by the admin. The gridview contains a select linkbutton. When the admin selects the link button and clicked the asp:net button, it is suppose to automatically update 'pending' to 'approved'. It will then refresh the gridview and automatically delete the pending account that has been approved.

I used this method response.redirect method

Response.Redirect("AdminVerify.aspx");

but it immediately refreshes my entire page and ignored my ajax scriptmanager

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

that i have added in. With the script manager, i reckon it is not suppose to refresh the entire page. Hence, i'm wondering how do i let a button update the gridview automatically maybe after 3 seconds when it is being clicked. I tried to input this html code but instead it automatically refresh my page every 5 sec even though i did not click anything

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

Source Code :

<%@ 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>

Back-end codes :

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");

    }
}
}

You can have separate method to load the gridview data. In first time page load you can call this method and also after you done changes on data you can reload the grid by calling this method.

    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();

    }

As far as I have observed GridViewID.DataBind() will update your grid on the server but it'll not reflect the changes on the browser (client-side). To get the changes reflected without calling Page_Load , just wrap your grid inside an UpdatePanel like this and change its mode to Conditional .

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

Then wherever you are binding data to the grid add UpdatePanel1.Update() after it.

C#

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM