简体   繁体   English

从服务器端C#调用Javascript函数

[英]Call Javascript Function From Server Side C#

I created a report that shows a list of manifests. 我创建了一个显示清单清单的报告。 The user can search through this list by the manifest number. 用户可以通过清单编号搜索此列表。 When the code is running the search, I'm displaying a Gif: 当代码运行搜索时,我正在显示一个Gif: 在此处输入图片说明

But this Gif won't disappear once the search is finished. 但是一旦搜索完成,此Gif不会消失。 I can see the correct record is being displayed so the search is over, but the Gif stays on the screen. 我可以看到显示了正确的记录,因此搜索结束了,但是Gif仍停留在屏幕上。

The function is called when the search button is clicked. 单击搜索按钮时将调用该函数。

 <asp:Button runat="server" CssClass="btnSearch loading" ID="btnSearch" Text="Search" OnClick="btnSearch_Click" OnClientClick="ShowLoadingGif()" ToolTip="Search" />    

<div id="dvLoading">
        <table>
            <tr>
                <td id="tdLoadingSave"><img src="/images/loading.gif" alt="Loading..." title="Loading..." /></td>
            </tr>
        </table>            
    </div>

    function ShowLoadingGif() {
                closefiltermenu();
                $("#tdLoadingSave").html($("#tdLoadingSave").html() + "<br/> Please wait, manifest list is loading");
                $('#dvLoading').fadeIn("500");
        }

 function CloseLoadingGif() {
        $('#dvLoading').fadeOut("500");
    }

The search is then run from another function: 然后从另一个函数运行搜索:

 protected void Search()
    {
        string Field = ddlSearchBy.SelectedValue;
        string SearchString = txtSearchBy.Text;

        string[] SearchFields = null;
        string[] SearchStrings = null;

        if (!string.IsNullOrEmpty(SearchString) && Field != "null")
        {
            SearchFields = new string[] { Field };
            SearchStrings = new string[] { SearchString };
        }

        List<lookupManifestAnalysis> main = lookupManifestAnalysis.SearchManifestItems(Company.Current.CompanyID,
                SearchStrings,
                SearchFields);

        gvResults.DataSource = main;
        gvResults.DataBind();

        udpResults.Update();

        ClientScript.RegisterStartupScript(GetType(), "Search", "CloseLoadingGif();", true);

    }

But how do I stop the Gif displaying once the search is over? 但是,搜索结束后如何停止显示Gif?

ScriptManager.RegisterStartupScript(this, GetType(), "CloseLoadingGif","CloseLoadingGif();", true);

如果您正在处理asp.net UpdatePanel和UpdateProgress,请使用以下代码:

ScriptManager.RegisterStartupScript(myUpdatePanelID,myUpdatePanelID.GetType(),"CloseLoadingGif", "CloseLoadingGif();", true); 

When you are executing your code in Server side ASP you should not use client side function for showing or hiding elements. 在服务器端ASP中执行代码时,不应使用客户端函数来显示或隐藏元素。 Instead try using Ajax Controls from .Net. 而是尝试使用.Net中的Ajax控件。

You will need to use AJAX Progress control. 您将需要使用AJAX进度控件。 Here have a look : 看一下:

https://msdn.microsoft.com/en-us/library/bb386421.aspx https://msdn.microsoft.com/zh-CN/library/bb386421.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM