【发布时间】: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