0

我有以下代码:

<script type="text/javascript">    

    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandle);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);
    function beginRequestHandle(sender, Args) {
        //Do something when call begins. 

        document.getElementById("btn1").style.visibility = "hidden";
        document.getElementById("btn2").style.visibility = "hidden";
    }

    function endRequestHandle(sender, Args) {
        if (document.getElementById('<%= hfResultsCount.ClientID %>').value != 0) {
            document.getElementById("btn1").style.visibility = "visible";
            document.getElementById("btn2").style.visibility = "visible";
        }
        else {
            document.getElementById("results").innerHTML = "<br><b><center><font style='font-family:Haettenschweiler; font-size:xx-large'>No data found, please try again.</b></font></center>";
        }
    }
</script>

和 btn2 的代码:

 <input type="button" runat="server" name="btn2" id="btn2" value="New Window"
 style="visibility:hidden;font-weight:bold;width:200" onclick="window.open('http://microsoft.com');" />

我正在使用 Js 来显示/隐藏按钮(需要这样做,所以不要提出其他建议),虽然btn1asp:button总是有效,但<input type=button>我一直收到这个错误

Microsoft JScript runtime error: Unable to get value of the property 'style': object is null or undefined

解决这个问题的方法btn1是添加ClientID=Static,但如何为<input>按钮做到这一点?(我不想让它成为 asp:button 因为我需要它不回发)

一切都在一个带有 ClientID=Static 的 UpdatePanel 中。

我知道它与 ID 和母版页有关,因为它可以在自己的页面上正常工作。

4

3 回答 3

1

If you do not need to access button on server side then you should not put runat="server", This will make your script to find button and it will not generate error.

<input type="button"  name="btn2" id="btn2" value="New Window"
 style="visibility:hidden;font-weight:bold;width:200" onclick="window.open('http://microsoft.com');" />

OR, If you want to make runat="server" you can access it like this

document.getElementById(<%= btn1.ClientID %>).style.visibility = "visible";
于 2012-07-10T11:22:51.790 回答
0

After the page rendering go to view source and check how ID is appearing with master page you may get some idea

It might not be btn directly....master_xxx

于 2012-07-10T11:21:28.003 回答
0

这应该工作:

<input type="button" runat="server" ClientIDMode="Static" 
name="btn2" id="btn2" value="New Window"
style="visibility:hidden;font-weight:bold;width:200"
onclick="window.open('http://microsoft.com');"/>

或者,如果您不需要访问后面代码中的控件:

<input type="button" name="btn2" id="btn2" value="New Window" 
style="visibility:hidden;font-weight:bold;width:200"
onclick="window.open('http://microsoft.com');"/>
于 2012-07-10T11:54:29.813 回答