4

I am using DetailsView which its DefaultMode: insert, and I want to make its checkbox to be checked by default also user can change it to unchecked, but to bind checkbox we should use

Checked='<%# Bind("Cit_Visible") %>' 

and this lets the default status of checkbox to be unchecked, so how can I solve this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Ahmad Z. Tibi
  • 419
  • 2
  • 8
  • 19

3 Answers3

2

You can assign value to text property of checkbox if you want your check box selected at the time of data binding.

<asp:CheckBox ID="chl" runat="Server" Checked="true" Text="<%# Bind('Cit_Visible') %>" />

on code behind you can access text value to save it to in DB

    CheckBox MyCheckbox = new CheckBox();
    MyCheckbox = (CheckBox)DetailsView1.FindControl("chl");
    Response.Write(MyCheckbox.Checked);
Mazhar Khan
  • 396
  • 4
  • 15
2

When using a DetailsView data control and you have checkbox values you may be starting with an asp:CheckBoxField which handles all the display modes for you. If you want to keep the checkbox binding but also set the default to checked perhaps for an insert you can do the following.

Convert the field to a TemplateField which can be done through the design view of visual studio or manually by replacing this type of block..

<asp:CheckBoxField DataField="Information" HeaderText="Information" SortExpression="Information" />

with a block of code like this

<asp:TemplateField HeaderText="Information" SortExpression="Information">
            <EditItemTemplate>
                <asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' Enabled="false" />
            </ItemTemplate>
        </asp:TemplateField>

Then to set the checkbox default value to be checked you can do this in the code-behind

Protected Sub dvInformation_PreRender(sender As Object, e As EventArgs) Handles dvInformation.PreRender
    If CType(sender, DetailsView).CurrentMode = DetailsViewMode.Insert Then
        Dim chk As Object = CType(sender, DetailsView).FindControl("chkInformation")
        If chk IsNot Nothing AndAlso chk.GetType Is GetType(CheckBox) Then
            CType(chk, CheckBox).Checked = True
        End If
    End If
End Sub

C# (Converted from VB

protected void dvInformation_PreRender(object sender, EventArgs e)
{
        if (((DetailsView)sender).CurrentMode == DetailsViewMode.Insert) {
            object chk = ((DetailsView)sender).FindControl("chkInformation");
            if (chk != null && object.ReferenceEquals(chk.GetType(), typeof(CheckBox))) {
                ((CheckBox)chk).Checked = true;
            }
        }
}

This is obviously best when the supporting database value is a non-null bit field

eyegropram
  • 672
  • 4
  • 11
1

Use TemplateField:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chk1" runat="server" OnInit="chk1_Init" Checked='<%# Bind("Cit_Visible") %>' />
    </ItemTemplate>
</asp:TemplateField>

Set the checkbox default value in the Init method:

protected void chk1_Init(object sender, EventArgs e)
{
    ((CheckBox)sender).Checked = true;
}
palota
  • 465
  • 4
  • 8