Get value from asp:textbox client side.

Posted on 2006-03-27
Last Modified: 2012-06-27

Ok, something i thought was easy has turned out not so much!

I am trying to get the value from an asp:textbox and without postback.
I have this.....
const string _AccountScript = @"
        <script language='javascript' type='text/javascript'>
            function AddAccount()
                var chrAccountName = document.getElementById('txtAccount').value;
                WinOpen('ToolsPopup.aspx?chrAccountName= + chrAccountName,290,520,'Aname');
                return false;
ClientScript.RegisterClientScriptBlock(GetType(), "AddAccount", _AccountScript);

in button properties i have AddAccount();
I have debugged as best i can and the getElementById is where it falls over.  I can set the var to a const string and it works fine??? My textbox is is without a doubt 'txtAccount' (correct case).

The error i get is 'Object Required'.

The second can i stop the button posting back??
Question by:osiris247
    LVL 23

    Expert Comment

    If you view the source HTML from your browser, can you confirm the textbox does have an id of "txtAccount"?

    i.e. if you have a usercontrol with a textbox (id "txtAccount") in, and have that usercontrol on your webform, the actual ID it will render with will include a prefix to link the textbox to the usercontrol it's embedded in, hence why your JS may not find it

    >> The second can i stop the button posting back??
    Use a normal HTML button
    LVL 7

    Author Comment

    thanks...first part.  You were correct the id does have a long prefix. :)

    second part.
    Its not a button, its a custom control i have inherited from the ImageButton class.

    Any ideas?
    LVL 23

    Accepted Solution

    Ok, you can cancel the postback by adding "return false;" to the onclick event of the button.


    <asp:button id="btnSomebutton" runat="server"/>

    In code behind:
    btnSomebutton.attributes.add("onclick", "alert('Test Message');return false;")

    This example will display a javascript alert box, and then by having the "return false" afterwards, it will prevent a postback.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now