We help IT Professionals succeed at work.

Get value from asp:textbox client side.

osiris247
osiris247 asked
on
Medium Priority
246 Views
Last Modified: 2012-06-27
Hi

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');
                //alert('chrAccountName');
                return false;
            }
        </script>
    ";
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 part....how can i stop the button posting back??
thanks
o
Comment
Watch Question

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 part....how can i stop the button posting back??
Use a normal HTML button

Author

Commented:
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?
o
Ok, you can cancel the postback by adding "return false;" to the onclick event of the button.

e.g.

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.