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!") 
   7: RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')") 
   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: } 


Anonymous said...

[url=]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=]louis vuitton buy online[/URL] , just clicks away pMCaAESu [URL= ] [/URL]

Anonymous said...

debe mirar esto [URL=]ralph lauren watches[/URL][URL=]ralph lauren romance for men[/URL][URL=]ralph lauren watches[/URL][URL=]ralph lauren belt[/URL][URL=]ralph lauren dresses[/URL][URL=]polo ralph lauren usa[/URL][URL=]ralph lauren swim[/URL][URL=]vintage ralph lauren[/URL][URL=]ralph lauren romance[/URL][URL=]ralph lauren website[/URL][URL=]rlx ralph lauren[/URL] con confianza AQwDEekt [URL= ] [/URL]

Anonymous said...

check [URL=]burberry shirts[/URL] for more detail RKGhrXjq [URL= ] [/URL]

Anonymous said...

check this link, jFXBkDlR [URL=]hermes outlet[/URL] and check coupon code available BQxSSzda [URL= ] [/URL]