December 03, 2008

Enable IE6 Toolbar

0 comments

I had the same problem with the IE toolbar (IE7 under win 2003, using it to style a Sharepoint site). A combination of the suggested solutions did the trick:


  • Tools->Internet options->Security Tab->Local Intranet->Custom Level->Run ActiveX Control or PlugIn (Administrator Mode)
  • Tools->Internet Options->Programs->Manage add-ons->IEDeveloperToolbarBHO->Enable
  • Tools->Internet Options->Advanced->Enable third party browser-extensions (requires restart)

November 07, 2008

call a WebService from Browser(javascript code)

0 comments

Following links are helpful for making a webservice request


  • http://debasishpramanik.wordpress.com/2007/08/17/calling-web-service-using-xmlhttp-object/#comment-1021


For getting Basics:

  • http://www.codeproject.com/KB/aspnet/XmlHttpWS.aspx
  • http://www.howtocreate.co.uk/tutorials/javascript/domstructure

November 03, 2008

Creating a Simple User Control

0 comments

User controls allow you to save a part of an existing ASP.NET page and reuse it in many other ASP.NET pages. A user control is almost identical to a normal .aspx page, with two differences: the user control has the .ascx extension rather than .aspx, and it may not have

<HTML>, <Body>, or <Form> tags.


The simplest user control is one that displays HTML only(.ascx file)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="MyUserControl.WebUserControl1" %>







CopyRight 2008 Ramp Infotech, Inc.
Support at http://www.rampgroup.com


To see it work add this in the .aspx file as:
<%@ Register tagprefix="ramp" TagName="CopyRight" src="CopyRight.ascx" %>


This registers the control with our page and we can use it like






What is there in _VIEWSTATE field?

0 comments

First of all _VIEWSTATE is a field(HTML Element) and its different from what we think about a ViewState.

Now, Will you have a value stored in the _VIEWSTATE field if it is a blank ASP.NET Page or ViewState completely diabled. The answer is yes. If we see the viewsource of this blank page we can find a small serialized value. The reason is the page itself saves approximately 20 bytes of information into the _VIEWSTATE field, which it uses to distribute postback data and viewstate values to the correct controls upon postback.


    The data stored in the _VIEWSTATE field consists of the following
  • Data stored by developers using the Viewstate[""] indexer.
  • Programmatic changes to the control state.

OK, so let us assume that we have a web page with only a Label control on it. Now, let us say that at design time, if we set the “Text” property of the Label to “statictext”. Now, load the page and open the View Source page. We can see the __VIEWSTATE field. Do you think “statictext” is saved there? The answer is, for sure, not. Recall that static properties of controls are assigned at the generated class. So, when the page is requested, the values stored in the generated class are displayed. As such, static properties that are set at design time are never stored in the __VIEWSTATE field. However, they are stored in the generated compiled class, and thus they show up when rendering the page.

Now if we change the label "Text" property to "dynamicText" in the page load then this "dynamicText" is saved in the _VIEWSTATE field. The reason is we have changed the label's "Text" property at run-time and this is done at the page load time during which the tracking of viewstate is enabled(TrackViewState() is enabled in the InitComplete event), the "Text" property is marked as "Dirty" and thus the "SaveViewState" will save the controls new value in the _VIEWSTATE field!

Keep Smiling and Programming!!

November 01, 2008

ViewState - Dynamic Controls

0 comments

The first fact that you should know about dynamic controls is that they should be added to the page at each and every page execution. Never wrap the code that initializes and adds the dynamic control to the page inside a “!IsPostback” condition. The reason is that since the control is dynamic, it is not included in the compiled class generated for the page. So, the control should be added at every page execution for it to be available in the final control tree. The second fact is that dynamic controls play “catch-up” with the page life cycle once they are added. Say, you did the following:

Label lbl = new Label();
Page.Controls.Add(lbl);

Once the control is added to the “Controls” collection, it plays “catch-up” with the page life cycle, and all the events that it missed are fired. This leads to a very important conclusion: you can add dynamic controls at any time during the page life cycle until the “PreRender” event. Even when you add the dynamic control in the “PreRender” event, once the control is added to the “Controls” collection, the “Init”, “LoadViewState”, “LoadPostbackdata”, “Load”, and “SaveViewstate” are fired for this control. This is called “catch-up”. Note though that it is recommended that you add your dynamic controls during the “PreInit” or “Init” events. This is because it is best to add controls to the control tree before tracking of the Viewstate of other controls is enabled…

Finally, what is the best practice for adding dynamic controls regarding Viewstate? Let us say that you want to add a Label at runtime and assign a value to its “Text” property. What is the best practice to do so? If you are thinking the below, then you are mistaken:

Label lbl = new Label();
Page.Controls.Add(lbl);
lbl.Text = "bad idea";

You are mistaken because using the above technique you are actually storing the value of the “Text” property in the Vewstate. Why? Because, recall that once a dynamic control is added to the “Controls” collection, a “catch-up” happens and the tracking for the Viewstate starts. So, setting the value of the “Text” property will cause the value to be stored in the Viewstate.

However, if you do the below, then you are thinking the right way, because you are setting the “Text” property before adding the control to the “Controls” collection. In this case, the value of the “Text” property is added with the control to the control tree and not persisted in Viewstate.

Label lbl = new Label();
lbl.Text = "good idea";
Page.Controls.Add(lbl);

Reducing ViewState - Disabling ViewState

0 comments

Disabling Viewstate would obviously reduce Viewstate size; but, it surely kills the functionality along the way. So, a little more planning is required…

Consider the case where you have a page with a drop down list that should display the countries of the world. On the “Page_Load” event handler, you bind the drop down list to a data source that contains the countries of the world; the code that does the binding is wrapped inside a “!IsPostback” condition. Finally, on the page, you have a button that when clicked should read the selected country from the drop down list. With the setup described above, you will end up with a large __VIEWSTATE field. This is due to the fact that data bound controls (like the drop down list) store their state inside the Viewstate. You want to reduce Viewstate; what options do you have?


  1. Option 1: Simply disable the Viewstate on the drop down list
  2. Option 2: Disable the Viewstate on the drop down list, and remove the “!IsPostback” condition
  3. Option 3: Disable the Viewstate on the drop down list, and move the binding code without the “!IsPostback” condition to the “OnInit” or “OnPreInit” event handlers

If you implement Option 1, you will reduce the Viewstate alright, but with it, you will also lose the list of countries on the first postback of the page. When the page first loads, the code in the “Page_Load” event handler is executed and the list of countries is bound to the list. However, because Viewstate is disabled on the list, this change of state is not saved during the “SaveViewState” event. When the button on the page is clicked causing a postback, since the binding code is wrapped inside a “!IsPostback” condition, the “LoadViewState” event has nothing saved from the previous page visit and the drop down list is empty. If you implement Option 2, you will reduce the Viewstate size and you will not lose the list of countries on postback. However, another problem arises: because the binding code is now executed at each “Page_Load”, the postback data is lost upon postback, and every time, the first item of the list will be selected. This is true because in the page life cycle, the “LoadPostbackdata” event occurs before the “Load” event. Option 3 is the correct option. In this option, you have done the following:

  • Disabled Viewstate on the drop down list
  • Removed the “!IsPostback” condition from the binding code
  • Moved the binding code to the “OnInit” event handler (or the “OnPreInit” event handler)

Since the “Init” event occurs before the “LoadPostbackdata” in the page life cycle, the postback data is preserved upon postbacks, and the selected item from the list is correctly preserved.

Now, remember that in Option 3, you have successfully reduced the Viewstate size and kept the functionality working; but, this actually comes at the cost of rebinding the drop down list at each postback. The performance hit of revisiting the data source at each postback is nothing when compared with the performance boost gained from saving a huge amount of bytes being rendered at the client’s __VIEWSTATE field. This is especially true with the fact that most clients are connected to the Internet via low speed dial up connections.

ASP.NET Page Life Cycle Demonstration

0 comments

Let us take an example ASP.NET page with the following characteristics:


  • It contains a single Label control (lbl) with its “Text” property set to “statictext”
  • It contains a Button control (btnA) with code in its event handler that sets the “Text” property of “lbl” to “dynamictext”
  • It contains a Button control (btnB) whose purpose is to cause a page postback

Now, let us examine what will happen during the page life cycle.

  1. The page is first loaded

    1. The compiled class of the page is generated and “statictext” is assigned to “lbl.Text”
    2. Tracking for Viewstate is enabled in the “InitComplete” event
    3. “LoadViewState” is not fired because there is no postback
    4. “SaveViewstate” is fired, but nothing happens because no change of state is recorded
    5. The page is rendered with the value “statictext” inside “lbl”

  2. btnA is clicked

    1. The previously generated compiled class is handed the request; “lbl.Text” is set to “statictext”
    2. Tracking for Viewstate is enabled
    3. “LoadViewState” is fired, but nothing happens because no change of state was recorded in the previous page visit
    4. “RaisePostbackEvent” event is executed and “btnA” click event handler is fired; “lbl.Text” is set to “dynamictext”; since tracking is enabled at this stage, this item is tracked (marked as “Dirty”).
    5. “SaveViewState” is fired; “dynamictext” is serialized and stored in the __VIEWSTATE field
    6. The page is rendered with the value “dynamictext” inside “lbl”

  3. btnB is clicked

    1. The previously generated compiled class is handed the request; “lbl.Text” is set to “statictext”
    2. Tracking for Viewstate is enabled
    3. “LoadViewState” is fired; since a change of state was recorded from the previous page visit, __VIEWSTATE is loaded and the value “dynamictext” is extracted; this value is then assigned to “lbl”
    4. “RaisePostbackEvent” event is executed; nothing happens here because “btnB” has no event handler for its “Click” event
    5. “SaveViewState” is fired, but nothing happens because no change of state is recorded
    6. The page is rendered with the value “dynamictext” inside “lbl”