Solved

DataBinding CheckBox in FormView and show as checked/unchecked accordingly

Posted on 2011-02-14
8
1,873 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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