Associate Command Button to Javascript Code

  • Thread starter Thread starter Angel
  • Start date Start date
A

Angel

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

Thanks
 
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)
...............
 
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.
 
Back
Top