Formatting data in databound controls

Posted by andy gaskell on Apr 18th, 2006

I’ve been trying to format DateTime and currency in asp.net 2.0 databound controls (DetailsView and GridView). After some googling I was able to get it figured out.

To format DateTime values, you might expect to use a tag like this:

<asp:BoundField
     DataField="BeginDate"
     DataFormatString="{0:d}"
     HeaderText="Begin Date" />

and have a result like this:
10/10/2004

but instead you get a result like this:
10/10/2004 12:00:00 AM

Here’s the fix – add HtmlEncode=”false” to your BoundField tag:

<asp:BoundField
     DataField="BeginDate"
     HtmlEncode="false"
     DataFormatString="{0:d}"
     HeaderText="Begin Date" />

The same idea applies to formatting currency, I’m assuming other types of data formatting as well. Here’s Microsoft’s explanation.

Technorati Tags: , , , ,

ASP.NET 2.0 Master Pages Design Templates

Posted by andy gaskell on Apr 6th, 2006

Microsoft has developed some design templates built specifically for ASP.NET 2.0′s master pages. If you’re like me, and you don’t normally read many “design philosophy whitepapers”, it can be pretty interesting. The installer for the templates is cool too, you just run the installer and start up VS 2005 and you’ll have new web project types – nice!

Technorati Tags: , ,

Implementing AutoPostBack on Infragistics WebCombo

Posted by andy gaskell on Apr 1st, 2006

I use Infragistics NetAdvantage control suite for UI development at work. If you hate UI development like me, these tools can help out tremendously. While working with their WebCombo control, a much more powerful version of the ASP.Net DropDownList control – I noticed the WebCombo was sorely missing is the AutoPostBack property.

I’ll be explaining how to implement an AutoPostBack property on a UserControl that contains a single Infragistics WebControl.

On your HTML page, add the following tag after the end of your opening WebCombo tag:


<CLIENTSIDEEVENTS AfterSelectChange="itgtbl_needPostBack('{0}')"></CLIENTSIDEEVENTS>

Create a basic AutoPostBack property on the codebehind of your UserControl.

protected bool autoPostBack;

public bool AutoPostBack
{
     get{return this.autoPostBack;}
     set{this.autoPostBack = value;}
}

Now you need to add a method that either sets up your WebCombo so that it automatically posts back, or does not generate any client code to post back. You can decide when you want to call this method in your code

private void SetupAutoPostback()
{
     if(this.autoPostBack)
     {
          string gridClientID = string.Empty;

          foreach(Control control in this.YOUR_COMBO_HERE.Controls)
          {
               Infragistics.WebUI.UltraWebGrid.UltraWebGrid grid = control as Infragistics.WebUI.UltraWebGrid.UltraWebGrid;

               if(grid != null)
               {
                    gridClientID  = grid.ClientID;
                    break;
               }
          }

          this.YOUR_COMBO_HERE.ClientSideEvents.AfterSelectChange = string.Format(this.YOUR_COMBO_HERE.ClientSideEvents.AfterSelectChange, gridClientID);
     }
     else
     {
          this.YOUR_COMBO_HERE.ClientSideEvents.AfterSelectChange = string.Empty;
     }
}

Run your page – your combo should now post back automatically when the selected index changes.

Technorati Tags: , ,