Get value from asp:textbox client side.

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
LVL 7
osiris247Asked:
Who is Participating?
 
adatheladCommented:
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.
0
 
adatheladCommented:
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
0
 
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?
o
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.