在DetailsView中自定义CheckBox

时间:2011-04-30 21:20:47

标签: c# asp.net sql detailsview

我有一个包含CheckBoxField的DetailsView。问题是我希望复选框即使在只读模式和插入模式下也可以更改,默认情况下必须检查它。我该如何解决这个问题?

我附上一段代码:

   <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>

2 个答案:

答案 0 :(得分:1)

您无法使用CheckBoxField执行此操作,您必须为此字段创建自己的模板。

首先,将此字段转换为模板字段,然后您就可以控制将成为CheckBoxes的模板。

然后,您将在插入和编辑tempaltes中找到这些CheckBox,并在ItemTmplate中禁用。你只需要启用它。

代码:

<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>

答案 1 :(得分:0)

假设正确设置了参数和sql语句,将DataField =“ yourField”添加到Checkbox行将把复选框的值保存到数据库中。

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