开发者

Customize CheckBox in DetailsView

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" />
            </Sele开发者_如何学GoctParameters>
            <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:

<asp:TemplateField HeaderText="view" SortExpression="view">
    <InsertItemTemplate>
        <asp:CheckBox ID="viewCheckBox" runat="server" Checked='<%# Bind("view") %>' />
    </InsertItemTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="viewCheckBox" runat="server" Checked='<%# Bind("view") %>' 
            Enabled="true" />
    </ItemTemplate>
</asp:TemplateField>


Assuming the parameters and the sql statement are properly set adding DataField="yourField" to the Checkbox line will save the value of the check box to the database.

<EditItemTemplate>
  <asp:CheckBox ID="CheckBox1" DataField="yourField" runat="server" checked='<%# Bind("yourField")%>'></asp:CheckBox>
</EditItemTemplate> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜