Button Click and JavaScript

G

Guest

Hi all

I have an ASP.NET Button Control. I woud like this button to make a Call to
JavaScript function on some condition. How would i do this

Thanks
Sekhar
 
K

Karl Seguin [MVP]

you need to add the javascript event ala

myButton.Attributes.Add("onclick", "doSomething();");

if the condition is server side, you can simply wrap the above line in your
if, otherwise put the condition in the doSomething() javascrit function.

Karl
 
S

Swanand Mokashi

Add an attribute to your button (say btnTest)

btnTest.Attributes.Add("onClick","your_javascript")

e.g.
btnTest.Attributes.Add("onClick", "javascript:alert('Hello')";

or
btnTest.Attributes.Add("onClick", "YourJavascriptFunction()")



--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
 
G

Guest

Hi
It doesnt work the way you suggested. Here is the scenario

I have a button btnTest and two text boxes UID and PWD
If the UID and PWD entered are valid, I would like to run the JavaScript Code

If I do it the way you suggested,
btnTest.Attributes.Add("onclick","ClientEvent()")
First the ClientEvent is fired then the Serverside code gets called which I
dont want to happen

I want the Client Side code to run only if the Condition is true on server
side

Thanks
Sekhar
 
S

Swanand Mokashi

In that case you can use the "RegisterClientScriptBlock" --see
http://msdn.microsoft.com/library/d...UIPageClassRegisterClientScriptBlockTopic.asp

You can add after you check the condition on the server.
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
 
J

Jeff Dillon

If you want the server code to execute, return True from your javascript
function. Otherwise, return false;

Jeff
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top