.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to call a javascript function in dot net code?

Posted By: jay     Posted Date: February 18, 2010    Points:2   Category :ASP.Net
how to call a javascript function in dot net code. i want to call a javascript function on click of a button.... in C#

Responses
Author: Syed Shakeer Hussain             
Posted Date: February 18, 2010     Points: 5   

Hi jay
Displaying a Message Box in Asp.net using JavaScript.

1)Alert Message Box :- This type of Message Box is used to give Alert Information to the User.This can be done by using 'alert' method.Use the Alert mehtod in <script> </script> tags by using a function with method name.Call this function On 'Clientclick event in Button as follows in Below code.

eg:alert("write Message here")
<html>
<head runat="server">
<title>Untitled Page</title>
<script type="text/Javascript" language ="javascript" >
function alert_meth()
{
alert("Client Side MessageBox");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Alert" OnClientClick ="alert_meth()" />
</form>
</body>
</html>


Author: jay             
Posted Date: February 18, 2010     Points: 5   

ur right but how to call this code in button1_click event in a inline code of C#

Author: Lalij Mer          Accepted Answer   
Posted Date: February 19, 2010     Points: 10   

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);
}


OR


Set the Body tag in your page/master page to runat="server" and an id="MainBody".

Put the script tag in your page like you normally would with any HTML file
<script src="popup.js"  type="text/javascript"></script>

Then in the code behind attach the onload attribute to the body tag .

protected void Page_Load(object sender, EventArgs e)
{
MasterPageBodyTag = (HtmlGenericControl)Page.Master.FindControl("MainBody");
MasterPageBodyTag.Attributes.Add("Onload", "Popup()");
}


Please Check my answer if you help....
Thank You...

Lalit.
Author: Syed Shakeer Hussain             
Posted Date: February 19, 2010     Points: 5   

Hi,
Write the Below code in a Button and click the button you will get a Alert Message Box with a Message as 'Hello World'.


Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SCRIPT", string.Format("alert('Hello World')"), true);


Author: abhay             
Posted Date: March 04, 2010     Points: 5   

ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>Bindcomment();</script>");

Bindcomment is the javscript function



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend