![]() |
Web Development »
ASP.NET »
Samples
Intermediate
License: The Code Project Open License (CPOL)
Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side ScriptBy santosh poojariThis article helps a learner to know more about the different ways of calling client side JavaScript from server side code-behind. |
C#, Javascript.NET 1.1, Win2K, WinXP, ASP.NET, VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
One can find this article useful during the development phase of a web application. One requires functionalities such as calling JavaScript client side code for validation, confirming popup, prompting user for unwanted action, etc. at server side code behind.
So we start with an example through code.
Using RegisterStartupScript we can write a JavaScript function in code behind and call it from code-behind or from HTML. Look at the code below for reference.
private void Page_Load(object sender, System.EventArgs e)
{
string jScriptValidator;
jScriptValidator="<script> function ReqFieldValidator()" +
" { if (document.forms[0].txtField.value == '') \n";
jScriptValidator+="{ alert('TextBox cannot be empty') \n ";
jScriptValidator+="return false; \n";
jScriptValidator+="} \n";
jScriptValidator+=" return true \n";
jScriptValidator+=" } </script>";
Page.RegisterStartupScript("regJSval",jScriptValidator);
btnSubmit.Attributes.Add("onclick","return ReqFieldValidator()");
}
//Server side
private void Page_Load(object sender, System.EventArgs e)
{
string jScript;
jScript="<script>function JavScriptFn(){alert" +
" ('Client Function in javascript is call')}</script>";
}
//HTML side
< A onclick="JavScriptFn()" >
< asp:Label id="Label1" runat="server" Width="281px"
ForeColor="#8080FF">Click to call Javascript function.
</asp:Label> >/A >
<Head >
<script>
function ReqField1Validator()
{
if (document.forms[0].txtField1.value == '')
{
alert('TextBox cannot be empty')
return false
}
return true
}
</script>
</Head >
private void Page_Load(object sender, System.EventArgs e)
{
btnOK.Attributes.Add("onclick","return ReqField1Validator()");
}
Suppose we want JavaScript code to be executed but not a function. In that case, we make use of RegisterClientScriptBlock.RegisterClientScriptBlock which helps to make server side code as well as client side code inline to each other.
private void btnClientBlock_Click(object sender, System.EventArgs e)
{
string jScript;
jScript="<script>alert ('Javascript block of code executed')</script>";
Page.RegisterClientScriptBlock("keyClientBlock",jScript);
lblSequencial.Text="Remaining part of the code executed";
}
One can call JavaScript client side function directly from code behind using RegisterStartupScript. If one wants JavaScript to be embedded into codebehind, then make use of RegisterClientScriptBlock:
<script>
function fnShowMessage()
{
alert(" Invoke Javascript function from Server Side Code Behind ");
}
</script>
protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript
(GetType(),"Javascript", "javascript: fnShowMessage(); ",true);
}
protected void Button2_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(GetType(), "Javascript",
"<script>alert('Record Added Successfully')</script>");
}
Hope you find the above article helpful. Any suggestions are most welcome.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Mar 2009 Editor: Deeksha Shenoy |
Copyright 2005 by santosh poojari Everything else Copyright © CodeProject, 1999-2009 Web13 | Advertise on the Code Project |