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
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:
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()
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.
astankovic
0 Points
3 Posts
Default state for bound CheckBox control
Apr 26, 2007 02:44 PM|LINK
Hello,
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:FormView> <InsertItemTemplate> <asp:CheckBox ID="ShowListPriceCheckBox" runat="server" Checked='<%# Bind("ShowListPrice") %>' /> </InsertItemTemplate> </asp:FormView>Thanks,
Alex
Girijesh
Contributor
3126 Points
664 Posts
Re: Default state for bound CheckBox control
Apr 26, 2007 03:05 PM|LINK
http://www.girijesh.in/
limno
All-Star
117340 Points
8005 Posts
Moderator
MVP
Re: Default state for bound CheckBox control
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:
C#:
Checked='<%# (Eval("ShowListPrice")==DBNull.Value ? true :false) %>'
VB:
Checked='<%# IIF(CONVERT.ToString(Eval("ShowListPrice"))="", true, false) %>'
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
astankovic
0 Points
3 Posts
Re: Default state for bound CheckBox control
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()
Thanks,
Alex
limno
All-Star
117340 Points
8005 Posts
Moderator
MVP
Re: Default state for bound CheckBox control
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.
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
astankovic
0 Points
3 Posts
Re: Default state for bound CheckBox control
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.
Thanks,
Alex
Allen Chen –...
All-Star
40943 Points
4949 Posts
Re: Default state for bound CheckBox control
May 01, 2007 04:10 AM|LINK
Hi:
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.
Regards
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
JFMConcepts
Member
12 Points
8 Posts
Re: Default state for bound CheckBox control
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.