Associate Command Button to Javascript Code

A

Angel

How do I associate a server side command button to a
Javascript function that is in the Web Form HTML Code?

Thanks
 
H

Hermit Dave

You have to use RegisterClientScriptBlock to write a javascript funciton to
browser... to associate it... just use say onclick='function_name();' add
that to ur control...
Here's some code from my earlier post:

on aspx page
<td colspan="3">
<asp:LinkButton id="lnkUpdate"
runat="server">Update</asp:LinkButton>&nbsp;&nbsp;
<asp:HyperLink id="lnkCancel" onclick='CancelMe();' runat="server"
NavigateUrl="#">Cancel</asp:HyperLink>
</td>

in code behind .cs file within Page

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// Form the script that is to be registered at client side.
String scriptString = "<script language=JavaScript> function CancelMe()
{";
scriptString += "window.history.back(); }</script>";

if(!this.IsClientScriptBlockRegistered("clientScriptCancel"))
this.RegisterClientScriptBlock("clientScriptCancel", scriptString);

if(!Page.IsPostBack)
...............
 
K

Kikoz

Hi.

Use Attributes collection of the Button web control.
Ex:

public Button btn;

public void SomeEvent(someParameters)
{
btn.Attributes["onClick"] = "javascript: alert('Event
happened');";
}

Check the syntax (I'm avay from VS right now :))

Regards.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top