|
|
14.1 What approaches can users use to add Client-side script code?
|
 |
14.2 How can users use ASP.NET's server side methods to implement client scripts?
|
 |
14.3 How to emit client-side javascript blocks from VB.NET/C#?
|
 |
The RegisterStartupScript method emits the script just before the closing tag of the Page object's <form runat= server> element.
VB.NET
|
RegisterStartupScript("Sample", "<SCRIPT Language='javascript'>alert('Hello World');</SCRIPT>")
|
RegisterStartupScript("Sample", "<SCRIPT Language='javascript'>alert('Hello World');</SCRIPT>");
|
Alternatively, use the RegisterClientScriptBlock method which emits the client-side script just after the opening tag of the Page object's <form runat= server> element.
|
14.4 How to open a new Window using javascript function from a Link button?
|
 |
link.Attributes( "onClick" ) = "window.open( 'url', 'name', 'properties' )";
|
link.Attributes[ "onClick" ] = "window.open( 'url', 'name', 'properties' )";
|
14.5 Is there a JavaScript Quick Reference Guide?
|
 |
14.6 How to set the background color of a web page using code behind?
|
 |
Yes
- In the body tag, add runat="server" and give the tag an id (e.g. id="bodyID").
- In the class definition in the code-behind, add
VB.NET
|
Protected bodyID As System.Web.UI.HtmlControls.HtmlGenericControl
|
protected System.Web.UI.HtmlControls.HtmlGenericControl bodyID ;
|
In code, use the attributes collection to set the bgcolor attribute:
VB.NET
|
bodyID.Attributes.Add("bgcolor", "green")
|
bodyID.Attributes.Add("bgcolor", "green");
|
14.7 How to resolve error message "String constants must end with a double quote."?
|
 |
14.8 Why can't I open a new browser window from within server code?
|
 |
Server code executes on Server, whereas the new window is created on the client. You need to use client-side script to open new window. |
14.9 How to get the confirmation of Yes/No from a javascript pop-up and display the value on the page?
|
 |
Button1.Attributes.Add("onclick", "getMessage()")
|
<SCRIPT language=javascript>
|
ans=window.confirm('Is it your confirmation.....?');
|
document.Form1.hdnbox.value='Yes';
|
document.Form1.hdnbox.value='No';}
|
To display the Yes/No value selected by user, in your code behind file:
|
Response.Write(Request.Form("hdnbox"))
|
14.10 How to open a browser window with maximum size on click of a button?
|
 |
Button1.Attributes.Add("onclick", "window.open('page2.aspx','','fullscreen=yes')")
|
Button1.Attributes.Add("onclick", "window.open('page2.aspx','','fullscreen=yes')");
|
14.11 How to show Modal and Modeless dialog windows in Javascript?
|
 |
When you show a modal dialog the window remains on top of other windows until the user explicitly closes it.
|
window.showModalDialog("Test.html","dialogWidth:400px; dialogHeight:225px; status:no; center:yes");
|
When you show a modeless dialog the window remains on top of other windows, but you can still access the other windows.
|
window.showModelessDialog("Test.html","dialogWidth:400px; dialogHeight:225px; status:no; center:yes");
|
14.12 How can I know if the client browser supports active scripting?
|
 |
You can detect and intercept the capabilities of your client using the namespace System.Web.HttpBrowserCapabilities :
VB.NET
|
Dim browser As System.Web.HttpBrowserCapabilities = Request.Browser
|
Response.Write("Support ActiveXControl: " + browser.ActiveXControls.ToString())
|
System.Web.HttpBrowserCapabilities browser = Request.Browser;
|
Response.Write ("Support ActiveXControl: " + browser.ActiveXControls.ToString ());
|
14.13 How to determine if the Browser supports javascript?
|
 |
if Page.Request.Browser.JavaScript then
|
if (Page.Request.Browser.JavaScript )
|
14.14 How to create files on the client m/c using JavaScript?
|
 |
Here is a technique usable in IE Only.
|
var fso=new ActiveXObject("Scripting.FileSystemObject");
|
var writeStream=fso.CreateTextFile("C:\TestFile.txt",true);
|
writeStream.writeLine("This is a test file");
|
14.15 How to parse the files/folders in the client's file system?
|
 |
Here is a technique usable in IE Only
|
var fso=new ActiveXObject("Scripting.FileSystemObject");
|
function ShowFilesIn(path) // path could be "C:\Test" for example.
|
if(fso.FolderExists(path))
|
ShowFiles(fso.GetFolder(path));
|
function ShowFiles(folderpath)
|
var objFso = new Enumerator(folderpath.Files);
|
for(i=0;!objFso.atEnd();objFso.moveNext())
|
alert(objFso.item().name);
|
14.16 How can i form a regular expression which identifies a valid whole number without decimal places?
|
 |
The regular expression used to identify only the numbers is var regexp = /\D|\./.
|
function IdentifyNumbers()
|
var Number = document.getElementById("NumericValue").value;
|
alert("Only enter numbers and no decimal point\nInvalid input: "+Number.match(regexp));
|
14.17 How to check/ uncheck a checkbox based on the text entered in textbox?
|
 |
<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
|
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
|
<script type="text/javascript">
|
function chkTextEntered()
|
document.getElementById("CheckBox1").checked = true;
|
if(document.getElementById("TextBox1").value =="" )
|
document.getElementById("CheckBox1").checked = false;
|
TextBox1.Attributes.Add("onKeyDown", "chkTextEntered();")
|
TextBox1.Attributes.Add("onKeyDown", "chkTextEntered();");
|
14.18 How to rotate a Label Text?
|
 |
<asp:Label id="Label1" style="writing-mode:tb-rl" runat="server">Label</asp:Label>
|
14.19 How to display a message in the status bar of a browser window?
|
 |
<body onload ="window.status='First Page'">
|
14.20 How to change the BackgroundColor of a page based on the value selected in a DropdownList?
|
 |
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
|
<asp:ListItem Value="Red">Red</asp:ListItem>
|
<asp:ListItem Value="Blue">Blue</asp:ListItem>
|
<asp:ListItem Value="Green">Green</asp:ListItem>
|
Page.RegisterClientScriptBlock("BodyStyle", "<style type='text/css'>body{background-color: " + DropDownList1.SelectedItem.Value + ";}</style>")
|
Page.RegisterClientScriptBlock("BodyStyle", "<style type='text/css'>body{background-color: " + DropDownList1.SelectedItem.Value + ";}</style>");
|
14.21 How to disable a Dropdownlist once someone has selected an item in the Dropdownlist?
|
 |
<asp:DropDownList id="DropDownList1" runat="server">
|
<asp:ListItem Value="Red">Red</asp:ListItem>
|
<asp:ListItem Value="Blue">Blue</asp:ListItem>
|
<asp:ListItem Value="Green">Green</asp:ListItem>
|
DropDownList1.Attributes.Add("onChange","this.disabled=true;" )
|
DropDownList1.Attributes.Add("onChange","this.disabled=true;" );
|
14.22 How can I make a Textbox a mandatory field if a checkbox is checked on a button click event in the client side?
|
 |
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 104px" runat="server"></asp:TextBox>
|
<asp:CheckBox id="CheckBox1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 80px" runat="server"></asp:CheckBox>
|
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 144px" runat="server" Text="Button"></asp:Button>
|
if(document.getElementById ("CheckBox1").checked == true)
|
if(document.getElementById("TextBox1").value == "")
|
alert("Enter something in textbox");
|
Button1.Attributes.Add ("onclick" , "return func1()");
|
Button1.Attributes.Add ("onclick" , "return func1()");
|
14.23 Why does the SmartNavigation does not work on the live server but works perfectly on the Development Machine?
|
 |
May be the domain does not have access to the aspnet_client folder which is located in the wwwroot folder. i.e the website is not able to find the scripts for smart navigation. So set up a virtual folder to the wwwroot/aspnet_client and it will fix the problem. |
14.24 How to pop up a message box when no item in the dropdownlist is selected before postback?
|
 |
- Make sure to add a ListItem with Text="Please Choose" and Value ="".
- Add a RequiredFieldValidator with ControlToValidate= <Dropdownlist1> and Display="Dynamic"
- Add a ValidationSummary with ShowMessageBox =true
|
<asp:DropDownList id="DropDownList1" runat="server">
|
<asp:ListItem Value="">Please Choose</asp:ListItem>
|
<asp:ListItem Value="Faq">Faq</asp:ListItem>
|
<asp:ListItem Value="Tips">Tips</asp:ListItem>
|
<asp:ListItem Value="Tricks">Tricks</asp:ListItem>
|
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 48px"
|
runat="server" ErrorMessage="Please Select an Item in the dropdownlist" ControlToValidate="DropDownList1"
|
Display="Dynamic"></asp:RequiredFieldValidator>
|
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 128px; POSITION: absolute; TOP: 16px" runat="server"
|
Text="Button"></asp:Button>
|
<asp:ValidationSummary id="ValidationSummary1" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 72px"
|
runat="server" ShowMessageBox="True" ShowSummary="False"></asp:ValidationSummary>
|
14.25 How can I use a Timer Control to refresh a page automatically at a specified interval?
|
 |
<asp:DropDownList id="DropDownList1" runat="server" onChange="SetClientRefresh(this);">
|
<asp:ListItem Value="1000">1 second</asp:ListItem>
|
<asp:ListItem Value="2000">2 seconds</asp:ListItem>
|
<asp:ListItem Value="3000">3 seconds</asp:ListItem>
|
<script language='javascript'>
|
function SetClientRefresh(sel)
|
var newRefresh = sel.options[sel.selectedIndex].value;
|
window.clearTimeout(cTimeOut);
|
cTimeOut = window.setTimeout("ReLoadPage()", newRefresh);
|
window.location.reload();
|
14.26 How to open a new window without IE menus and toolbars on click of a button?
|
 |
Button2 .Attributes.Add ("onclick", "window.open('webform1.aspx','_blank','toolbar=no')")
|
Button2 .Attributes.Add ("onclick", "window.open('webform1.aspx','_blank','toolbar=no')");
|
14.27 Does JavaScript support hashtables/ hash tables or dictionary type data structures?
|
 |
All Objects in JavaScript implicitly support hash table like syntax by virtue of behaving as Associative Arrays. Properties of an object can be accessed in 2 ways as shown below:
|
object["property"] = value;
|
So, when used in a hash table like syntax as shown above, you will be simply creating dynamic properties and assigning values to those properties.
|
14.28 How to disable the right click option on a web page?
|
 |
<body oncontextmenu="return false;">
|
Note :User can still do a View/Source in their browser menu. |
14.29 How to hide a control using javascript?
|
 |
document.getElementById("<id>").style.visibility="hidden";
|
14.30 Can I modify WebUIValidation.js?
|
 |
You are encouraged to read the script to see more of what is going on. However, it is not recommended that you modify these scripts, because their function is very closely tied to a particular version of the run time. If the run time is updated, the scripts may need a corresponding update, and you will have to either lose your changes or face problems with the scripts not working. If you must change the scripts for a particular project, take a copy of the files and point your project to them by overriding the location of the files with a private web.config file.It is perfectly fine to change this location to be a relative or absolute reference.
|
14.31 How to change a Label element's text in javascript?
|
 |
document.getElementById("Label1").innerText = "Changed Text";
|
14.32 How to resize two <div> tags on a webform?
|
 |
var DivTop = document.getElementById('Top')
|
var DivBottom = document.getElementById('Bottom')
|
var DivBottomPosition = 0;
|
BodyHeight = document.body.clientHeight;
|
DivBottomHeight = DivBottom.clientHeight;
|
DivBottom.style.top = BodyHeight - DivBottomHeight;
|
DivTop.style.height = DivBottom.style.top;
|
window.onload = ResizeDivs;
|
window.onresize = ResizeDivs;</script>
|
<div id="Top" style="position:absolute; top:0px; left:0px; background-color:#c0c0c0; overflow:auto; width:100%">
|
<div id="Bottom" style="position:absolute; background-color:#808080; width:100%">
|
Note : if the DIV has no borders, clientHeight works. If you are going to be using a border or margins, then use offsetHeight |
14.33 How can I change the scroll bar color?
|
 |
Use Style Sheet to change the color of scroll-bar
|
background-color: #EEEEEE;
|
scrollbar-face-color: #EEEE99;
|
scrollbar-highlight-color: #DDDDDD;
|
scrollbar-shadow-color: #DEE3E7;
|
scrollbar-3dlight-color: #FF6600;
|
scrollbar-arrow-color: #006699;
|
scrollbar-track-color: #EFEFEF;
|
scrollbar-darkshadow-color: #98AAB1;
|
14.34 How to create dynamic javascripts in server side code based on server side variables?
|
 |
Here's and example:
VB.NET
|
Dim value As String = "pic1.jpg"
|
Button1.Attributes("onMouseOver") = "alert( '" + value + "');"
|
string value = "pic1.jpg";
|
Button1.Attributes["onMouseOver"] = "alert( \"" + value + "\");" ;
|
14.35 Is it possible to identify the source webpage where I came from?
|
 |
You can identify the source webpage using the document.referrer command. The document object is the page currently loaded in the browser window - presumably your Web page. The referrer property is the page the visitor was at immediately prior to visiting the current page. You can code it as,
|
functionIdentifySourceWebPage()
|
if(document.referrer != '')
|
document.write('You came from ' + document.referrer);
|
14.36 How can i resize the dialog window?
|
 |
You can size a window with JavaScript using "window.resizeTo(valueX,valueY)".
|
window.resizeTo(800,800);
|
Note also that the parameters of resizeTo() have different meaning in different browsers: in Internet Explorer the parameters specify the outer size of the window, while in Netscape Navigator they refer to the inner size (which does not include the window borders, toolbar, status bar, title bar, and the address line). The "window.resizeBy()" method is slightly different than it's "resizeTo()" which changes window's dimensions to a certain number of pixels from it's current size.
|
14.37 How the browser name can be detected?
|
 |
We can use navigator.appName property, to identify the browser we are using .For Netscape browsers, the value of navigator.appName is "Netscape". For Microsoft Internet Explorer browsers,the value of navigator.appName is "Microsoft Internet Explorer" .
The following code is used for browser detection.
|
document.write('navigator.appName = '+navigator.appName+' ')
|
document.write('navigator.userAgent = '+navigator.userAgent+' ')
|
if ((navigator.userAgent).indexOf("Opera")!=-1)
|
document.write("You are using an Opera browser.")
|
else if (navigator.appName=="Netscape")
|
document.write("You are using a Netscape browser.")
|
else if ((navigator.appName).indexOf("Microsoft")!=-1)
|
document.write("You are using Microsoft Internet Explorer.")
|
14.38 How can we check whether a function exists in javascript?
|
 |
We can check the existence of a function by Window.FunctionName as shown below, where the FunctionName is the name of the function,whose existence has to be checked.
|
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
|
if( window.CheckIfExists)
|
alert( Function Not Exists!' )
|
14.39 How can you generate a clock in javascript?
|
 |
A clock can be generated using the javascript function as given below.
|
<script language="JavaScript">
|
var nhours=thetime.getHours();
|
var nmins=thetime.getMinutes();
|
var nsecn=thetime.getSeconds();
|
var nday=thetime.getDay();
|
var nmonth=thetime.getMonth();
|
var ntoday=thetime.getDate();
|
var nyear=thetime.getYear();
|
if ((nyear>99) && (nyear<2000))
|
document.clockform.clockspot.value=nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear;
|
setTimeout('startclock()',1000);
|
Current Time: <INPUT TYPE="text" name="clockspot" size="40">
|
<script language="JavaScript">
|
14.40 How can we check whether the cookie is enabled?
|
 |
It can be done by checking the read only property navigator.cookieEnabled. navigator.cookieEnabled will give the value true/false based on the browser support for cookies.
|
if(navigator.cookieEnabled)
|
14.41 How can I check whether left or right mouse button has been clicked?
|
 |
To determine whether the user clicked the left or right button, you can use the following properties.
Event.which in Netscape Navigator
event.button in Internet Explorer
If the value of these properties is 1, the event occurred for the left button. In the following example, the onMouseDown event handler displays the messages Left button or Right button, depending on the mouse button you actually have clicked. The messages will appear on your browser's status bar. Click or right-click anywhere on this page to see it work:
|
<script language="JavaScript">
|
if (parseInt(navigator.appVersion)>3)
|
if (navigator.appName=="Netscape") clickType=e.which;
|
else clickType=event.button;
|
self.status='Left button!';
|
self.status='Right button!';
|
if (parseInt(navigator.appVersion)>3)
|
document.onmousedown = mouseDown;
|
if (navigator.appName=="Netscape")
|
document.captureEvents(Event.MOUSEDOWN);
|
14.42 How can you identify the drives available in the System?
|
 |
The drives available in system can be identified using the following code.
|
<SCRIPT language=JavaScript>
|
function ShowAvailableDrives()
|
document.write(GetDriveList());
|
fso = new ActiveXObject("Scripting.FileSystemObject");
|
e = new Enumerator(fso.Drives);
|
if (x.DriveType == 3) n = x.ShareName;
|
else if (x.IsReady) n = x.VolumeName;
|
else n = "[Drive not ready]";
|
<SCRIPT language=JavaScript> ShowAvailableDrives();
|
14.43 How can I add and remove an Html element dynamically using javascript?
|
 |
Below is the javascript code which is used to add and remove div elements dynamically.Here the div tags are added in one base
div tag container.
|
function AddHtmlElement()
|
var divElement = document.getElementById('MYDIV');
|
var divNumber= document.getElementById('hiddenValue');
|
var num = (document.getElementById('hiddenValue').value -1)+ 2;
|
var newdiv = document.createElement('div');
|
var divIdName = 'MYDIV'+num;
|
newdiv.setAttribute('id',divIdName);
|
var divLoadedText='Div '+num+' Is Added!';
|
divElement.appendChild(newdiv);
|
newdiv.innerHTML =divLoadedText+" "+"<a href='#' onclick=RemoveHtmlElement('"+divIdName+"')>Remove div '"+divIdName+"'</a>";
|
functionRemoveHtmlElement(divNum)
|
var divId = document.getElementById('MYDIV');
|
var childId = document.getElementById(divNum);
|
divId.removeChild(childId);
|
<input id="hiddenValue" type="hidden" value="0">
|
<a href="#" onclick="AddHtmlElement()">Add Html Elements</a>
|
14.44 How can you make the text to scroll in the window's status bar?
|
 |
The text can be made to scroll using the javascript codings as below.
|
<title> Scrolling Text On Window Status Bar </title>
|
<script language="javascript">
|
var message = "Syncfusion - .Net Essentials";
|
message += " Text Scrolling In Window Status Bar";
|
for (var c = 0; c < spaceslength; c++)
|
timer = setTimeout('scroll(130)', 500);
|
function scroll(position)
|
if (position > spaceslength)
|
cmd = "scroll(" + position + ")";
|
timer = setTimeout(cmd, delay);
|
else if (position <= spaceslength && position > 0)
|
startMessage = spaces.substring(0, position);
|
cmd = "scroll(" + position + ")";
|
window.status = startMessage;
|
timer = setTimeout(cmd, delay);
|
if (-position < message.length)
|
startMessage += message.substring(-position, message.length);
|
startMessage += spaces.substring(0, spaces.length / 4);
|
cmd = "scroll(" + position + ")";
|
window.status = startMessage;
|
timer = setTimeout(cmd, delay*3);
|
cmd = "scroll(" + spaces.length / 4 + ")";
|
timer = setTimeout(cmd, delay);
|
<body bgcolor="#ffffff" onLoad="init()">
|
<h4>JavaScript Demonstration- Scrolling Text On Window Status Bar</h4>
|
14.45 How can you avoid postback on submit button?
|
 |
The postback on submit button can be avoided by giving return=false in the event handler function as below.
|
<INPUT type="submit" value="Submit" onclick="return false;">
|
14.46 Is there Trim function available in Javascript?
|
 |
There is no predefined trim function available. The function written below is used to Trim white spaces.
|
var whitespace = new String(" \t\n\r");
|
if (whitespace.indexOf(s.charAt(0)) != -1)
|
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
|
var whitespace = new String(" \t\n\r");
|
if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
|
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
|
return RTrim(LTrim(str));
|
14.47 How can you resize the html elements dynamically?
|
 |
Html elements can be resized dynamically by using the style properties. In the below example the TextArea size is
increased dynamically on clicking the button.
|
<script language=javascript>
|
document.form1.TextArea1.style.height='600px';
|
document.form1.TextArea1.style.width='400px';
|
<TextArea style="WIDTH: 200px; HEIGHT: 200px" name="TextArea1"></TextArea>
|
<input type="button" style="WIDTH: 100px; HEIGHT: 100px" name="button1">
|
<input type=button value="ClickMe" onclick=javascript:resize()>
|
14.48 How can you get the X Co-ordinates Y Co-ordinates values during the Mouse Movement?
|
 |
The X and y co-ordinates during the mouse movement can be obtained by the following codings.
|
<script language="Javascript">
|
// Detect if the browser is IE or not.
|
// If the browser is not IE, we assume that the browser is NS.
|
var IE = document.all?true:false
|
// If NetScape then set up for mouse capture
|
if (!IE) document.captureEvents(Event.MOUSEMOVE)
|
// Set-up to use getMouseXY function onMouseMove
|
document.onmousemove = getMouseXY;
|
// Temporary variables to hold mouse x-y pos.s
|
// Main function to retrieve mouse x-y pos.s
|
if (IE) { // grab the x-y pos.s if browser is IE
|
tempX = event.clientX + document.body.scrollLeft;
|
tempY = event.clientY + document.body.scrollTop;
|
// grab the x-y pos.s if browser is NS
|
// catch possible negative values in NS4
|
if (tempX < 0){tempX = 0}
|
if (tempY < 0){tempY = 0}
|
// show the position values in the form named Show
|
// in the text fields named MouseX and MouseY
|
document.Show.X.value = tempX;
|
document.Show.Y.value = tempY;
|
14.49 How can you make a piece of text get moving along the mouse Movement?
|
 |
We can make text to move along the mouse using javascript as given below,
|
var text="Syncfusion .NET Essentials "
|
for (i=0;i<=text.length-1;i++)
|
for (i=0;i<=text.length-1;i++)
|
x = (document.layers) ? e.pageX : document.body.scrollLeft+event.clientX
|
y = (document.layers) ? e.pageY : document.body.scrollTop+event.clientY
|
if (flag==1 && document.all)
|
for (i=text.length-1; i>=1; i--)
|
var spanValue = eval("span"+(i)+".style")
|
spanValue.posLeft=xpos[i]
|
else if (flag==1 && document.layers)
|
for (i=text.length-1; i>=1; i--)
|
var spanValue = eval("document.span"+i)
|
var timer=setTimeout("MoveText()",30)
|
<body onLoad="MoveText()" style="width:100%;overflow-x:hidden;overflow-y:scroll">
|
for (i=0;i<=text.length-1;i++)
|
document.captureEvents(Event.MOUSEMOVE);
|
document.onmousemove = handlerMM;
|
14.50 Can I access the query string using JavaScript?
|
 |
A query string is an optional part of a URL that goes after the file name and begins with a questionmark.
For eg the query string will be as
http://www.sitedemo/com/home.htm?newquerystringadded.
Here querystringvalue is the newquerystringadded.
The query string can be added and it can be retrieved as shown,
|
<input type=button value="AddQueryString"
|
self.location.protocol+'//'
|
+self.location.pathname+'?addquerystring'">
|
<input type=button value="ShowQueryString"
|
onClick="alert('Query string Value: '+self.location.search)">
|
14.51 How can the 'Enter' key be disabled?
|
 |
In order to avoid the Form Submission by pressing the 'Enter Key' you have to write following code on the KeyPress Event Of the Body Tag
|
function DisableEnterKey()
|
if (window.event.keyCode == 13)
|
14.52 How can you do ClientSide Validation?
|
 |
Client-side validation is enabled by default. If the client is capable, uplevel validation will be performed automatically. To disable client-side validation, set the page's ClientTarget property to "Downlevel" ("Uplevel" forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to "false" to disable client-side validation for that specific control.
|
<%@ Page ClientTarget=UpLevel %>
|
<script language="C#" runat=server>
|
// Validate initially to force *s to appear before the first round-trip
|
void ValidateBtn_Click(Object Sender, EventArgs E)
|
if (Page.IsValid == true)
|
lblOutput.Text = "Page is Valid!";
|
lblOutput.Text = "Some of the required fields are empty";
|
<font face="Verdana">Client-Side RequiredFieldValidator Sample</font>
|
<table bgcolor="#eeeeee" cellpadding=10>
|
<asp:Label ID="lblOutput" Name="lblOutput" Text="Fill in the required fields below"
|
ForeColor="red" Font-Names="Verdana" Font-Size="10" runat=server />
|
<font face=Verdana size=2>Credit Card Information</font>
|
<font face=Verdana size=2>Card Type:</font>
|
<ASP:RadioButtonList id=RadioButtonList1 RepeatLayout="Flow"
|
onclick="ClientOnChange();" runat=server>
|
<asp:ListItem>MasterCard</asp:ListItem>
|
<asp:ListItem>Visa</asp:ListItem>
|
<td align=middle rowspan=1>
|
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
|
ControlToValidate="RadioButtonList1"
|
</asp:RequiredFieldValidator>
|
<font face=Verdana size=2>Card Number:</font>
|
<ASP:TextBox id=TextBox1 onchange="ClientOnChange();" runat=server />
|
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server"
|
ControlToValidate="TextBox1"
|
</asp:RequiredFieldValidator>
|
<font face=Verdana size=2>Expiration Date:</font>
|
<ASP:DropDownList id=DropDownList1 onchange="ClientOnChange();"
|
<asp:ListItem></asp:ListItem>
|
<asp:ListItem >06/00</asp:ListItem>
|
<asp:ListItem >07/00</asp:ListItem>
|
<asp:ListItem >08/00</asp:ListItem>
|
<asp:ListItem >09/00</asp:ListItem>
|
<asp:ListItem >10/00</asp:ListItem>
|
<asp:ListItem >11/00</asp:ListItem>
|
<asp:ListItem >01/01</asp:ListItem>
|
<asp:ListItem >02/01</asp:ListItem>
|
<asp:ListItem >03/01</asp:ListItem>
|
<asp:ListItem <04/01</asp:ListItem>
|
<asp:ListItem <05/01</asp:ListItem>
|
<asp:ListItem <06/01</asp:ListItem>
|
<asp:ListItem <07/01</asp:ListItem>
|
<asp:ListItem <08/01</asp:ListItem>
|
<asp:ListItem <09/01</asp:ListItem>
|
<asp:ListItem <10/01</asp:ListItem>
|
<asp:ListItem <11/01</asp:ListItem>
|
<asp:ListItem <12/01</asp:ListItem>
|
<asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server"
|
ControlToValidate="DropDownList1"
|
</asp:RequiredFieldValidator>
|
<ASP:Button id=Button1 text="Validate" OnClick="ValidateBtn_Click"
|
<script language=javascript>
|
function ClientOnChange()
|
if (typeof(Page_Validators) == "undefined")
|
document.all["lblOutput"].innerText = Page_IsValid ? "Page is Valid!" : "Some of
|
the required fields are empty";
|
14.53 How can the a client side function be called on a button click event?
|
 |
In 2.0 Button control has a property OnClientClick which is used to call a client side function before postback.
|
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ClientFunction()"/>
|
alert("ClientFunctionCalled");
|
14.54 Is it possible to identify the source web page where I came from?
|
 |
You can identify the source webpage using the document.referrer command. The document object is the page currently loaded in the browser window, presumably your Web page. The referrer property is the page the visitor was at immediately prior to visiting the current page. You can code it as,
|
function IdentifySourceWebPage()
|
if(document.referrer != '')
|
document.write('You came from ' + document.referrer);
|
14.55 How can I resize the window?
|
 |
You can resize a window with JavaScript using "window.resizeTo(valueX,valueY)".
|
window.resizeTo(800,800);
|
Note also that the parameters of resizeTo() have different meaning in different browsers. In Internet Explorer the parameters specify the outer size of the window, while in Netscape Navigator they refer to the inner size (which does not include the window borders, toolbar, status bar, title bar, and the address line). The "window.resizeBy()" method is slightly different than it's "resizeTo()" which changes the windows dimensions by number of pixels from it's current size.
|
14.56 Are there any resources regarding the Mozilla specific Browser Objects and CSS information?
|
 |
|
|
|
|