2013-05-28 48 views
0

我试图在点击asp:net按钮时自动更新我的gridview。我的gridview包含正在等待管理员验证的帐户。 gridview包含一个select linkbutton。当管理员选择链接按钮并单击asp:net按钮时,假设自动将“挂起”更新为“已批准”。然后,它将刷新gridview并自动删除已批准的待处理帐户。通过单击按钮更新gridview asp.net c#

我用这个方法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。 我试着输入这个网站的代码,而是它会自动刷新我的网页每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"); 

    } 
} 
} 
+0

什么是按钮的目的是什么?只是每隔一段时间更新一次,或者是因为输入了会影响Gridview的新数据? – adaam

+0

该按钮仅用于验证一些待处理的帐户。我已经设置了按钮来将列从待处理更新为已批准。 –

+0

你的按钮位于哪里?它在UpdatePanel1内吗?显示代码你在做什么按钮点击 – Damith

回答

1

您可以有单独的方法来加载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(); 

    } 
+0

它的工作原理。但是我遇到了一个问题。 此代码:lblMsg.Text =“以下人员已通过验证。” GridView确实自动更新,但标签中仍然有名称。 –

+0

因为标签也在同一个更新面板中,所以标签文本应该更新 – Damith

+0

其实我设法通过添加这一行代码来解决它012 lblblOID.Text =“”; –

0

据我观察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