Solved

DataBinding CheckBox in FormView and show as checked/unchecked accordingly

Posted on 2011-02-14
8
1,801 Views
Last Modified: 2013-12-17
Hi all,

I'd assumed this was straightforward but apparently not:

So firstly how do you databind a CheckBox in a FormView?
My FormView is Databound but I want my CheckBox to display as checked or unchecked based on the data already inserted (so this  is my edit view) therefore I need to tell the checkbox which field it's bound to but I can't see how!??

I'm very green in the realms of C# so I feel like I want to use an OnDatabinding event but I get as far as:  
protected void ChkBox_Bind(object sender, EventArgs e)

anyone able to help??
0
Comment
Question by:forsters
  • 4
  • 4
8 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 34897106
You should be able to bind the CheckBox control as in this example:

<asp:CheckBox ID="BlueCheckBox" runat="server" Checked='<%# Bind("Blue") %>' />

<table style="width: 514px; height: 154px">
            <tr>
                <td style="width: 100px">
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
            DataSourceID="SqlDataSource1" Height="50px" Width="125px" DataKeyNames="Room">
            <Fields>
                <asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" ReadOnly=True />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
                <asp:CheckBoxField DataField="Red" HeaderText="Red" SortExpression="Red" />
                <asp:CheckBoxField DataField="Blue" Text="Blue" SortExpression="Blue" />
                <asp:CommandField ShowEditButton="True" />
            </Fields>
        </asp:DetailsView>
                </td>
                <td style="width: 100px">
                    <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" Width="151px">
                        <EditItemTemplate>
                            Room:
                            <asp:TextBox ID="RoomTextBox" runat="server" Text='<%# Bind("Room") %>'>
                            </asp:TextBox><br />
                            Name:
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                            </asp:TextBox><br />
                            Color:
                            <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>'>
                            </asp:TextBox><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server" Checked='<%# Bind("Red") %>' /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server" Checked='<%# Bind("Blue") %>' /><br />
                            &nbsp;<br />
                            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update">
                            </asp:LinkButton>
                            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel">
                            </asp:LinkButton>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            Room:
                            <asp:TextBox ID="RoomTextBox" runat="server" Text='<%# Bind("Room") %>'>
                            </asp:TextBox><br />
                            Name:
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                            </asp:TextBox><br />
                            Color:
                            <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>'>
                            </asp:TextBox><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server" Checked='<%# Bind("Red") %>' /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server" Checked='<%# Bind("Blue") %>' /><br />
                            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                Text="Insert">
                            </asp:LinkButton>
                            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel">
                            </asp:LinkButton>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            Room:
                            <asp:Label ID="RoomLabel" runat="server" Text='<%# Bind("Room") %>'></asp:Label><br />
                            Name:
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label><br />
                            Color:
                            <asp:Label ID="ColorLabel" runat="server" Text='<%# Bind("Color") %>'></asp:Label><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server"  Enabled="false" /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server"  Enabled="false" /><br />
                            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit">
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:FormView>
                </td>
            </tr>
        </table>

Open in new window

0
 

Author Comment

by:forsters
ID: 34897253
Ah thank you so much, you know what I nearly tried that but because the default intellisense is just true/false I presumed it wasn't possible.

Just tried it now but I'm getting a "Specified Cast is not valid" error, any ideas why that might be?

My set up is slightly different in that I get to the FormView from a page which displays the integer value as a datafield in an asp:BoundField...is that the problem?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 34897270
Did you try a CheckBoxField, instead of a BoundField?

<asp:CheckBoxField DataField="Red" HeaderText="Red" SortExpression="Red" />
0
 

Author Comment

by:forsters
ID: 34897365
just tried that with one of the check boxes and now that page errors too saying string was not recognised as a valid boolean...?

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 34897810
Is the field a string, or a bit, or a boolean?
0
 

Author Comment

by:forsters
ID: 34898089
its an integer (tinyint)
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 34898229
Do you have the option of changing that to a bit type?
0
 

Author Comment

by:forsters
ID: 34898312
Ahh that now works perfectly, thank you so much I've been looking everywhere to solve this.
Thats so simple now , brilliant thank you.

So just for my own understanding  I presume from your earlier question that the ChkBox control supports string, bit & bool datatypes?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now