Solved

Using  JavaScript in C Sharp button event

Posted on 2013-01-29
6
365 Views
Last Modified: 2013-01-30
Hi,

How can I call a JavaScript code in C Sharp Button even call???

Your help will be appreciated.
Thank you!

protected void btnMyBtTest_Click(object sender, EventArgs e)
{

// Need to use a JavaScript call here

}

Open in new window

0
Comment
Question by:Rad1
  • 3
  • 3
6 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 500 total points
ID: 38832569
Probably the best explanation for this is here:
http://forums.asp.net/t/1117189.aspx

Note that you want the entry near the bottom that shows this:

The best that you can do is something like this:
private void Button1_Click(object sender, System.EventArgs e)
{
 string javaScript =
  "<script language=JavaScript>\n" +
  "alert('Button1_Click client-side');\n" +
  "</script>";
 
 RegisterStartupScript("Button1_ClickScript", javaScript); 
}

Open in new window


Do the same in your event.
0
 

Author Comment

by:Rad1
ID: 38832935
I am getting Error      The name 'RegisterStartupScript' does not exist in the current context
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 38832996
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:Rad1
ID: 38833008
By the way, the code in on .ASCX... does it make a difference???
0
 

Author Closing Comment

by:Rad1
ID: 38833119
Thank you!

Also, please note:
string javaScript =
  "<script language=JavaScript>\n" +
  "alert('Button1_Click client-side');\n" +


 "<" + "/script>";
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 38837134
ASCX or ASPX should not make any difference.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question