Wednesday, October 17, 2007

MSDN code examples are terrible

This is the page that really got my goat. I'll list the relevant parts here for ease of reading.

Page.RegisterOnSubmitStatement Method

Allows a page to access the client OnSubmit event. The script should be a function call to client code registered elsewhere.

   1: '[Visual Basic] 
   2: Dim scriptString As String = "<script language=JavaScript> function doClick() {"
   3: scriptString += "document.write('<h4>' + myForm.myHiddenField.value+ '</h4>');}<"
   4: scriptString += "/" + "script>"
   5: RegisterHiddenField("myHiddenField", "Welcome to Microsoft .NET!") 
   6:  
   7: RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')") 
   8:  
   9: RegisterStartupScript("startup", scriptString) 


   1: //[C#] 
   2: void Page_Load(Object sender, EventArgs e) 
   3: { 
   4:    String scriptString = "<script language=JavaScript> function doClick() {";
   5:    scriptString += "document.write('<h4>' + myForm.myHiddenField.value+ '</h4>');}<";
   6:    scriptString += "/" + "script>";
   7:    RegisterHiddenField("myHiddenField", "Welcome to Microsoft .NET!"); 
   8:    RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')"); 
   9:    RegisterStartupScript("startup", scriptString);
  10: } 

Ok, how many things can you find wrong with this simple example?


  1. string concatenating to make a script block

  2. a bunch of totally unrelated code that muddies the point

  3. document.write() - who does that?

  4. VB and C# code don't even match; C# includes the method declaration; VB only includes the method body

  5. scriptString += "/" + "script>"

  6. definition says "The script should be a function call to client code registered elsewhere." but that is not what the example code does

Here's my stab:



   1: //[C#] 
   2: void Page_Load(Object sender, EventArgs e) 
   3: { 
   4:   if (!IsClientScriptBlockRegistered("doClickScriptKey"))
   5:   {
   6:     String scriptString = @"
   7:     <script type='text/javascript'>
   8:         function doClick() { alert('the form is about to submit!'); }
   9:     </script>";
  10:     RegisterClientScriptBlock("doClickScriptKey", scriptString);  //"registered elsewhere"
  11:   }
  12:   RegisterOnSubmitStatement("submitScriptKey", "doClick();"); //"function call to client code registered elsewhere"
  13: } 

8 comments:

Anonymous said...

[url=http://www.hadigel.net/program/Program.asp?id=1964]need for speed indir[/url]

Anonymous said...

What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

price per head service said...

Hi, great article. The way you explained it is really awesome and makes every one to read till the end. keep posting..

pay per head said...

This is really great. I like to be visiting your blog. Thanks very much for sharing all this great information here.

Anonymous said...

purchase HCYYPAXO [URL=http://www.louis--vuitton--online--shop.org/]louis vuitton buy online[/URL] , just clicks away pMCaAESu [URL=http://www.louis--vuitton--online--shop.org/ ] http://www.louis--vuitton--online--shop.org/ [/URL]

Anonymous said...

debe mirar esto [URL=http://www.ralph--lauren.org/]ralph lauren watches[/URL][URL=http://www.ralph-lauren2013.net/]ralph lauren romance for men[/URL][URL=http://www.ralph-lauren2013.com/]ralph lauren watches[/URL][URL=http://www.ralph-lauren2013.com/]ralph lauren belt[/URL][URL=http://www.ralphlauren--outlet.com/]ralph lauren dresses[/URL][URL=http://www.ralphlauren--outlet.com/]polo ralph lauren usa[/URL][URL=http://www.cheap--ralph-lauren.com/]ralph lauren swim[/URL][URL=http://www.ralphlauren--outlet.com/]vintage ralph lauren[/URL][URL=http://www.ralph--lauren-outlet.net/]ralph lauren romance[/URL][URL=http://www.ralph--lauren-outlet.net/]ralph lauren website[/URL][URL=http://www.ralph-lauren2013.com/]rlx ralph lauren[/URL] con confianza AQwDEekt [URL=http://www.cheap--ralph-lauren.com/ ] http://www.ralphlauren--outlet.com/ [/URL]

Anonymous said...

check [URL=http://www.socialimba.com/burberryonlinestoreqq/]burberry shirts[/URL] for more detail RKGhrXjq [URL=http://burberryquiltedjacket.besthometreadmill.co.za/ ] http://burberryjacket.mzk-pulawy.pl/ [/URL]

Anonymous said...

check this link, jFXBkDlR [URL=http://www.hermes-birkinprice.weebly.com/]hermes outlet[/URL] and check coupon code available BQxSSzda [URL=http://www.hermes-birkinprice.weebly.com/ ] http://www.hermes-birkinprice.weebly.com/ [/URL]