Zeeshan Hirani
-
Returning Anonymous Types from WebService to asp.net Ajax
If your objects do not have complex types, you can have your WebService return an array of anonymous types and .net serializer will correctly serialize your objects to be easily consumable from JavaScript code. In order for you to return anonymous types from your WebService method simply return an array of objects because you do not know what the actual type is? Below is a simple example that demonstrate how to consume an array of anonymous type from asp.net client side.
-
Missing Association Links
I must have spent good one hour trying to figure out why when I do category and dot, the products collection does not appear. I confirmed in the designer that category and product has an association link but when I open up designer.cs, I don't see Products EntitySet defined in Category class. It's really confusing why the designer shows that there is an association between category and products but the code is not generated. After an hour I finally realized that my category table did not define categoryid as primary key column. So keep in mind that if your table does not have primary key column defined, linq to SQL designer would not generate the code for association relationship.
-
Creating DataBound Control
The other day at work, I was working with working with collection of data from database and wondered how hard would it be to implement a custom DataBound control. Surely enough on researching, I found out that you can inherit from CompositeDataBoundControl and override CreateChildControls to bind the data to the markup user has specified in ItemTemplate property of the control. Here is an example of a SimpleDataBoundControl class that I have created that inherits from CompositeDataBoundControl.
-
UseSubmitBehavior=false
Most of the viewers would agree that there are so many gems hidden inside of asp.net that it takes some times years coming across them. Today I discovered SubmitBehavior property on button control. By default all button uses browser's default submit behavior to post the page back to the server. However there are cases when you are building server controls where you need explicit control as well as access to the JavaScript function that causes that button to post the page back to the server. In those scenarios, you can set the SubmitBehavior = false which will cause __doPostback JavaScript function to be attached to the button that will handle the postback for the button. On the server side, if you want to access the JavaScript function that causes the postback for the button you can use GetPostBackEventReference to access the JavaScript function. Here is a simple markup that uses UseSubmitBehavior property and also shows the JavaScript function emitted by asp.net.
-
ASP Namespace
ASP is the namespace where all the generated code files in asp.net reside. When a request is made to IIS for a page, if it ends in aspx extension, the request gets sent to aspnet worker process where the aspx file is converted to a class with filename_aspx and stored in Temporary Asp.net Files. By default all these generated files are stored in ASP namespace. In fact in visual studio, you can get intellisense of on these files as shown below.
-
ParseChildren in Templated Controls
Well, I have been spending sometime learning about Templated controls at work. Hence I am slowly getting an understanding of common attributes that you would want to use on Templated controls. If your template control inherits from WebControl than you do not need to worry about setting ParseChildren to true because by default WebControls have ParseChildren set to true. However if your controls inherit from Control class, than in order to get Templates to work you need to set ParseChildren to true. When you set ParseChildren to true, asp.net parser maps the top level tags of the server control to properties on the server control. For e.g
-
Instantiating Templates
I thought to write a blog post telling what is the correct way to instantiate templates because on researching many blog posts and reading few articles, I saw couple of different usages and one of them is misleading and incorrect. Here is the most common way of instantiating templates for a custom control.
-
Use TemplateInstance.Single to avoid FindControls
If you have created templated controls, you would be aware that it requires you to implement INamingContainer. Only job of INamingContainer is to ensure that controls are unique within that Naming Container. However sometimes if you are using Templated controls that are not going to be repeated like LoginView Control has anonymous Template which does not have a concept of a repeated template, in those cases you would like to be able to access those controls directly in the page.Templated controls have a requirement that contents with in the templated controls must be instantiated inside of a container that implements INamingContainer. Here is a simple example that illustrates this behavior.
-
When to instantiate templates
Most of the examples that I saw on the net recommended to instantiate templates inside of CreateChildControls method but it does not work correctly. Basically I marked my Itemplate property to TemplateInstance.Single, so all the controls inside my template are available to the page without using FindControl. This works great but only in design time. In the design mode, I can access all the controls but when the page executes, any controls being accessed that are inside of template causes object reference not set to an instance of an object. For a while I couldn't figure out what was going on. Then I realized that UpdatePanel has pretty much the same implementation I can access all the controls inside of UpdatePanel without any problems. Using Reflector I saw that UpdatePanel was not instantiating the template inside of CreateChildControls, in fact they were instantiating the template inside ITemplate property as shown below
-
Using Pragma to disable warnings
Lot of times, you have variables declared in your class that are never getting used directly and are accessed and their values altered using reflection. In those cases when you build you solution you get a warning saying that this particular field is never used. Here is a simple example that shows warning for first field in Person class for not being used.