在Click - Event期间刷新页面两次

时间:2014-03-23 20:05:35

标签: c# asp.net sql sharepoint

我正在开发一个使用ASP.NET和C#作为代码的网页。 我有一个带有按钮的页面,该按钮应触发事件刷新或重新呈现页面2次并启动SQL查询:1是在SQL查询运行时使GUI处于非活动状态,2刷新或重新呈现以切换回普通的GUI。

我已经使用Response.Redirect尝试了它,所以页面执行PostBack并在Page_Load中 - 事件I检查isGUInactive变量。如果为true,则GUI将被禁用,直到SQL查询完成,然后我再次刷新并将isGUIinactive设置为true,因此GUI将在Page_Load - Event中的另一个PostBack中启用。 但是Response.Redirect不能以这种方式工作。等到button_Click方法完成,然后执行PostBack。 您可以为我提供任何替代方案,让我的页面刷新或重新渲染两次以实现我的目标吗?

private void button_Click(object sender,EventArgs e)
{  
   isGUIinactive = true; // Disable GUI
   Response.Redirect(Request.RawUrl, false); // Disable GUI in PostBack

   using (SqlConnection connection = new SqlConnection(connectionString))
      {
          connection.Open();

          SqlCommand command = new SqlCommand(sqlcomm, connection);
          command.CommandType = CommandType.Text;
          SqlDataReader reader = command.ExecuteReader();  
          DataTable dt = new DataTable();
          dt.Load(reader);
          BindData(dt); // Bind Data to GridView
      }
   isGUIinactive = false; // Enable GUI
   Response.Redirect(Request.RawUrl, false); // Enable GUI in PostBack
}

更新

ASP:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"  %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint"  Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true"  CodeBehind="GUIUserControl.ascx.cs" Inherits="Project.GUI.GUIUserControl" %>

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">


<script type="text/javascript">
  function lockGUI() {
      var blocking_div = "<div style='" +
            "position:fixed;" +
            "width:100%;" +
            "height:100%;" +
            "left:0;" +
            "top:0;" +
            "background-color:#444455;" +
            "color:white;" +
            "text-align:center;font-size:20px;" +
            "'>Loading</div>";
      var GUIPanel = document.getElementById("block_div");
      GUIPanel.style["visibility"] = "hidden";
      document.body.innerHTML += blocking_div;

  }

    </script>

<body>

<div id="block_div" style="background-color:White;">

// Big GridView

<asp:Table ID="Table1" runat="server" BorderWidth="0px" Width="233px" 
Height="16px">
<asp:TableRow ID="Tr1" runat="server" >
    <asp:TableCell>
        <asp:Button UseSubmitBehavior="false"  ID="btnsynchro" runat="server" Text="synchronize table" 
OnClick="btnsynchro_Click" OnClientClick="lockGUI();" />
    </asp:TableCell>

    <asp:TableCell>
        <asp:Button ID="btntest" runat="server" Text="for testing" 
        onclick="btntest_Click"/>
    </asp:TableCell>
</asp:TableRow>

  </asp:Table>

</div>

</body>
</html>

C#

protected void btnsynchro_Click(object sender, EventArgs e)
 {
using (SqlConnection connection = new SqlConnection(connectionString))
  {
      connection.Open();

      SqlCommand command = new SqlCommand(sqlcomm, connection);
      command.CommandType = CommandType.Text;
      SqlDataReader reader = command.ExecuteReader();  
      DataTable dt = new DataTable();
      dt.Load(reader);
      BindData(dt); // Bind Data to GridView
  }
 Response.Redirect(Request.RawUrl, false);
}

1 个答案:

答案 0 :(得分:1)

好吧,所以这里:

基本上一个简单的解决方案是调用javascript函数来禁用GUI,然后执行asp.net点击。

它可以通过onClientClick属性轻松完成,它调用一个javascript函数,然后执行OnClick。

我做了一个例子,它有一个页面页面Default.aspx,在这个例子中我隐藏了gui div并在页面上放置了一个阻塞div:

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function lockGUI() {
            var blocking_div = "<div style='" +
                "position:fixed;" +
                "width:100%;" +
                "height:100%;" +
                "left:0;" +
                "top:0;"+
                "background-color:#444455;" +
                "color:white;" +
                "text-align:center;"+
                "'>Loading, Please wait.</div>";
            var GUIPanel = document.getElementById("UI_DIV");
            GUIPanel.style["visibility"] = "hidden";
            document.body.innerHTML += blocking_div;

        }

    </script>

</head>
<body>
    <form runat="server" id="TheForm">
   <div id="UI_DIV" style="background-color:red;">
       this is the ui 
       <asp:Button UseSubmitBehavior="false" OnClientClick="lockGUI();" ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
   </div>
    </form>
</body>
</html>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 1000000000; i++)
        {
             //just a codeblock to make it load long, replace with yours.
        }
        Response.Redirect("Default.aspx");
    }
}

如果您有任何疑问,请随时提出