ClientScript.RegisterClientScriptBlock not working

2019-07-30 04:55发布

I have a popup in my page which I am trying to show on dropdownlist selected index changed event. Here is register statement

ClientScript.RegisterClientScriptBlock(GetType(),"id", "ShowApplicationPopUp()",true);

Here is my javascript function

function ShowApplicationPopUp() {

    $('#NewCustomerMask').show("slow");
    $('#NewCustomerApplicationPopUp').show("slow");

}

Both of my divs are initially hidden by using display:none; statement. The problem is when my dropdownlist is changed the popup is not seen at all.I tried putting an alert statement to check if the function is called , and the alert statement is fired.Any ideas as to what I am doing wrong. Any suggestions are welcome. Thanks.

2条回答
唯我独甜
2楼-- · 2019-07-30 05:17

When you use RegisterClientScriptBlock the Javascript code is inserted early in the page, so it will run before the elements are loaded.

Use RegisterStartupScript instead, which places the code at the end of the form.

查看更多
手持菜刀,她持情操
3楼-- · 2019-07-30 05:34

I too could not get this code to work but thanks to the above I now have working code. Note, I have a linkbutton inside an Ajax Update Panel.

in my code behind aspx.cs page is:

protected void OpenButton_Click(object s, EventArgs e)
{
    // open new window
    string httpLink = "../newWindow.aspx";      
    ScriptManager.RegisterStartupScript(this, GetType(), "script", "openWindow('" + httpLink + "');", true);
}

in my apsx page is first the link to jQuery source, then second the JavaScript for the openWindow function:

<script src="../js/jquery-1.10.1.js" type="text/javascript"></script>

<script type="text/javascript">
    function openWindow(url) {
        var w = window.open(url, '', 'width=1000,height=1000,toolbar=0,status=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
        w.focus();
    }
</script>

and the link that makes it all happen:

<asp:LinkButton Text="Open New Window" ID="LnkBtn" OnClick="OpenButton_Click" runat="server" EnableViewState="False" BorderStyle="None"></asp:LinkButton>

Im not a jQuery expert and must attribute some of this to the following blog:

https://blog.yaplex.com/asp-net/open-new-window-from-code-behind-in-asp-net/

查看更多
登录 后发表回答