DetailsView checkbox default appearance
I'm working with a GridView / DetailsView combo to maintain records in an Access database. One of the shortcomings of DetailsView is the difficulty in establishing default values for fields on the new records. But I have found a way to make that work by using the data binding event. The following code is how I set values, one for a textbox and one for a checkbox.
Protected Sub DetailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender
If DetailsView1.CurrentMode = DetailsViewMode.Insert Then
Dim ThisControl As TextBox
ThisControl = DetailsView1.FindControl("InsertDays")
ThisControl.Text = "1"
Dim ThisCheck As Checkbox
ThisCheck = DetailsView1.FindControl("InsertRegisterOnline")
ThisCheck.Checked = True
End if
End Sub
My current problem is the checkbox. I managed to give it an ID by using a templatefield (you can't assign an ID to a simple CheckBoxField). So inside the DetailsView, I now use:
<asp:TemplateField HeaderText="Register online?">
<ItemTemplate>
<asp:Checkbox ID="RegisterOnlineLabel" Runat="Server" Checked='<%# Eval("RegisterOnline") %>' />
</ItemTemplate>
<InsertItemTemplate>
<asp:Checkbox ID="InsertRegisterOnline" runat="server" Checked='<%# Bind("RegisterOnline")%>' />
</InsertItemTemplate>
<EditItemTemplate>
<asp:Checkbox ID="EditRegisterOnline" runat="server" Checked='<%# Bind("RegisterOnline")%>' />
</EditItemTemplate>
</asp:TemplateField>
This sort of works, but when I am in read mode, the checkbox looks as though it is editable (the user can click and change the checkbox setting). The changes do not carry through to the database, of course.
At the moment I need some way of making the checkbox appear read-only when the record has been selected and is in fact read-only.
Frankly, I'll accept any other scheme to do this, because all of this code is just a workaround for the absence of a simple method of setting default values when in insert mode. (ASP sure was a lot simpler to program!)
|