user1468537
user1468537

Reputation: 703

asp.net controlIDs

I have the following code:

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

and the code for 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');" />

I am using Js to show/hide buttons (needs to be done like this so don't suggest otherwise) and while btn1 is asp:button it always works but for <input type=button> I keep getting this error

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

The way to fix that for btn1 was to just add ClientID=Static but how to do that for <input> button? (I do not want to make it asp:button since I need it to not postback)

Everything is in an UpdatePanel with ClientID=Static also.

I know its something to do with the IDs and the master page since it works fine on a page on its own.

Upvotes: 0

Views: 190

Answers (3)

Julien Ch.
Julien Ch.

Reputation: 1261

This should work :

<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');"/>

or, if you don't need to access the control in your code behind :

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

Upvotes: 0

Adil
Adil

Reputation: 148120

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

Upvotes: 1

Peru
Peru

Reputation: 2971

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

Upvotes: 0

Related Questions