Click here to Skip to main content
14,921,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<script type="text/javascript">

    function onTxtDoubleClick() {

        var x = document.getElementById("<%= Button1.ClientID %>");
    if (x) x.click(); else alert(x); }
   </script>


Always return null .Please Hep me?

I am using this script on my usercontrol and the user control includes texbox and Button1. When I am adding this usercontrol in to my pages then problem occurs because document.getElementById("<%= Button1.ClientID %>"); retuns null because the Button ,Button1, dosent Exist in the page.Button1 exist in usercontrol
Posted
Updated 6-Aug-13 19:19pm
v2
Comments
ZurdoDev 6-Aug-13 8:15am
   
Debug it and tell us what is happening.
Member 8390746 7-Aug-13 1:07am
   
I am using this script on a user control and when I am adding this user control in to my page then problem occurs. document.getElementById("<%= Button1.ClientID %>"); returns null because there is no button, Button1, in my page.
[no name] 6-Aug-13 8:46am
   
What is your purpose here?
ZurdoDev 7-Aug-13 7:25am
   
Why is Button1 not on your page?

When controls are in user controls and then placed on a page, the actual client ID has part of the usercontrol name in it to keep the IDs unique. Some of the solutions have pointed that out so you may be better off just finding what the actual ID is and using that.
Member 8390746 7-Aug-13 8:03am
   
My user control name is txtLOV and in debugg
var x =document.getElementById("<%= Button1.ClientID %>"); looks like
var x = document.getElementById("'txtLOV2_Button1'");
and x is null
ZurdoDev 7-Aug-13 8:18am
   
You'll have to view source. It is likely ctl001_txtLOV2_Button1.
[no name] 8-Aug-13 2:00am
   
Have you tried solution 2. I guess that is the reason you might be getting null.

Do you try this?
C#
document.getElementById('buttonName')
   
v2
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <script type="text/javascript">
        function TextBoxDoubleClick() {
            alert("Double click detected.");
        }
    </script>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:textbox id="TextBox1" ondblclick="TextBoxDoubleClick()" runat="server"></asp:textbox>
    </div>
    </form>
</body>
</html>
   
v2
JavaScript
<script type="text/javascript">
 
    function onTxtDoubleClick() {
 
        var x = document.getElementById("buttonclientid");
    if (x) x.click(); else alert(x); }
   </script>

will work for you

regards....:)
   
v2
Comments
Member 8390746 7-Aug-13 1:55am
   
It also return null
Dholakiya Ankit 7-Aug-13 2:08am
   
show me ur code pls....
use browser developer tools to find out if the id of the button is same as u have given and 'x' is not a boolean value so compare it with some value. hope this will help u
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900