自定义DetailsView中的CheckBox
我有一个包含 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用 CheckBoxField 执行此操作,您必须为此字段创建自己的模板。
首先,将此字段转换为模板字段,然后您将能够控制将成为复选框的模板。
然后,您将发现这些复选框在插入和编辑模板中启用,而在 ItemTmplate 中禁用。您只需要启用它即可。
代码:
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:
假设参数和 sql 语句设置正确,将 DataField="yourField" 添加到 Checkbox 行会将复选框的值保存到数据库。
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.