http://www.eggheadcafe.com/articles/20041016.asp


      
    <html>	 
     <TITLE>ASP.NET Client-Side Script FAQ</TITLE>
	 <HEAD>
   
       <link rel="alternate" type="application/rss+xml" title="Eggheadcafe.com Main Feed" href="http://www.eggheadcafe.com/rss.xml"> 
	     <script src="/common.js" type="text/javascript"></script>
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
    <script type="text/javascript">
       _uacct = "UA-142061-1";
       urchinTracker();
    </script>

     
 
	  <SCRIPT LANGUAGE="JavaScript" src="/behaviors/HiLite.js" ></script>

     <SCRIPT LANGUAGE="JavaScript1.2"> 

       var  baseURL = 'http://www.eggheadcafe.com'; 

       try
       {
           if (self.parent.frames.length != 0)
	          self.parent.location=document.location;
	   }
   	   catch (Exception) {}

       function PrintThisPage()
	   {
	      var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
              sOption+="scrollbars=yes,width=750,height=650,left=100,top=25";
                      					                 
              var winprint=window.open("/print.asp","Print",sOption);
                            
              winprint.focus();
 
	   }
              
     </SCRIPT> 
  
     <META content="7" name="revisit-after">
     <META http-equiv=Content-Type content='text/html; charset=utf-8'>
     <META content='MSHTML 5.50.4134.600' name=GENERATOR>
 
<style type="text/css">
 
 
	br.short	{ line-height: 4px; }
	.bright		{ font-weight: bold; color: #FF7800; }	
.bright9	{ font-weight: bold; color: #FFFFFF; font-size: 8pt; font-family: Arial}	
 



	body,td		{ font-family: arial, helvetica; font-size: 11px; font-color: #444444; }
	b		{ color: #333333; }
	.sidelink	{ font-family: verdana, arial, helvetica; color: #666666; font-size: 10px; letter-spacing: -1px; }
	.carea		{ color: #777777; line-height: 20px; }
	A:visited	{ text-decoration: none; }
	A:link		{ text-decoration: none; }
	A:hover		{ text-decoration: underline; }
	.bottom		{ color: #EDEDED; }
	a.bottom	{ font-weight: bold; }
 
</style>

  <LINK href=/eggheadcafe.css rel=Stylesheet>
  <link href="/stylesheet.css" rel="Stylesheet">

  </HEAD>
   
   
<body   leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="background-color:#FFFFFF">


 
 
 
   <table width="100%"  border="0" cellpadding="0" cellspacing="0">
   	<tr>
		<td width="339"><a href="/default.aspx"><img src="/graphics/index01.jpg" border="0"></a></td>
		<td width="90"><a href="/topicareas.aspx"><img src="/graphics/b1.jpg" border="0"></a></td>
		<td width="90"><a href="/login.aspx"><img src="/graphics/b2.jpg" border="0"></a></td>
		<td width="90"><a href="/aboutus.aspx"><img src="/graphics/b3.jpg" border="0"></a></td>
		<td width="90"><a href="/contactus.aspx"><img src="/graphics/b4.jpg" border="0"></a></td>			
		<td width="90"><a href="/forum.aspx"><img src="/graphics/b5.jpg" border="0"></a></td>				
 	    <td width="90"><a href="/userurls.aspx"><img src="/graphics/b6.jpg" border="0"></a></td>
		<td width="90" ><a href="/signout.aspx"><img src="/graphics/b7.jpg" border="0"></a></td>					
		<td style="background:#FFFFFF url('/graphics/index08.jpg') repeat-x; "><img src="/graphics/index07.jpg" border="0"></td>
	</tr>
</table>

<table width="100%"  border="0" cellpadding="0" cellspacing="0">
<tr>

 <td align="left" valign="top" width="14%">

    <br/>
	<br>
   
<table border="0" cellspacing="0" cellpadding="2" width="100%">
 
<tr><td align="left" colspan="3">&nbsp;Search</td></tr>
<tr><td align="left" colspan="3"><input type="textbox" ID="searchRelatedText" Value="" style="color:#4b7c9f;font-weight:bold;border-width:1px 0px 1px 1px;border-style:solid none solid solid;border-left-color:#4b7c9f;border-top-color:#4b7c9f;border-bottom-color:#4b7c9f;padding:0px 0px 0;height:20px;width:100px;vertical-align:top;background-position:right;background-repeat:no-repeat;background-color:White" onkeydown="return searchRelatedClassicMaster(event)"><a href="javascript:doSearchClassicMaster();" ID="SearchIconButton" ><img src="/graphics/searchicon.gif" border="0"></a></td></tr>
 
 <tr><td align="left" colspan="3">&nbsp;<br></td></tr>

 

<tr>
    <td align="right"><img src="/graphics/folder.gif" border="0"></td>
	<td align="left" class="sideHeading" colspan="2">Messageboard</td>
</tr>

<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumrecent.aspx"  class="sidelink">New Posts</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forummyposts.aspx"  class="sidelink">My Posts</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=12"  class="sidelink">Dr. Dotnetsky</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=2"  class="sidelink">C#</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=14" class="sidelink">VB.NET</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=7" class="sidelink">VS.NET</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=6" class="sidelink">ASP.NET 1.1</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=17" class="sidelink">ASP.NET 2.0</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=3" class="sidelink">JavaScript</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=9" class="sidelink">SQL 2000</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=13"  class="sidelink">SQL 2005</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/forumtree.aspx?topicid=8"  class="sidelink">VB 6.0</a></td>
</tr>

 

<tr>
    <td align="right"><img src="/graphics/folder.gif" border="0" alt="Member Details"></td>
	<td align="left" class="sideHeading" colspan="2">Community</td>
</tr>

<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationgroupsummary.aspx" class="sidelink">All Categories</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=454" class="sidelink" target="_self">.NET</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=103" class="sidelink" target="_self">BizTalk</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=602" class="sidelink" target="_self">Exchange</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=929" class="sidelink" target="_self">IIS</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=2298" class="sidelink" target="_self">IE</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=963" class="sidelink" target="_self">ISA Server</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1" class="sidelink" target="_self">Misc</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1570" class="sidelink" target="_self">Mobile Dev</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1291" class="sidelink" target="_self">MSN</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1375" class="sidelink" target="_self">Networking</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1487" class="sidelink" target="_self">Office</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=2475" class="sidelink" target="_self">SharePoint</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1731" class="sidelink" target="_self">SQL Server</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1705" class="sidelink" target="_self">Mgmt Server</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1996" class="sidelink" target="_self">Win32</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1380" class="sidelink" target="_self">Windows 2003</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1389" class="sidelink" target="_self">Windows Media</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=2168" class="sidelink" target="_self">Windows NT</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=2412" class="sidelink" target="_self">Windows Vista</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=1404" class="sidelink" target="_self">Windows XP</a></td>
</tr>
<tr>
    <td align="right"></td>
	<td align="left"><img src="/graphics/technotes.gif" border="0"></td>
	<td align="left"><a href="/conversationlist.aspx?groupid=2053" class="sidelink" target="_self">WinFX</a></td>
</tr>
 
</table>
</td>
 
 <td width="40px">&nbsp;</td>
   <td align="center" valign="top" width="70%">
 
      <table    border="0" cellpadding="0" cellspacing="4">
       <tr><td align="center" ><div id="Card_Search" style="visibility:hidden;background-color:#FFFFFF;position:relative;BORDER-RIGHT: 0px outset #FFFFFF; BORDER-TOP:0px outset #FFFFFF; BORDER-LEFT: 0px outset #FFFFFF; BORDER-BOTTOM: 0px outset #FFFFFF;HEIGHT:5px; OVERFLOW: auto; scrollbar-base-color: #EFEFEF; scrollbar-arrow-color: #FFFFFF; scrollbar-DarkShadow-Color: #FFFFFF; scrollbar-Track-Color: #FFFFFF; scrollbar-highlight-color:#CCCCCC">&nbsp;</div><DIV id=contentstart name=contentstart align=center width="100%"><!-- google_ad_section_start -->
  
 

       

 
 
<!--startPF-->
<Style>
 <!--
.clsBodyText
{PADDING-RIGHT: 4px;
	PADDING-LEFT: 4px;
	FONT-WEIGHT: normal;
	FONT-SIZE: 11px;
	PADDING-BOTTOM: 4px;
	OVERFLOW: auto;
	PADDING-TOP: 4px;
	FONT-FAMILY: Verdana;
	text-align: Justify;
	font-style: normal;
	color: #000000;}
PRE.clsCode, CODE.clsText { font-size:110%; font-family:'courier new',courier,serif; white-space:normal;word-wrap:break-word;}

PRE.clsCCCode { font-size:120%; font-family:'courier new',courier,serif;background:none }

PRE.clsSyntax { font-size:100%; font-family:verdana,arial,helvetica,sans-serif; }


-->
 </style>
<style>
<!--
@media screen {
	pab\:PULLQUOTE {behavior:url(../behaviors/PULLQUOTE.htc)}
	.pullstyle {width: 200;
		color:black;
		text-align: left;
		border-color:#9966cc;
		border-width:thin;
		border-style:solid;
		border-right: none;
		border-left: none;
		padding: 1em;
		margin: 6pt;
		font-family: arial;
		font-style: italic;
		font-size: 12pt;
	}
}
.style4 {font-family: Verdana, Arial, Helvetica, sans-serif}
.Heading {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
	font-weight: bold;
}

PRE
{
	background:#EEEEEE;
	margin-top:1em; margin-bottom:1em; margin-left:0px;
	padding:5pt;
	white-space:normal;
	word-wrap:break-word;
}


SPAN.db PRE, SPAN.ci PRE, SPAN.di PRE
{
	background:#EEEEEE;
	padding-top: 0pt;
	padding-bottom: 0pt;
}
.style5 {color: #0000FF}
.style6 {color: #804040}
.style7 {color: #800000}
.style8 {color: #000000}


-->
</style>
 
<body bgcolor="#FFFFFF"> 
<table   border="0" align=center width="725" > 
  <TR> 
    <TD height="136" align=center  class="Normal"> <p> <font size="3" face="Verdana, Arial, Helvetica, sans-serif"><b>   ASP.NET
            Client-Side Script FAQ <br> 
        </b></font><font face="Verdana, Arial, Helvetica, sans-serif">(Various
        contributors) </font><font face="Verdana, Arial, Helvetica, sans-serif"><br> 
        </font><font face="Verdana, Arial, Helvetica, sans-serif"><br> 
    </font></p></TD> 
  </TR> 
</table> 
<table border=0 align=center cellpadding=1 cellspacing=1 width=675> 
  <tr> 
    <td align=left class="clsBodyText"  width=675> <p align="center">
	</p>
	  <p>We have a fair amount of forum questions here on Eggheadcafe.com that center
	  around the area of <strong>client-side scripting</strong> with ASP.NET, and
	  we thought it would be about time to start an FAQ article on the subject.
	  We'll include all the basics of client - side script with ASP.NET here,
	  drawn from reliable sources (and simplified!) along with our own
	  &quot;discoveries&quot;. We'll also insert links to other resources where
	  appropriate. This is a community &quot;work in progress&quot; which we will
	  add to on a regular basis, so please feel free to take advantage of the
	  little &quot;article
	  discussion forum&quot;
	  at the bottom to post or answer questions and make requests, as well as
	  to make repeat visits to see &quot;what's new&quot; on the page! All contributions
	  are encouraged, either via the discussion at the bottom of this page, or
	  via email.
	   

 

 <table border=0 cellspacing=0 cellpadding=0 width="100%" align=center >
<tr><td align=left valign=bottom><br> </td></tr>
  <tr><td align=center valign=bottom>  
  
<div id="AdPage" name="AdPage">

<table border=0 cellspacing=0 cellpadding=0 align=left><tr><td valign="bottom"><script type="text/javascript"><!--

google_ad_client = "pub-8682474657542641";
google_alternate_ad_url = "http://www.eggheadcafe.com/alternateads.asp";
google_ad_type = "text";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_channel ="0679176943";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_text = "0000ff";
google_color_url = "666666";
google_color_link = "000000";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></td></tr></table>
 
 
</div>
 </td>

 

</tr>

</table>
 
 <!-- End of your database generated content-----  background-color:#F7F7F7; ------------------------->


	  
	  </p>
	  <h4><strong>Basic Concepts: </strong></h4>
	  <p>There are two important component parts of any ASP.NET &quot;page&quot; - the <strong>&quot;.ASPX&quot;</strong>	  portion,
	    which is really a modified HTML page that holds the asp:control server
	    tags, client script, and HTML elements, and the <strong>&quot;Codebehind&quot;</strong> or
	    server-side portion of the page, which can be either inside &lt;script runat=&quot;server&quot;
	    ... tags, or more typically, compiled into the .NET assembly that represents
	    all the pages of the particular ASP.NET Web application. Client-side
	    script (normally JavaScript or &quot;JScript&quot; if you prefer to use the Microsoft
	    &quot;branded&quot; name) always appears in the rendered HTML that shows up in
	    your browser when you request an ASP.NET page, because in the final analysis,
	    it doesn't matter what technology is used to &quot;generate&quot; a page- its still
	    just HTML, CSS and Script when its done
  and	the browser has consumed it.  </p>
	  <p>The script that shows up in the browser - rendered
	      version of the page, however, could be generated by either having it
	    prewritten, sitting inside &lt;script&gt;&lt;/script&gt; tags in the ASPX portion
	    (or with a src=&lt;scriptlocation&gt; attribute), or it could be generated
	    from server side processing, as we will soon see. </p>
	  <h4><strong>ASP.NET Intrinsic Client-Script Related Methods</strong></h4>
	  <p>ASP.NET has several built in methods of the Page class that were specifically
	    designed to help with client-side scripting. Let's look at these first:
 </p>
	  <h5> <strong>RegisterClientScriptBlock</strong>	    and <strong>RegisterStartupScript</strong> methods
      of the Page class. </h5>
	  <div align="justify"> These methods identify your script with a string key,
	      so that multiple server control instances can request the script block
	      without it being inserted in the output more than once. The <strong>IsClientScriptBlockRegistered</strong> and <strong>IsStartupScriptRegistered</strong> methods
	      of the Page object are used to determine if the script has already
	      been registered. <br>      
        <br>      
        <strong>Differences between RegisterClientScriptBlock and RegisterStartupScript</strong> </div>	  
	  <p>	    The RegisterClientScriptBlock method inserts the client-side script
	    immediately below the opening tag of the Page object's &lt;form runat=”server”&gt; element.
The RegisterStartupScript method inserts the specified client-side script just
before the closing tag of the Page object's &lt;form runat=”server”&gt; element.
You need to include the &lt;script&gt; start and end tags in the output when
using the above methods. The choice of which method to use really depends on
the &quot;order&quot; in which you want your script to be run by the browser when rendering
the page. </p>
	   Both of these methods take two strings as input. The second parameter, <em>script </em>,
	    is the client-side script-including the opening and closing &lt;script&gt; tags.
	    The first parameter, <em>key </em>, serves as a unique
	    identifier for the inserted client-side script.<br>      <br>
      There are two different methods for emitting client-side
script because client-side script generally can have one of two purposes:
code that is designed to run immediately when the page is loaded, and code
	    that is designed to run when some client-side event occurs. An
	    example of code designed to run when the page is loaded is script that
	    sets the focus to a textbox. When you visit Google, a piece of client-side
	    code is executed when the page is loaded to set the focus
	    to the search textbox. <br>	    <br>      
        <h5><strong>RegisterOnSubmitStatement method</strong></h5>       <br>
      This
method allows a page to access the client <strong>OnSubmit </strong> event.
        The script should be a function call to client code registered elsewhere.<strong>Parameters:</strong><br>      <em>key </em>Unique key that identifies a script block.<br>      <em>script </em>The
	  client-side script to be sent to the client.
<p>Example (This also uses the <strong>RegisterHiddenField</strong> method): </p>
	  <p align="left"><span class="style5">void Page_Load(Object sender, EventArgs e) <br>
	    { <br>
	    String
	      scriptString = "&lt;script
	    language=JavaScript&gt;function doClick() {"; scriptString += "document.write('&lt;h4&gt;'
	    + myForm.myHiddenField.value+ '&lt;/h4&gt;');}&lt;"; <br>
	    scriptString += "/" + "script&gt;"; <br>
	    RegisterHiddenField("myHiddenField", "Welcome to Microsoft .NET!"); <br>
	    RegisterOnSubmitStatement("submit", "document.write('&lt;h4&gt;Submit
	    button clicked.&lt;/h4&gt;')"); RegisterStartupScript("startup", scriptString); <br>
        } <br>      
      </span>
     <h5> <strong>Preventing Form Submissions with Client Script</strong> </h5>
    <p>The confirm dialog box is displayed using the script <strong>confirm(message) </strong> function.
      It displays a dialog box with the text specified by
      the <em>message </em> input parameter along with OK and Cancel buttons.
      The <strong>confirm(message) </strong> function returns true if the user
      clicks OK and false if they click Cancel. </p>     
    <p>Confirm dialog boxes are normally used to ensure that the user wants
      to continue before submitting a form. When an HTML element is clicked to
      submit a form,  if it fires a client-side
      event handler that returns false, <strong>the form submission
      is canceled</strong>. Typical
      usage in a Web page as follows: </p>
    &lt;form ...&gt; <br>    
    &lt;input type="submit" value="Click To Submit" onclick="return
    confirm('Are you sure you want to submit this form?');" /&gt; &lt;/form&gt;
    <p>When the user clicks the "Click To Submit" button, they see
  the confirm dialog. If the user clicks OK, <strong>confirm() </strong> will
  return true and the form will be submitted. If, however, they click Cancel, <strong>confirm() </strong>will
  return false and the form's submission will be canceled. </p>
    <p>Code such as the above can be easily attached to the Button that submits
      a form as follows:<br>
      <br>
      <span class="style5">private void Page_Load(object sender, System.EventArgs
      e)<br>
{<br>
string scr =&quot;return confirm('Are you sure you want to submit this form?');&quot;;<br>
this.Button1.Attributes.Add(&quot;onclick&quot;,scr);<br>
}</span></p>
    <h5>Preventing Form Submissions that programmatically call __doPostBack </h5>
    <p> Listening for form.onsubmit (by using Page.RegisterOnSubmitStatement
    ) is not sufficient because it won't fire when the form is submitted programmatically
      as with ASP.NET's __doPostBack method. Here is a solution that &quot;hijacks&quot;
      the __doPostBack method to ensure that your onSubmit handler is executed
      first:</p>
    <p>In the ASPX portion of your page:</p>
    <p class="style5">&lt;HTML&gt;<br>
&lt;HEAD&gt;<br>
&lt;script&gt;<br>
<span class="style6">// save a reference to the original __doPostBack</span><br>
var __oldDoPostBack = __doPostBack;<br>
<span class="style6">// replace __doPostBack with another function</span><br>
__doPostBack = AlwaysFireBeforeFormSubmit; <br>
function AlwaysFireBeforeFormSubmit (eventTarget, eventArgument) {<br>
<span class="style7">// do whatever pre-form submission<br>
// chores you need here (example:)</span><br>
return confirm('Are you sure?');<br>
<span class="style6">// finally, let the original __doPostBack do its work</span><br>
return __oldDoPostBack (eventTarget, eventArgument);<br>
}<br>
&lt;/script&gt; </p>
    <p class="style8">Then in your Page_Load:</p>
    <p class="style5">private void Page_Load(object sender, System.EventArgs
      e)<br>
{ <br>
System.Web.UI.HtmlControls.HtmlForm frm =(System.Web.UI.HtmlControls.HtmlForm)this.FindControl(&quot;Form1&quot;);<br>
frm.Attributes.Add(&quot;onSubmit&quot;,&quot;AlwaysFireBeforeFormSubmit();&quot;); <br>
} </p>    
    <h5>ValidateRequest Directive </h5>
    <p> ASP.Net 1.1 will not allow html code to be sent to the server unless
      you use the page directive validateRequest = "false". This can also be
      set globally in web.config in the pages element:</p>
    <p> &nbsp; Add the "pages" line within the pre-existing &lt;system.web&gt; tag:<br>
&nbsp;<span class="style5"> &lt;system.web&gt; <br>
&nbsp; &nbsp; &lt;pages validateRequest="false" /&gt; <br>
&nbsp; &lt;/system.web&gt;</span> <br>
<br>
NOTE: If you use the above, your site may be vulnerable to cross-site scripting attacks,
unless your code was already well-written to prevent that. </p>
    <h5>GetPostBackClientEvent Method </h5>
    <p>The GetPostbackClientEvent method returns a string containing the client-side
      script for the postback event of a given control:</p>
    <p>Parameters:<br>
control - the control that should be passed to the postback method on the client
  side as the source of the postback event.<br>
argument- the argument that will be passed to the postback method</p>
    <p>String = GetPostBackClientEvent(control, argument)</p>
    <p>GetPostBackClientHyperlink Method<br>      
        <br>
        The
      GetPostBackClientHyperlink method returns a string representing a hyperlink
      to the client-side script call that initiates the postback process.    </p>
    <p>Parameters:<br>
control - the control that should be passed to the postback method on the client
  side as the source of the postback event.<br>
argument- the argument that will be passed to the postback method        <br>
<br>
This method allows developers to create hyperlinks that will cause the page to
postback to the server. For example:</p>
    <p>&lt;a id=&quot;link1&quot; href=&quot;&lt;%=GetPostBackClientHyperlink(button1, &quot;submit&quot;) %&gt;&quot;&gt;Link
      1&lt;/a&gt;</p>
    <p>The above creates a link to the postback method associated with the control
      with id &quot;button1&quot;, and passes in an argument of &quot;submit&quot;.</p>
    <h5>The GetPostBackEventReference Method </h5>
    <p>Gets a reference to the client-side function that initiates the postback.
      When building web pages, it can be helpful to get a reference to the postback
      script function in order to programmatically cause the page to postback.
      This method will cause the page to postback, indicating that the specified
      control caused the postback.</p>
    <p>String = GetPostBackEventReference(control, [argument])<br> 
      </p>    
    <p>Parameters:<br>
      control  - the control that should be passed to the postback method on
      the client side as the source of the postback event.<br>
      argument-  the argument that will be passed to the postback method<br>
    </p>
    <h5>The RegisterArrayDeclaration Method <br>
    </h5>
    <p>This method creates a client-side script block that declares an array. In
      addition, you can add an element to the array. Subsequent calls to this method
      using the same array name add new elements to the same array. In this way,
      multiple controls in a page request can add themselves or elements of their
      content to this array.<br>
      <br>
      Void RegisterArrayDeclaration (arrayname, value) <br>
      <br>
      Parameters:</p>
    <p>arrayname (string)- the name to give to the client script array variable<br>
      value (string) - the value to be added to the array. Can be the name of
        an object or control on the page.</p>
    <p>Example:<br>
      <br>
      RegisterArrayDeclaration(&quot;TextBoxes&quot;,&quot;TextBox1&quot;)<br>
      RegisterArrayDeclaration(&quot;TextBoxes&quot;, &quot;TextBox2&quot;)</p>
    <p>Client - side output of the above two calls would look like this:</p>
    <p>&lt;script language=&quot;javascript&quot; &gt;<br>
      &lt;!--<br>
      var TextBoxes = new Array(TextBox1, TextBox2);<br>
      // --&gt;<br>
      &lt;/script&gt;
</p>
    <h5>Causing a page to reload via server - side action: </h5>
    <p>// cause page to reload from server-side Response object:<br>
      Response.Write(&quot;&lt;script&gt;window.location.reload(true);&lt;/script&gt;&quot;);</p>
    <p>// Cause page to repeatedly reload <br>
  Response.Write(&quot;&lt;script&gt;window.setTimeout(&quot;window.location.reload(true)&quot;,
  1000);&lt;/script&gt;&quot;);<br>
    </p>
    <p><br>    
    </p></td>
  </tr>
 
</table> 
<!--endPF--> 
 

<Center class="style4">
   FAQ Discussion: 
</center> 
<span class="style4"> 
<IFRAME name="IFR" src="/discussion/default.aspx?ForumId=20041016" width=100% height=50 scrollbars=yes border=no></IFRAME> 



 
      
	<!-- google_ad_section_end --></DIV> 
       </td>
	 
	   </tr>
      </table> 
	    <td align="left" width="10%">&nbsp;</td>
	  
	   </tr>
	</table>
   

          <table width="100%"  border="0" cellspacing="0" cellpadding="0">
               	<tr>
		<td>&nbsp;</td>
		<td class="text2">&nbsp;</td>
	</tr> 
	<tr>
		<td width="40" style="background:#FFFFFF url('/graphics/index38.gif') repeat; "><img src="/graphics/index38.gif"  width="20" height="32"></td>
		<td style="background:#FFFFFF url('/graphics/index38.gif') repeat; " class="style1" nowrap><a href="/default.aspx">Home</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href ="/contactus.aspx">Contact Us</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="/conversationgroupsummary.aspx">.NET Answers</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="/privacy.aspx">Privacy Policy</a></td>
	</tr>
 
	<tr>
		<td style="background:#FFFFFF url('/graphics/index39.gif') repeat; "><img src="/graphics/index39.gif"  width="20" height="13" border="0"></td>
		<td background="images/index39.gif">&nbsp;</td>
	</tr>
	<tr>
		<td><img src="/graphics/spacer.gif"  width="1" height="1" border="0"></td>
		<td class="text2">Copyright (c) 2007 eggheadcafe.com.&nbsp;&nbsp;All rights reserved.</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td class="text2">&nbsp;</td>
	</tr>
</table>
 
 <table width="100%"  border="0" cellpadding="0" cellspacing="0">
 <tr><td align="center"><div id="CardHidden_Search" style="visibility:hidden;background-color:#FFFFFF;position:relative; HEIGHT:5px;OVERFLOW: auto;"><table border="0" cellspacing="0" cellpadding="5" width="660px" height="100%"><tr><td style="background:#FFFFFF url('/graphics/SearchAreaBackground.gif') no-repeat; " align="center"><iframe id="iframeSearch" src="/blank.htm"  frameborder="no" style="overflow:auto;width:600px;height:310px;"></iframe></td></tr></table>
		</div></td></tr>
 </table>


<script>
var iFr;
function log() { 
var theAd=""+ escape(window.status.substring(6)) ;
var format=queryString('format',iFr.src);
var channel=queryString('channel',iFr.src);
//alert(iFr.src);
var bug = new Image();

var thetitle='/articles/20041016.asp' + '?' + '';
bug.src = '/pix.aspx?ref=' + document.referrer + '&url=' +theAd +'&title=' + thetitle+ '&format=' + format +'&channel='+channel ; 

// new code block
if (window.status.indexOf('go to') == 0) 
	{ 		
		urchinTracker('/adsense/ads/' + escape(window.status.substring(6)) +'_'+ thetitle ); 
	} 
	else if (window.status.indexOf('View ads about') == 0) 
	{ 		
		urchinTracker('/adsense/links/' + escape(window.status.substring(15))+'_'+ thetitle ); 
	} 
// end new code block
}  
 
	var elements; 
	elements = document.getElementsByTagName("iframe"); 
	for (var i = 0; i < elements.length; i++) 
		{ 
		if(elements[i].src.indexOf('googlesyndication.com') > -1) 
			{ 
				elements[i].attachEvent("onfocus",log) 
				iFr=elements[i];
				 
			} 
		} 

function queryString(item, source)
{
var itemLoc=source.indexOf(item);
var newSrc=source.substring(itemLoc);
var endLoc=newSrc.indexOf("&");
 var lstSrc=newSrc.substring(0,endLoc);
 var itm=lstSrc.substring(item.length+1);
 return itm;
} 
</script> 
<script>
var theIP='130.203.133.75';
var thePage='/articles/20041016.asp';
var theMember ='';
var theTitle ='ASP.NET Client-Side Script FAQ';
var logImg = new Image();
//logImg.src='/includes/ViewLog.aspx?r=' + document.referrer + '&p=' + thePage +'&m='+ theMember +'&t=' +theTitle +'&ip=' +theIP ;
</script>
</form>

</body>
</html>

 
</span>