Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 22, 2009 06:42 PM by JFMConcepts
Apr 26, 2007 02:44 PM|LINK
I have a FormView and a bound checkbox control in the InsertItemTamplate. I have to make that checkbox checked by default, but I don't see how I can do that when I have to set the Checked property to Bind("").
I've been trying to find solution for this but I can't think of anything other than handling it in the codebehind which is less then optimal. So if you have any suggestions please let me know. Here is the relevant part of the FormView
<asp:CheckBox ID="ShowListPriceCheckBox" runat="server" Checked='<%# Bind("ShowListPrice") %>' />
Apr 26, 2007 03:05 PM|LINK
Apr 26, 2007 03:44 PM|LINK
The best solution to this is to have your ShowListPrice column which should be a bit type with a default value, in your case, it is True.
If you have Null value in this column, you need to check whether there is a NULL there and set the value accordingly for the Checked property (if it is NULL, set Checked to True).
Other than using a function from code, you can use an inline IIF function to check NULL or C# equivalent:
Checked='<%# (Eval("ShowListPrice")==DBNull.Value ? true :false) %>'
Checked='<%# IIF(CONVERT.ToString(Eval("ShowListPrice"))="", true, false) %>'
Apr 26, 2007 05:12 PM|LINK
Thanks for the suggestion Limo. That works but I lose the two way binding functionality of Bind() by using the Eval function. For some reason the same syntax does not work with Bind()
Apr 26, 2007 06:07 PM|LINK
If you need two way databinding, you'd better fix your data source for this column. Do a simple update for this column like:
Update yourTable Set ShowListPrice=1 WHERE ShowListPrice IS NULL
Hope this helps.
Apr 27, 2007 01:28 PM|LINK
Maybe I wasn't clear in my original post. This is in the InsertTemplate when a new record is being inserted so there is no record in the database. I simply want to have a checkbox default to checked state but still take advantage of two way binding. It seems
like there's no way to do that declaratively.
May 01, 2007 04:10 AM|LINK
You can do this. In PreRender, if(!IsPostBack) then manually "click" your checkbox field one by one.
If it doesn't help, just inform us.
Jun 22, 2009 06:42 PM|LINK
That works, but it sucks! There should definately be a default state for the Insert templates on the Checkbox control.