Tuesday, August 19, 2008

Last post was about how dynamic javascript in the update panel was not being re-executed on a partial postback.  I have a better way of making it execute -- parse the controls in the UpdatePanel and call the javascript method eval().

Here is the hook:

Page.ClientScript.RegisterStartupScript(this.GetType(), "LoadHandler", "function LoadHandler(){EvalScript($get('" + updatePanel.ClientID + "'),'scriptId');};\r\n", true);

Page.ClientScript.RegisterStartupScript(this.GetType(), "AddLoadHandler", "Sys.Application.add_load(LoadHandler);\r\n", true);

EvalScript looks like this:

function EvalScript(control, id)
{
 if(!control){return;}
 for(var n=0;n<control.children.length;n++)
 {
     if((control.children[n].tagName == 'SCRIPT') && (control.children[n].id == id)){
         var scriptElement = control.removeChild(control.children[n]);
         eval(scriptElement.innerHTML);
     }else{
         EvalScript(control.children[n], id);
     }
 }
}

Basically EvalScript searchs client side all the controls in the updatepanel control for one with the name specified, then calls Eval on it's innerHTML.  Just a note, you don't want inside comments in your <SCRIPT> tag, they don't eval() well.

{6230289B-5BEE-409e-932A-2F01FA407A92}

AJAX | ASP.NET | Wayne
Tuesday, August 19, 2008 12:06:39 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Friday, August 15, 2008

If you are using an Ajax UpdatePanel in ASP.NET and you have a control that renders javascript, that javascript will not change when on a partial postback.  For example, I have a Repeater control that loops though so GEO points and outputs javascript to add PushPin to a Virtual Earth map.  If a partial postback is called, and the Repeater rebinds it recreates the javascript, however on the client side the ScriptManager just replaces the UpdatePanel innerHTML with then new code.  It doesn't tell the javascript engine to reparse that code and recreate the functions that are being replaced.  To solve this problem I had to write some javascript client code that would parse the return, and move my <SCRIPT> tags to the head of the page.  This "woke up" the javascript engine and the new functions where recompiled.

How to hook it up:

1) The first thing to do is to tell the ScriptManager to parse the innerHTML of the UpdatePanel and move all script blocks to the head of the page (note: you need to have a page head).  You can do this like this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

2) Next you need to add a function that call EndRequestHandler that will call the parser, like this:

function EndRequestHandler(sender,args){ParseScript($get('UpdatePanelId'));};

Mine looks like this:

Page.ClientScript.RegisterStartupScript(this.GetType(), "EndRequestHandler", "function EndRequestHandler(sender,args){ParseScript($get('" + updatePanel.ClientID + "_Container'));};\r\n", true);

Not that the ParseScript function traverses the controls in the page and find childern with the tag name of <SCRIPT>, so I wanted to start with a UpdatePanel parent, and since the id is dynamically generated, I need to add the EndRequestHandler client side javascript from the server.

3) Next write ParseScript to traverse the childern and move all SCRIPT tags to the head, I will let you write your own code, cause this is QED.

{6230289B-5BEE-409e-932A-2F01FA407A92}

ASP.NET | Wayne | Ajax
Friday, August 15, 2008 9:20:08 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, August 14, 2008

I took off 4 months to remodel my kitchen, in that time I did no programming.  Now I am back to work for at least a week now.  I havn't forgotten anything (except for the SQL Server sa password -- which means that it is a good one.)  I can program just as well as before I left. 

{6230289B-5BEE-409e-932A-2F01FA407A92}

Thursday, August 14, 2008 9:06:09 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Sunday, August 10, 2008

I was investigating a starter website and noticed that it mentioned subsonic. Since third-party components can be tricky for implementation and security, I went to see what it was. Subsonic is an open source Data Access Layer. Someone has probably mentioned it to me before but it didn't register. The website has videos using subsonic which are great. Subsonic connects to the database and creates a DAL for a .Net project which uses intellisense for discovery. The only thing that worries me is that the connection isn't through SPs they create but they say they parse the SQL commands so that SQL injection isn't a problem. I also watched another video of theirs about their REST handler on their tips and tricks page. While I still like .NetTiers for my DAL (which requires a purchase of CodeSmith), I liked the easy of use and the fact that CodeSmith is not required. Just as I was grumbling to myself about a startersite for this, I found the link on the front page of their site for just that. It's a content management site that includes login, page generation, etc. The video cut out on me halfway through but I got the main idea and I liked it. I also noticed that the video mentioned FCKeditor which I hadn't heard of before.

I like this stuff but will probably use it for my own admin pages. It's not that the product is bad but I want to go through generated sp's and I like that .NetTiers gives me an admin site and web service. .NetTiers is also a layer I know who to code to and go farther with in terms of specificity and inheritance. But I really like this subsonic stuff.

 

 

Sunday, August 10, 2008 7:43:13 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Monday, June 30, 2008

My blackberry just threw an exception. The consumer in me thinks oh, $%^@#$. The programmer in me thinks that's so cool, I wonder what did it. A hard boot got the thing going again.

Monday, June 30, 2008 8:41:13 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, June 24, 2008

The basics for the login pages are done. A few more tweaks and that will be done.

It might seem like I take forever but in truth, I don't have many hours a week to give to this pet project right now. I hope to have more time this fall.

 

Tuesday, June 24, 2008 7:39:38 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [1]  | 
 Thursday, June 05, 2008

In my quest to find a suitable generic update method for a generic LINQ Data Access class I have found a decent amount of code and even more theories (sans code).  Some good, some verbose, and a lot bad.  It seems that Extension Methods are a fan favorite for just about everything nowadays and I wasn't suprised to see Extension Methods being used for LINQ to SQL updates.

In reading about Extension Methods I have heard good and bad feedback.  The good usually involves the convenience of bypassing a utility class and the bad involving code readability / maintability.  Forgive me for I am the William Hung of LINQ as I have had no formal training in this arena, but IMHO Extension Methods are a pain-in-the-ass when ExtensionMethodA returns the result of ExtensionMethodB which depends on the output of ExtensionMethodC which ..... Here is an example of what I am talking about in pseudo-code:

public static bool Update<T>(this Table<T> table, T instance) where T: class
{
   try
   
{
      // do stuff here with parameters...
      
return table.Foo() > 0;
   }
   catch (Exception)
   {
      return false;
   }
}

public static int Foo<T>(this Table<T> table) where T: class
{
   return table.GetEnumerator().Bar();
}

public static int Bar<T>(this IEnumerator<T> e) where T : class
{
   return 1;

Horrible example yes and for that I apologize.  Hopefully you see where I am going with this and that if you are given a similar codebase it can be quite confusing.  When I first heard of extension methods I though they were for simple utility tasks such as outputting a decimal value as a dollar amount (again, doesn't REALLY warrant an extension method but...):

public static string PrintWithDollarSign(this decimal input)
{
   return input.ToString("c");
}

Simple. Effective. Here's how you would use:

Console.WriteLine(instance.Price.PrintWithDollarSign());

Cheap. No frills.  I could have just bypassed this extension method altogether but it provides a very "Hello World"'y introduction to extension methods for those of you not hip to them.  In my previous example if you had an object with a decimal property and the value was 5.00 the output would be $5.00. Easy.

Anyone care to chime in on extension methods relying on other extension methods? I'd love to get some feedback.

C# | Will
Thursday, June 05, 2008 6:19:17 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, June 04, 2008

Now that I'm diggging a little deeper into my pet project web site, I'm learning how I really use the data. The Get() methods provided by .netTiers are great but I thought I would have more. I started looking at my database starting with the aspnet_Membership tables. I would expect unique indexes on something as obvious as the email column but it is not there. Same goes for other obvious columns in these microsoft-provided tables. So I add unique indexes to the membership tables. Of course, I do the same for my own custom tables and find that some of the text or varchar(max) have to be changed. No biggie but another step I have to take.

Now that the database is set, I need to regen the .netTiers templates, rerun the GrantUser stored proc, build the .netTiers stuff and copy it into the /bin of my project. Not hard work but just tedious to get each step completed and move on to the next one. All this so my .Get methods are consistent with my usage of the library.

All this so I can get my validators working - I really hate validators.

Wednesday, June 04, 2008 8:35:07 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [1]  |