0

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

    }
}
}
4
  • What is the purpose of the button? Just to update it every so often or because new data has been entered that will affect the Gridview? Commented May 28, 2013 at 3:18
  • The button is just to verify some pending accounts. I have set the button to update the column from pending to approved. Commented May 28, 2013 at 3:20
  • where your button located? is it inside UpdatePanel1? show the code what you doing on button click Commented May 28, 2013 at 3:22
  • can you post your full code behind? Commented May 28, 2013 at 3:26

2 Answers 2

1

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

    }
Sign up to request clarification or add additional context in comments.

3 Comments

it works. However i got one problem. This code : lblMsg.Text = "The following officer has been verified." The gridview indeed automatically update however the label still has the name in it.
since label also inside the same update panel that label text should update
Actually i managed to figure it out by adding this line of code lblOID.Text = "";
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.