显示/隐藏ASPX背后的代码div

时间:2016-11-30 11:31:53

标签: javascript html css asp.net

我有以下html页面(使用bootstrap),按钮和进度如下:

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

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

由于

2 个答案:

答案 0 :(得分:0)

你可以简单地使div runat =&#34; server&#34;并在代码中显示隐藏。

<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;

答案 1 :(得分:0)

通过对OnClientClick按钮处理程序作出反应,在客户端显示加载指示符。

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

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