I have a DetailsView that containing a CheckBoxField. The problem is that I want the check box to be changeable even in read only mode and in insert mode, it must be checked by default. How do I resolve this?
I attach a piece of code:
<asp:DetailsView ID="newsDetail" runat="server" DataSourceID="SqlDataSourceNews"
AutoGenerateRows="False" DataKeyNames="id">
<Fields>
<asp:TemplateField FooterText="View at startpage" HeaderText="View" SortExpression="view">
<ItemTemplate>
<asp:CheckBox runat="server" ID="view" Checked='<%# Eval("view") %>' />
</ItemTemplate>
<InsertItemTemplate>
<asp:CheckBox ID="viewInsert" Checked="true" runat="server" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:CheckBox runat="server" ID="view" Checked='<%# Eval("view") %>' />
</EditItemTemplate>
</asp:TemplateField>
...
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" NewText="New" ButtonType="Button"
EditText="Edit" CancelText="Avbryt" DeleteText="Delete" InsertText="Add"
SelectText="Select" UpdateText="Uppdatera" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceNews" runat="server" ConnectionString="<%$ ConnectionStrings:newsConnectionString %>"
DeleteCommand="DELETE FROM [nyheter] WHERE [id] = @id" InsertCommand="INSERT INTO [nyheter] ([view], [headline], [post], [pic], [pic2]) VALUES (@view, @headline, @post, @pic, @pic2)"
SelectCommand="SELECT [id], [view], [date], [headline], [post], [pic], [pic2] FROM [nyheter] WHERE ([id] = @id)"
UpdateCommand="UPDATE [nyheter] SET [view] = @view, [headline] = @headline, [post] = @post, [pic] = @pic, [pic2] = @pic2 WHERE [id] = @id">
<SelectParameters>
<asp:ControlParameter ControlID="newsList" Name="id" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="view" Type="Boolean" />
<asp:Parameter Name="headline" Type="String" />
<asp:Parameter Name="post" Type="String" />
<asp:Parameter Name="pic" Type="String" />
<asp:Parameter Name="pic3" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="view" Type="Boolean" />
<asp:Parameter Name="headline" Type="String" />
<asp:Parameter Name="post" Type="String" />
<asp:Parameter Name="pic" Type="String" />
<asp:Parameter Name="pic2" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
You can not do that with the CheckBoxField, you have to create your own template for this field.
First, convert this field to a template field then you’ll be able to control the templates which will be CheckBoxes.
Then, you will find these CheckBoxes enabled in the insert and edit tempaltes and disabled in the ItemTmplate. You just need to enable it.
The code: