Link to home
Start Free TrialLog in
Avatar of SaraDob
SaraDob

asked on

Javascript : not working even with <%=textbox1.Clientid">, when included in masterpage

Hi all,
Hope to get few answers thsi time atlease. I have been posting questions, but failed to get even a comment for this : https://www.experts-exchange.com/questions/23914095/Tree-Menu-navigation-based-on-User-Session-Can-sombody-have-a-look-atleast.html

But the real question for this post is here: I have included my content page with the masterpage.
i have a textbox.The user is not supposed to enter more than 3 chars. I have set the maxlength of the textbox=3.
But i also want to alert the user " cannot enter more than 3 chars". I call a javascript function. It was all working fine as an individual .Aspx page.
now i have included that in a master page. Now the above said error come, when i try to enter  anything in that textbox.Here is piece of code.
Plase help..Hope to get answers SOON
'This is the javascript code
<asp:Content ID="ContProduct" ContentPlaceHolderID="MainContent" Runat="Server">
   
   <script type="text/javascript" >
 
    function ValidateTxtarea()
 
    {
 
    if (document.getElementid('<%=txtActivityCode.ClientID%>').value.length >= 3) 
        {
        alert('Activity Code Cannot be more than 3 Chars')
 
        return false;
        }
        else
        {
        return true;
 
        }
 
    }
 
    </script>
 
'this is how the textbox is :
 <asp:TextBox ID="txtActivityCode" runat="server" Onkeypress= "return ValidateTxtarea()" MaxLength="3"></asp:TextBox>

Open in new window

Avatar of silemone
silemone
Flag of United States of America image

can you try to use something like this:

var elem = document.getElementid('<%=txtActivityCode.ClientID%>');
alert(elem.value); <-- debug the value

Avatar of SaraDob
SaraDob

ASKER

Thnaks for quick reply.You have solved soo many of my problems..hoping to get this done ASAP.
I tried the code you suggested...Nope still the same problem. I get the error " Object or method not found.
But i get a build error as "OnKeypress"  of textbox is outdated. I dont think it will be the problem because, on key press, the javascript function is being called.Will that keypress make a difference?
ok...for the first problem...

what environment are you using?  vs2005?  

try this:  under Tool of IE window--> Select Internet Options -->Select Advanced Tab --> Uncheck Disable Script Debugging (in IE)...
This may give you a better error and specific line where the error is occurring if you're using vs2005...which I assume because in vs2008 you can actually debug javascript...

second,
If you don't have the right event name in your tags, it will give you errors, but the javascript method name can be similar...

also, when you load the page, you can actually look at view source and get the correct name for the control.  The source will show modified name due to it being in master page...
Avatar of SaraDob

ASKER

Silemone,
i'm using VS2008.
I had already unchecked the script debugging.
When i view source i get this in javascript:
 var elem = document.getElementid('ctl00_MainContent_txtActivityCode');
        alert(elem.value);  
Because of this manipulation of textboxe names, i have used
document.getElementid('<%=txtActivityCode.ClientID%>')
Any lead!!!!!!
ASKER CERTIFIED SOLUTION
Avatar of SaraDob
SaraDob

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
good job...

cheers...