2013-05-28 83 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