Zeeshan Hirani
-
Creating Extension methods with same name as instance methods
I had a really good time at the user group meeting tonight. Learned some new stuff on linq. An interesting point one of my prior co-worker Matt mentioned was if you create an extension method with the same name as the instance level method on the class, the compiler does not raise any exceptions. For a second I didn't believe it and so I went home and tried. Sure enough he was true. I would think that it is illegal to declare an extension method with the same name as instance method because instance method should always take precedence over extension method. Also say if you had an extension method that does some stuff. Later you upgrade to a new framework and the framework also includes a method with the same name. Since C# compiler does not raise compilation error, you wouldn't know that in fact you are actually using the method implementation from the framework than your extension method which may not be the desired behavior. I am curious to know why other people think that C# compiler should not raise a compilation error in this scenario.
-
Using RegisterDataItem on ScriptManager
Although there are different ways of sending data to client side from asp.net Ajax such as webservice, page methods, ScriptManager.RegisterStartupScript and many other ways. However my specific requirement was a bit different. I simply needed to send a value down to the client when a postback occurs through a button that is inside of an UpdatePanel, basically send additional data via ScriptManager to the client code when its sending the partial changes of the UpdatePanel down to the browser.You would want to use this technique if you are updating or want to update a control that is not part of an UpdatePanel. If the control that you are wanting to refresh is inside of an Update Panel, than simply calling UpdatePanel.Update would be sufficient enough. Here is the markup that shows how to send values down to the client code and updating a label on the client side with the data from the server.
-
Causing Full Postbacks from Inside of UpdatePanel
Recently I had a need where I wanted to just make one control inside of my Update Panel fully post back to the server with out any asynchronous request. Well my need had to do with uploading a file which is an upload control followed by an upload button. If you have tried like me before you would realize that you cannot upload a file from inside of an Update Panel. It has to go through a regular postback to successfully upload the file to the server. If you have a similar need you can make use of triggers collection and add upload button as part of Postback trigger instead of an asynchronous postback trigger. Here is a simple markup that demonstrate the behavior.
-
Canceling Async PostBack
Asp.net Ajax can perform at most one Async PostBack at a time. If the user initiates a second PostBack, it automatically aborts the previous request and initiates a new asynchronous PostBack. If this is not the behavior you desire, you have the ability to cancel new request until the existing request completes. In order to cancel new request you will have to register with beginRequest event of the pagerequest manager and check to see if there is an existing request that has not completed. If it is not completed than you can cancel the new request. The reason asp.net Ajax has limitations for only 1 Async PostBack is because of viewstate. If it were to allow more than 1 request than viewstate would get out of sync. If the current request takes too long to come back you can also have a cancel button on the UI and let the user cancel the existing page request. Here is the markup that I have written that displays the behavior.
-
Tracing Asp.net Ajax Client Side Request
When you add script manager control on the page, it not only goes though page life cycle on the server but the request goes through series of events on the client side. There are two client side objects available in asp.net Ajax; Sys.Application and Sys.WebForms.PageRequestManager object. Sys.Application events are raised because of scriptmanager and PageRequestManager events gets raised only when there are updatePanels present on the page. In order to illustrate all these events I will use tracing feature available in asp.net Ajax. By using Sys.Debug.trace, I will write trace messages to a textarea with id equal to TraceConsole. If you have Firebug extension installed on firebox, the traces get automatically written to the trace console of Firebug. Here is how my markup looks like.
-
Sending script After Async PostBackPostBack
I am sure many of you have been in a situation where you have a form view or details view control inside of an UpdatePanel and after inserting a successful record into the database you went to send a small JavaScript to the end user saying that your record got successfully inserted into the database. I made use of ItemInserted event of FormsView control and used the Page.ClientScript.RegisterStartupScript to inject the JavaScript into the output stream. Unfortunately this ClientScript on the page object does not work when you are inside of an UpdatePanel. Here is the markup and codebehind of my page.
-
Setting Default Values on Your Custom Control
Today when I was working with creation of server controls, I realized that I wanted my server controls to work out of the box using default values when you drag and drop the control onto the designer surface. That's when I started digging into what do I needed to do on my server controls to set default values. Sure enough I welcomed the new attribute ToolBoxData in which you can specify default values for the properties of your control when it gets dragged. Here is an example of Address server control in which I am setting default values for the properties address,city, state and zip on my address control.
-
How to avoid cc1 prefix when dragging server controls
Continuing with my exploration in server controls, i discovered a small nuisance when i you drag your custom controls from the toolbox onto the designer. For some reason visual studio would automatically prefix the server control with cc1 tagprefix as shown in the example below.
-
RenderContents Using enumerations
Continuing with my discovery of server controls, I have come across few more goodies. As I mentioned in my earlier blog posting that there are two different ways of writing content when you override RenderContents method inside of a server control. Here is an example of two different approaches.
-
Registering Server Controls in web.config
If you have a custom server control that you are going to use in several different pages. It might be worth while to register the server control in web.config file instead of registering it in several different pages. Here is an example of registering my custom controls library in web.config file.