Solved

DataBinding CheckBox in FormView and show as checked/unchecked accordingly

Posted on 2011-02-14
8
1,825 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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