Friday, September 11, 2009

How to Add an Event Handler to Control Created in Code Behind

I hope that title makes sense to you. Basically I'm building a big form in the code behind. Sucks. But in my case I had to do it and also had to set AutoPostback and add an EventHandler to some checkboxes.

cb = new CheckBox();
cb.AutoPostBack = true;
cb.CheckedChanged += new System.EventHandler(this.CB_CheckedChanged);


"CB_CheckedChanged" is a method I created to handle the cb checks. You can basically use the same code for RadioButtonLists etc.

Thursday, September 10, 2009

Format Date in GridView

I always forget how to do this one. If you need to format a date a certain way in a
GridView, try this out:

<asp:BoundField DataField="the_date" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false" />

Friday, September 4, 2009

Syntax error: Missing operand after 's' operator.

Don't bother trying to filter a DataView by setting the RowFilter property to a string with a single quote. Make sure you escape the quotes first:

dv.RowFilter = "Something = 'O'Malley'";

becomes...

dv.RowFilter = "Something = 'O''Malley'";