Solved

Adding Javascript Function to Button Click in addition to Function already called

Posted on 2008-09-30
4
903 Views
Last Modified: 2013-11-07
Greetings All!

I have a webform that has a button which when clicked calls a C# function to do a bunch of stuff.  That all works great.  I now want to add a simple Javascript alert in addition to the C# function being called whenever the button is clicked.   Originally I wanted to just to the Javascript alert from within the C# function but this always interferes with the final page destination (through a previous Question I tried many different alternatives with no luck).  

So with my webform can I tell the Button to do a Javascript function in addition to calling the C# function?   Currently the C# function call is set up by naming it in the Click field of the Properties box for the Button.

Many thanks for any ideas on this!
0
Comment
Question by:andremara
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22611946
use button's OnClientClick property to specify Client Function
0
 

Author Comment

by:andremara
ID: 22614153
Thanks....it seems that OnClientClick is not available in .NET 1.1 possibly only in 2.0 or above.  Does that make sense?  

I tried just using the OnClick function but am getting an error with that when I run this page...it says it can't find the function I'm specificying with OnClick.

I've attached a code segment (actually the whole aspx page..its short)...can you see where I'm going wrong?  

Many thanks!


<%@ Page language="c#" Codebehind="EmailWeeklyReports.aspx.cs" AutoEventWireup="false" Inherits="ReportParamObjEmailWeeklyReports.WebFormAsOfDate" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>WebFormInvoicing</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="C#" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
		<SCRIPT LANGUAGE="JavaScript">
 
<!-- Begin
function popUp(URL) {
 
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=100,height=100,left = 462,top = 334');");
}
 
function testalert()
{
  return alert('Hello!');
} 
 
 
// End -->
		</SCRIPT>
	</HEAD>
	
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<asp:button id="btnContinue" style="Z-INDEX: 107; LEFT: 264px; POSITION: absolute; TOP: 304px" OnClick="testalert()"
				runat="server" Width="176px" Height="32px" ToolTip="Click to Continue..." Text="EMAIL WEEKLY REPORTS"></asp:button>
			<asp:label id="lblAsOfReport" style="Z-INDEX: 105; LEFT: 168px; POSITION: absolute; TOP: 48px"
				runat="server" Width="344px" Height="48px" ToolTip="Select Clients to Email Reports To..."
				Font-Size="Large" Font-Underline="True">Select Clients to Email Reports To</asp:label><asp:button id="ButtonGoBack" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 8px"
				runat="server" Width="112px" Text="Back to Application" Font-Size="XX-Small"></asp:button>
			<asp:checkbox id="Client1" style="Z-INDEX: 111; LEFT: 296px; POSITION: absolute; TOP: 128px" Text="Client1"
				Runat="server"></asp:checkbox>
			<asp:checkbox id="Client2" style="Z-INDEX: 110; LEFT: 296px; POSITION: absolute; TOP: 160px" Text="Client2"
				Runat="server"></asp:checkbox>
			<asp:checkbox id="Client3" style="Z-INDEX: 106; LEFT: 296px; POSITION: absolute; TOP: 192px" Text="Client3"
				Runat="server"></asp:checkbox>
			<asp:checkbox id="Client4" style="Z-INDEX: 112; LEFT: 296px; POSITION: absolute; TOP: 224px" Text="Client4"
				Runat="server"></asp:checkbox>
		</form>
	</body>
</HTML>

Open in new window

0
 
LVL 14

Accepted Solution

by:
Binuth earned 250 total points
ID: 22647248
use Attributes.Add property

protected void Page_Load(object sender , EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				Button1.Attributes.Add("onclick" , "testalert()");
			}
		}

Open in new window

0
 

Author Closing Comment

by:andremara
ID: 31501633
Thank you.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...))The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all current…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

737 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