?
Solved

DataBinding CheckBox in FormView and show as checked/unchecked accordingly

Posted on 2011-02-14
8
Medium Priority
?
1,954 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 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 2000 total points
ID: 34897270
Did you try a CheckBoxField, instead of a BoundField?

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

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 

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
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 2000 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 2000 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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 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