using JavaScript - basic question

G

Guest

I've used C# to register a javascript function in the PageLoad event of a web
form page. My problem is that I can't seem to figure out how to call it.
I've tried the following, all to no avail:

<a href="javascript: myscript()">click here</a>
<asp:hyperlink id="hplnk1" runat="server" target="javascript:
myscript()">click here</asp:hyperlink>
<asp:linkbutton id="lnkbtn1" runat="server" onclick="javascript:
myscript()">click here</asp:linkbutton>

here's the function (simplified for the sake of saving space):

<script language='javascript'>
function myscript()
{
alert('You have clicked the link')
}
</script>

Can anyone please show me what I'm doing wrong? Thanks
 
J

John Timney \(ASP.NET MVP\)

Theres a little example below to get you started. It uses a button but the
event is the same.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director



<html>
<head>
<script language="C#" runat="server">

void Page_Load( Object sender , EventArgs e )
{

//Form the script that is to be registered at client side.
string ScriptString = "<script language=JavaScript> function
DoClick() {";

ScriptString += "var truthBeTold = window.confirm('Click OK to
continue. Click Cancel to stop.');";
ScriptString += "if (truthBeTold)";
ScriptString += "window.alert('Welcome to MVP World!');";
ScriptString += "else window.alert('Bye from MVP World!');}<";
ScriptString += "/";
ScriptString += "script>";

if(! IsClientScriptBlockRegistered("clientScript"))
{
RegisterClientScriptBlock("clientScript", ScriptString);
}
}

</script>
</head>
<body topmargin="20" leftmargin="10">
<form id="myForm" runat="server">
<input type="button" value="ClickMe" onclick="DoClick()">
</form>
</body>
</html>
 

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

Top