• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Get value from asp:textbox client side.


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 part....how can i stop the button posting back??
  • 2
1 Solution
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
osiris247Author 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?
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now