【问题标题】:Refresh Page twice during Click - EventClick - 事件期间刷新页面两次
【发布时间】:2014-03-23 20:05:35
【问题描述】:

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

我已经尝试使用 Response.Redirect,因此页面执行 PostBack 并在 Page_Load - 事件中检查 isGUInactive 变量。如果为 true,则 GUI 将被禁用,直到 SQL 查询完成,然后我再次刷新并将 isGUIinactive 设置为 true,因此 GUI 将在 Page_Load - 事件的另一个 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
}

更新:

平均售价:

<%@ 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);
}

【问题讨论】:

  • 首先,当您进行响应重定向时,其余代码不会执行
  • 如果你想禁用 gui,使用部分页面渲染。你可以通过控制面板来实现它。
  • 你在转义javascript吗?您似乎试图通过服务器上的 c# 完成所有工作
  • @Banana 你说得对,更新了我的问题,忘记添加参数了。
  • @user3453011 stackunderflow 是对的,你应该使用 javascript 来达到最佳效果。如果我有时间,我会试着给你写一些简洁的例子

标签: c# asp.net sql sharepoint


【解决方案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");
    }
}

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

【讨论】:

  • 非常感谢,但由于 OnClientClick -Event,我的 OnClick -Event 似乎被忽略了
  • 是的,我的朋友,这正是UseSubmitBehavior="false"放入的原因,你的点击甚至会被执行
  • 谢谢,但我已经包含了这个 sn-p: 脚本正常执行并锁定了整个GUI。
  • 而您的 OnClick 只是被忽略了?
  • 你能在你的点击处理程序中放一个断点,看看那里的代码是否会失败?
猜你喜欢
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多