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();

Wednesday, November 05, 2008

Changing ODS parameter at runtime causes double bind

changing a pagepropertyparameter value caused the ods to invalidate itself and forced a second database access
this was fixed by maintaining a local copy of a DatabaseAccess object and returning that on each subsequent property access (the first time, it's null and created from scratch)

Wednesday, October 01, 2008

Server tags reference

<% %> An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class.

<%= %> most useful for displaying single pieces of information.
CANNOT be used to assign values to properties (ie Text='<%= GetSomeText() %>' - this won't work) See here for an alternative.

<%# %> Data Binding Expression Syntax.

<%$ %> ASP.NET Expression.

<%@ %> Directive Syntax.

<%-- --%> Server-Side Comments.

Thursday, September 25, 2008

Javascript debugging

The community content on this page contains info on javascript debugging techniques

Monday, September 15, 2008

Could not load System.Web.Extensions

Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.


Solution: Download and install the Ajax library.

Friday, August 22, 2008

Failed to load viewstate

Error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

One possibility is that the query that selects the count and the query that selects the fields are in disparity. The count query must return the number of rows that would be returned by an unlimited field query.

Thursday, August 21, 2008

The name xxx Does Not Exist in the Current Context

This post helped me to realize that when I made copies as backups of "infringements.aspx" and "infringements.aspx.cs" named "copy of infringements.aspx" etc, the "copy of infringements.aspx" still referenced "infringements.aspx.cs" and caused the compiler some confusion.

Wednesday, August 20, 2008

Sys$CultureInfo$_getAbbrMonthIndex

Sys.ParameterCountException: Parameter count mismatch.'
function Sys$CultureInfo$_getAbbrMonthIndex(value)

I got this error when I had two controls that used javascript to show a modal popup extender. The control which was named by the mpe as the TargetControlID would not cause this error, but the other control would. (Infringements.aspx/mpeNoticeConfirm)

This appears to be a problem with the Web Developer 1.1.6 plugin for firefox. On computers without that plugin, the error console does not display the error.

Tuesday, June 03, 2008

Page Lifecycle

Here's a thread that discusses how to use trace to view the page lifecycle in action.

This page shows the complete lifecyle including master page and controls

Wednesday, February 06, 2008

DropDownList selection problem

If the selected value of a dropdownlist is incorrect it may be that the databinding is occurring in the Page_Load every time instead of only when IsPostBack is false.