ClientScript.RegisterClientScriptBlock not working

三世轮回 提交于 2019-12-24 09:18:39

问题


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.


回答1:


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.




回答2:


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/



来源:https://stackoverflow.com/questions/10927123/clientscript-registerclientscriptblock-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!