Friday, December 12, 2008

Page Loading Twice! or Data Binding Twice!

I often run into the problem of my data controls being databound twice. There are at least two causes.

1) The ObjectDataSource control has its EnableViewState property set to false. This results in the DataBind event firing twice.

2) An improperly defined html element on the page may be causing the browser to request the page multiple times. This results in the entire page lifecycle being executed repeatedly and thus resulting in multiple calls to DataBind. Two examples I have noted of improperly defined html tags are

  • An img tag with an empty src attribute
  • Others have mentioned that using the background attribute of a table cell tag (<td>) to set the color for the cell causes the browser to re-request the page. Use the bgcolor attribute instead.

Here are a couple of pages that discuss this problem.

Thursday, December 11, 2008

Login control default action problem

There is a bug in the Login control when you are using an image for the button, the default action will not be triggered. To overcome this, first wrap your Login control in a simple Panel:

<asp:Panel ID="LoginSubmitPanel" DefaultButton="" runat="server">
    <%--Login Control Here--%>
</asp:Panel>


Then add a load event to that panel to look for the unique id of the image button and assign the default action for this panel:

protected void LoginSubmitPanel_Load(object sender, EventArgs e)
{
    //Find all the controls we will need
    Login SideLogin = (sender as Login);
    Control LoginButton = (SideLogin.FindControl("LoginImageButton") as Control);
    Panel LoginSubmitPanel = (SideLoginView.FindControl("LoginSubmitPanel") as Panel);
    //We need the UniqueName under the proper context
    string btn = LoginButton.UniqueID.Remove(0, SideLoginView.UniqueID.Length + 1);
    //Assign the correct button as the default action        
    LoginSubmitPanel.DefaultButton = btn;
}


Now when you hit the enter key in the login control, the login action will be fired. Outside of the login control will follow regular default form submission.

Error list contains no line numbers

Warning! Visual Studio has a bug caused by the project path containing parentheses. It results in the error list not containing any line numbers. So after compiling, you get an error list, but you can't click the error to jump to the offending line. Not even the offending file is listed. All you can do is search your 10,000 lines of code for the error. This is particularly annoying when the error is "Semicolon expected".

By the way, a bug has been reported to the Visual Studio team and was marked "Closed (won't fix)." Thanks Microsoft!

Wednesday, December 03, 2008

Creating a Loading Page in ASP.NET

To display a waiting page while database or other lengthy work is done, you can use the Response.Write method to send text to the browser while keeping the HTTP connection alive. After your lengthy operation is complete, simply dump a javascript redirect, close the connetion and you are done.

For Example:
  Response.Write("<h3>Please wait...</h3>");
  Response.Flush();
  //Lengthy Operation
  string RedirectUrl = "<script language=javascript>window.location = \"done.aspx\";</script>";
  Response.Write(RedirectUrl);
  Response.End();