简体   繁体   English

通过单击按钮ASP.NET C#更新gridview

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

I'm trying to auto-update my gridview upon clicking the asp:net button. 我试图在单击asp:net按钮时自动更新gridview。 My gridview contains accounts that are waiting to be verified by the admin. 我的gridview包含等待管理员验证的帐户。 The gridview contains a select linkbutton. gridview包含一个选择链接按钮。 When the admin selects the link button and clicked the asp:net button, it is suppose to automatically update 'pending' to 'approved'. 当管理员选择链接按钮并单击asp:net按钮时,假定自动将“待处理”更新为“已批准”。 It will then refresh the gridview and automatically delete the pending account that has been approved. 然后,它将刷新gridview并自动删除已批准的待处理帐户。

I used this method response.redirect method 我用这种方法response.redirect方法

Response.Redirect("AdminVerify.aspx");

but it immediately refreshes my entire page and ignored my ajax scriptmanager 但它立即刷新了我的整个页面,并忽略了我的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. 因此,我想知道如何让按钮自动被单击时可能在3秒后自动更新gridview。 I tried to input this html code but instead it automatically refresh my page every 5 sec even though i did not click anything 我尝试输入此html代码,但即使没有单击任何按钮,它也会每5秒自动刷新一次页面

<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. 您可以使用单独的方法来加载gridview数据。 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). 据我观察, GridViewID.DataBind()将更新服务器上的网格,但是不会反映浏览器(客户端)上的更改。 To get the changes reflected without calling Page_Load , just wrap your grid inside an UpdatePanel like this and change its mode to Conditional . 要在不调用Page_Load情况下反映更改,只需将网格包裹在这样的UpdatePanel ,并将其模式更改为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. 然后,无论您将数据绑定到网格的什么位置,都在其后添加UpdatePanel1.Update()

C# 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