【问题标题】:Show/Hide div from code behind ASPX从 ASPX 后面的代码中显示/隐藏 div
【发布时间】:2016-11-30 19:38:33
【问题描述】:

我有以下 html 页面(带引导程序),按钮和进度如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestLoading.WebForm1" %>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="/css/bootstrap.min.css" rel="stylesheet" />
<title></title>
</head>
<body>
<form id="form1" runat="server">

<div>
 <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="Send" Width="71px" CssClass="btn btn-info" style="display: inline"/>
      <div class="progress" runat="server"  id="loadingSend">
          <div  class="progress-bar progress-bar-info progress-bar-striped active" aria-valuemax="100" aria-valuemin="0" aria-valuenow="100" role="progressbar" style="display: none">
              Loading...
          </div>
        </div>
</div>
</form>

我想在后面的代码中显示/隐藏进度条,我尝试了什么:

 private HttpWebRequest request;

 protected void btnSend_Click(object sender, EventArgs e)
    {            
        request = (HttpWebRequest)WebRequest.Create("http://localhost:65533/MyService.svc/");

        request.Method = WebRequestMethods.Http.Get;
        request.ContentType = "application/json";

        HtmlControl control = FindControl("loadingSend") as HtmlControl;
        control.Style.Add("display", "block");

        request.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
    }

    void FinishWebRequest(IAsyncResult result)
    {
        StringBuilder sb = new StringBuilder();
        request.EndGetResponse(result);

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

        HtmlControl control = FindControl("loadingSend") as HtmlControl;
        control.Style.Add("display", "none");
    }

但这对我不起作用,有什么建议吗?

谢谢

【问题讨论】:

    标签: javascript html css asp.net


    【解决方案1】:

    您可以简单地使 div runat="server" 并显示隐藏在代码中。

    <div id="divProgress" runat="server"  class="progress-bar progress-bar-info progress-bar-striped active" aria-valuemax="100" aria-valuemin="0" aria-valuenow="100" role="progressbar" style="display: none">
      Loading...
    </div>
    

    在 C# 中

    divProgress.Visible = false;
    

    【讨论】:

    • 嗨,感谢它的工作并在按钮回调中显示它,但在 FinishWebRequest 中它不适合我
    • 请求以btnSend_Click结束时可以调用此代码
    • 请求是异步的
    • 显示设置为 true 像这样divProgress.Visible = true;
    • 是的,这就是我正在做的事情
    【解决方案2】:

    如何通过响应OnClientClick 按钮处理程序在客户端显示加载指示器。

    <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="Send" OnClientClick="onBeforeSend"/>
    

    所以执行并返回结果后,刷新页面可以再次禁用。

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2015-10-20
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多