我正在使用qazxsw poi,它的qazxsw poi:insert,我想默认选中它的复选框,用户可以将其更改为未选中,但要绑定复选框我们应该使用
DetailsView
这样就可以取消选中复选框的默认状态,那么我该如何解决呢?
如果希望在数据绑定时选中复选框,则可以为复选框的文本属性赋值。
DefaultMode
在后面的代码上,您可以访问文本值以将其保存到DB中
Checked='<%# Bind("Cit_Visible") %>'
当使用DetailsView数据控件并且你有复选框值时,你可能会开始使用asp:CheckBoxField来处理所有的显示模式。如果要保留复选框绑定,但也可以将默认值设置为检查插入,则可以执行以下操作。
将字段转换为TemplateField,可以通过visual studio的设计视图完成,也可以通过替换此类型的块来手动完成。
<asp:CheckBox ID="chl" runat="Server" Checked="true" Text="<%# Bind('Cit_Visible') %>" />
用这样的代码块
CheckBox MyCheckbox = new CheckBox();
MyCheckbox = (CheckBox)DetailsView1.FindControl("chl");
Response.Write(MyCheckbox.Checked);
然后设置要检查的复选框默认值,您可以在代码隐藏中执行此操作
<asp:CheckBoxField DataField="Information" HeaderText="Information" SortExpression="Information" />
C#(从VB转换而来
<asp:TemplateField HeaderText="Information" SortExpression="Information">
<EditItemTemplate>
<asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' />
</InsertItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
当支持数据库值是非空位字段时,这显然是最佳的
使用TemplateField:
Protected Sub dvInformation_PreRender(sender As Object, e As EventArgs) Handles dvInformation.PreRender
If CType(sender, DetailsView).CurrentMode = DetailsViewMode.Insert Then
Dim chk As Object = CType(sender, DetailsView).FindControl("chkInformation")
If chk IsNot Nothing AndAlso chk.GetType Is GetType(CheckBox) Then
CType(chk, CheckBox).Checked = True
End If
End If
End Sub
在Init方法中设置复选框默认值:
protected void dvInformation_PreRender(object sender, EventArgs e)
{
if (((DetailsView)sender).CurrentMode == DetailsViewMode.Insert) {
object chk = ((DetailsView)sender).FindControl("chkInformation");
if (chk != null && object.ReferenceEquals(chk.GetType(), typeof(CheckBox))) {
((CheckBox)chk).Checked = true;
}
}
}