C# .NET - How to Java script in User Control
Asked By sudhakar subramaniyan
18-Jul-08 05:28 AM
How can use java scrpt in user control
try this...
there is a special way to embed javascript in usercontrols because the usercontrol and the page which consumes the usercontrols get one unit. So one way you can solve your problem is to write you javascript into the page OR you create the javascript dynamically via code:
Small example:
StringBuilder strBld = new StringBuilder();
strBld.AppendLine("function endScript(){");
strBld.AppendLine("alert('Come visit us again');");
strBld.AppendLine("}");
ClientScript.RegisterClientScriptBlock(this.GetType(), "endScript", strBld.ToString(), true);
Solution How to Java script in User Control
this is one method
usrctrl.Attributes.Add("Onclick", "javascript:alert('test')");
Javascript in User Control
For simple stuff, you can use it just as you would in a normal .aspx page. If you need to reference a server control in Javascript, you can use the Control's
ClientID property.
Check this article for insight on how to use Javascript in a user control -
http://www.codeproject.com/KB/user-controls/ASPNET_DateSelector.aspx
very simple way to use javascript in user control
<uc1:userJs id="UserJs1" runat="server" uCtrlId="UserJs1"> </uc1:userJs><select name="UserJs1:ddlItem" id="UserJs1_ddlItem"
onchange="return CodeBehindDDlJs();" style="width:120px;" >Dim strScript2 As String = "< script language=" & _
"JavaScript > function CodeBehindDDlJs(me) {"
strScript2 += "document.forms[0]." & uCtrlId & "_btnSubmit.focus();}"
strScript2 += "alert('You selected '+ me.value)}"
strScript2 += "<"
strScript2 += "/"
strScript2 += "script>"
If (Not Page.IsStartupScriptRegistered("Startup1")) Then
Page.RegisterStartupScript("Startup1", strScript1)
End If
If (Not Page.IsStartupScriptRegistered("Startup2")) Then
Page.RegisterStartupScript("Startup2", strScript2)
End If
ddlItem.Attributes.Add("onchange", "return CodeBehindDDlJs(this);")
You can do this
What you can do is to create a .js file, place all your javascript
functions inside it. then call you javascript functions from your
usercontrol. When u place the user control on a webform, add the link
to that js file inside the webform hosting the user control
so in the <head> tag of your webform place the below code,
which will link the js file into the webform, in this way the user
control inside that webform can use the javascript functions in it.
<script src="UC.js" type="text/javascript"></script>
hi

the following code is the example of JavaScript Validation in User Control
Go through this link:
http://www.codeproject.com/KB/aspnet/Javascript_in_Usercontrol.aspx
Dim strScript1 As String = "< script language" & _
"=JavaScript > function CodeBehindButtonJs() {"
strScript1 += "if (document.forms[0]." & uCtrlId & _
"_ddlItem.selectedIndex == 0){alert('Validation:" & _
"Please Select Item');return false;};}"
strScript1 += "<"
strScript1 += "/"
strScript1 += "script >"
Dim strScript2 As String = "< script language=" & _
"JavaScript > function CodeBehindDDlJs(me) {"
strScript2 += "document.forms[0]." & uCtrlId & "_btnSubmit.focus();}"
strScript2 += "alert('You selected '+ me.value)}"
strScript2 += "<"
strScript2 += "/"
strScript2 += "script>"
If (Not Page.IsStartupScriptRegistered("Startup1")) Then
Page.RegisterStartupScript("Startup1", strScript1)
End If
If (Not Page.IsStartupScriptRegistered("Startup2")) Then
Page.RegisterStartupScript("Startup2", strScript2)
End If
ddlItem.Attributes.Add("onchange", "return CodeBehindDDlJs(this);")
btnSubmit.Attributes.Add("onClick", "return CodeBehindButtonJs();")
Java script in User Control
<%
@ Control Language="c#" AutoEventWireup="false" Codebehind="ChangePassword.ascx.cs" Inherits="Novel_HRM.Modules.PIM.ChangePassword" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<
script language="javascript" src="./ValidationFile.js" type="text/javascript"></script>
<
script language="javascript" type="text/javascript">
var
rules=new Array();
debugger
;
rules.push(
"Alphanumeric,ChangePassword1_txtOldPassword,Enter your oldpassword.");
rules.push(
"Alphanumeric,ChangePassword1_txtNewPassowrd,Enter your new password.");
rules.push(
"Alphanumeric,ChangePassword1_txtConfirmPassword,Enter your confirmpassword.");
rules.push(
"same_as,ChangePassword1_txtConfirmPassword,Your Password Doesn't Match Enter Similar Password.");
</
script>

please let me know. I am using a javascript function which is present on Master page. but i want to call this function from another aspx form(contentpage). So please let 0);"); or you can break it like this to check and makesure that the master page is of type MyMaster MyMaster CurrentMasterPage = this.Master as MyMaster; if(CurrentMasterPage! = null) { CurrentMaster.Body http: / / forums.asp.net / p / 1680117 / 4415472.aspx / 1?Re+Calling+javascript+function+from+content+page+in+master+page http: / / www.dotnetcurry.com / ShowArticle.aspx?ID = 273 In this article, we will see some common problems and their solutions of calling JavaScript from a Master / Content page. This article is Part II of the series ‘Calling JavaScript from ASP.NET Master Page and Content Pages’ and in this article; we will cover JavaScript in Content Pages. Part I of this article can be read over here Calling JavaScript from ASP.NET Master Page and Content Pages - Part I Call JavaScript from Content Page Here are some common scenarios of executing JavaScript from a Content Page. 1. Create a
ASP.NET AJAX Maintain Scroll Position from a Partial Page Update How to Maintain Scroll Position from a Partial Page Update - Using Ajax. On and off for a while now I have worked with the WebForms.PageRequestManager endRequest Event , and of course the ScriptManager that has to be on every page that a UpdatePanel is on (Or if you are using a masterpage you may use visibility = 'hidden' ; } < / script > - -- -- -- -- -- -- -- -- -- -- -- -- - If you are thinking to yourself, "WOW that looks alot like the Page Model in .net." Well, my dear Watson you would be correct. I am not going Example: http: / / www.eggheadcafe.com / fileupload / 215296388_UpdatePanel-EndRequest.zip Ever wonder where ajax and parcel page updates fit into the page lifecycle? Check this out - -- http: / / techblog.muthuka.com / wp-content / uploads / 2006 / 12 / beantown_ajax.ppt#302, 17, ASP.NET Page Life Cycle Popularity ( 18348 Views ) View erik little's Articles Picture Biography - erik little Have opinion Create New Account Article Discussion: Shows how to Maintain Scroll Position from a Partial Page Update - Using Ajax erik little posted at Saturday, February 03, 2007 12:45 PM Original
what is the difference between registerclientscriptblock and registers what is the difference between registerclientscriptblock and registerstartupscript RegisterClientScriptBlock inserts the script immediately following the <form> start tag whereas RegisterStartupScript adds the script immediately before the < / form> end tag. In IE the HTML DOM is blocks) your controls such as textboxes etc in your form will be created, then your RegisterStartupScript will be parsed (and executed if not in function blocks). So, in IE at least elements will exist for your scripts to access if you put the code in the RegisterStartupScript. Conversely, you are basically guaranteed that any form elements will not exist for your scripts javascript / "> " + "function a(){window.alert( document.form[0].txtElement.value );}" + "< / script> " ); Code Snip-it 3: RegisterStartupScript( "CS3", "<script language = / "javascript / "> " + "window.alert( document.form[0].txtElement.value );" + "< / script> " ); Code Snip-it
Page.RegisterStartupScript Problem This code is what I use to redirect a page using javascript, and this is how I execute it in VB Page.RegisterStartupScript( "SCRIPTNAME" , "<script language = 'javascript'> ConfirmSave();< / script> " ) It work properly, but whtn I presse the back button in IE of the redirected page back to my main page, the confirm box reappear, the one that the function ConfirmSave() does. Please help me on back button then browser again show previous output, because of that you are getting same page with dialog, to solve this problem clear the history- I provide you with solutions to