Javascript in ASP.Net page

I'm following this example (the first part) and it works fine in a page with a form.
http://rosshawkins.net/archive/2006/12/04/asp.net-text-counter.html.aspx

I want to do the same in a content page (page that has a master page). But i keep getting
"txtMessage is not defined."

I think this is because of "is.form.remLen" but I'm not sure. What can I do? thanks
LVL 8
CamilliaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddayx10Commented:
I'd guess that you didnt do the stuff listed in the section: "Adding some Server Side smarts".

When you add your page to a masterpage then your textbox id is going to rendered using the "inamingcontainer" convention. So when the id changes it is breaking the code.

But there's some bigger issues, and you can likely ignore the above for a simpler fix.

Also lets take a look at this:
<asp:TextBox ID="txtMessage" TextMode="MultiLine"  Width="200px" Rows="3" runat="server"  onkeyup="textCounter(txtMessage, this.form.remLen, 160);" onkeydown="textCounter(txtMessage, this.form.remLen, 160);" />

Specifically this:
onkeyup="textCounter(txtMessage, this.form.remLen, 160);"

If I remember correctly this works because of a quirk in IE where you can pass "txtMessage" as a parameter for your function (because it sees it as an object for some reason) and it works as though you were passing (this). I'm pretty sure if you try this in FF it will fail. I'm also 99% sure if used with a masterpage this will fail.

Never-the-less that is just old memory that may be tricking me. I'm pretty sure I stumbled on this once and thought it was cool, then realized it only worked in IE and so stopped using it.

What may be better all around and resolve both your "masterpage/clientID" issue and the "browser specific" issue is changing to this:

<asp:TextBox ID="txtMessage" TextMode="MultiLine"  Width="200px" Rows="3" runat="server"  onkeyup="textCounter(this, this.form.remLen, 160);" onkeydown="textCounter(this, this.form.remLen, 160);" />

Now the parameter "field" in function textCounter(field, countfield, maxlimit) should know the textbox without needing a clientID or any ID at all.

Its worth noting that when I tested this on a masterpage/page setup I had the darndest time getting the onkeydown to see my javascript function unless I moved the script to the end of the page under the textboxes? Not sure what that was about...(likely something to do with calling form.elements as parameters).

Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
wow, thanks for the detailed answer. Let me read. You said you tested it, can u attach your test pages here?
0
ddayx10Commented:
sure its nothing fancy just your code... see attached
PageForMaster.zip
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.