How to link an asp:textbox to an asp:imagebutton in a User Control

A

Alex D.

Does any body knows how to link an asp:textbox to an asp:imagebutton, both
inside a User Control in asp.net 2 beta 2? So when a user enter some text
in the textbox and press the "Enter" key the corresponding button click
event is launched?
Tnx.
 
S

souri challa

Alex,

You can register a java script that looks like below to get what you
need.

TextBox1.Attributes.Add("onkeydown", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.all." + ImageButton.ClientID + ".click();return
false;}else return true;");

You can use the following free control too.
http://www.metabuilders.com/tools/DefaultButtons.aspx

Asp.net 2.0 has the default button property built in.

HTH,
Souri Challa
 
A

Alejandro Penate-Diaz

Hi Souri, I tried that many times before with asp.net 2 beta 2 and it
doesn't work. It worked ok when I used asp.net 1.x. Problem is that asp.net
2 is rendering something like amp&; instead of just & and then the
javascript is not correct. I have tried many different options but none have
worked.
The built in default button property is just for one buton but I have like
five pairs textbox-imagebutton.

thanks,
alex.
 
S

souri challa

Ok. I see what is going on in asp.net 2.
Replacing the above code with below statement will do the trick.

TextBox1.Attributes.Add("onkey­down", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.getElementById(" + ImageButton1.ClientID +
").click();return false;}else return true;");

Reason: Asp.net 2.0 does not render a 'name' tag which is breaking the
above client script that uses document.all syntax. So changing it to
document.getElementById does the job.

-Souri
 
A

Alejandro Penate-Diaz

thanks a lot. I'll try that and I'll let you know.

alex.

Ok. I see what is going on in asp.net 2.
Replacing the above code with below statement will do the trick.

TextBox1.Attributes.Add("onkey­down", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.getElementById(" + ImageButton1.ClientID +
").click();return false;}else return true;");

Reason: Asp.net 2.0 does not render a 'name' tag which is breaking the
above client script that uses document.all syntax. So changing it to
document.getElementById does the job.

-Souri
 
A

Alejandro Penate-Diaz

It worked just OK. Thanks!


Ok. I see what is going on in asp.net 2.
Replacing the above code with below statement will do the trick.

TextBox1.Attributes.Add("onkey­down", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.getElementById(" + ImageButton1.ClientID +
").click();return false;}else return true;");

Reason: Asp.net 2.0 does not render a 'name' tag which is breaking the
above client script that uses document.all syntax. So changing it to
document.getElementById does the job.

-Souri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top