Button Click and JavaScript

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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

Back
Top