根据数据列表中的列结果将复选框设置为选中/取消选中
我在数据列表中有 2 个复选框 [isSold(bit), Offline(bit)] ,我想根据该行的数据库结果将该复选框设置为选中或取消选中。例如,如果产品已售出,则已售出列将为 true ,因此应选中该复选框。我尝试使用下面的代码来达到所需的结果,但没有成功:
<asp:DataList ID="dlEditCaravans" runat="server"
RepeatDirection="Vertical" DataKeyField="makeID" OnEditCommand="edit"
OnDeleteCommand="delete" ItemStyle-CssClass="dleditCaravans"
onitemcommand="dlEditCaravans_ItemCommand"
onitemdatabound="dlEditCaravans_ItemDataBound">
<ItemTemplate>
<div class="imgeditCaravan">
<asp:Image runat="server" ID="caravanImage" ImageUrl='<%#String.Format("/images/caravans/{0}", Eval("image")) %>' Height="80" Width="80" />
</div>
<div class="lblmakeCaravan">
<asp:Label ID="lblMake" runat="server" Text='<%#Eval("make") %>' CssClass="lblheader"></asp:Label>
<br />
<asp:Label ID="imgDesc" runat="server" CssClass="lblDescription" Text='<%#(Eval("description").ToString().Length>350)?Eval("description").ToString().Substring(0,50)+ "....":Eval("description").ToString() + "...." %>'></asp:Label>
<br />
<asp:Label ID="lblPrice" runat="server" Text='<%#"£ "+Eval("Price")%>'></asp:Label>
<br />
</div>
<div class="editImage">
<asp:ImageButton ID="edit" runat="server" CommandName="edit" ImageUrl="~/images/newsControls/edit.gif" ToolTip="Edit Caravan"/>
<asp:ImageButton ID="delete" runat="server" CommandName="delete" ImageUrl="~/images/newsControls/delete.gif" ToolTip="Delete Caravan"/>
<br /> <br />
<asp:Button ID="btnAddToFeature" runat="server" Text="Add To Feautured Caravans" CommandName="AddToCaravans" Enabled="false" Width="210" Height="30" ForeColor="#1D91BD" ToolTip="Add Caravan To Feautured Caravans"/><br /><br />
<asp:Button ID="btnRemoveFeature" runat="server" Text="Remove From Feautured Caravans" CommandName="RemoveToCaravans" Enabled="false" ToolTip="Remove from Featured Caravans" Width="210" Height="30" ForeColor="#1D91BD" />
<br /> <br />
<asp:CheckBox runat="server" ID="chkOffline" Checked='<%#Eval("offline") %>' /> <label>Set This Caravan in Offline mode</label>
</div>
</ItemTemplate>
<SeparatorTemplate>
<div class="descSeparator"></div>
</SeparatorTemplate>
</asp:DataList>
I have got 2 check box[isSold(bit), Offline(bit)] in the datalist , I want to set that check box to be checked or unckecked depending on the database result of that row. For instance if the product is sold , the sold column will be true , therefore the checkbox should be checked. I tried to do acheive the required result by using code below with no success:
<asp:DataList ID="dlEditCaravans" runat="server"
RepeatDirection="Vertical" DataKeyField="makeID" OnEditCommand="edit"
OnDeleteCommand="delete" ItemStyle-CssClass="dleditCaravans"
onitemcommand="dlEditCaravans_ItemCommand"
onitemdatabound="dlEditCaravans_ItemDataBound">
<ItemTemplate>
<div class="imgeditCaravan">
<asp:Image runat="server" ID="caravanImage" ImageUrl='<%#String.Format("/images/caravans/{0}", Eval("image")) %>' Height="80" Width="80" />
</div>
<div class="lblmakeCaravan">
<asp:Label ID="lblMake" runat="server" Text='<%#Eval("make") %>' CssClass="lblheader"></asp:Label>
<br />
<asp:Label ID="imgDesc" runat="server" CssClass="lblDescription" Text='<%#(Eval("description").ToString().Length>350)?Eval("description").ToString().Substring(0,50)+ "....":Eval("description").ToString() + "...." %>'></asp:Label>
<br />
<asp:Label ID="lblPrice" runat="server" Text='<%#"£ "+Eval("Price")%>'></asp:Label>
<br />
</div>
<div class="editImage">
<asp:ImageButton ID="edit" runat="server" CommandName="edit" ImageUrl="~/images/newsControls/edit.gif" ToolTip="Edit Caravan"/>
<asp:ImageButton ID="delete" runat="server" CommandName="delete" ImageUrl="~/images/newsControls/delete.gif" ToolTip="Delete Caravan"/>
<br /> <br />
<asp:Button ID="btnAddToFeature" runat="server" Text="Add To Feautured Caravans" CommandName="AddToCaravans" Enabled="false" Width="210" Height="30" ForeColor="#1D91BD" ToolTip="Add Caravan To Feautured Caravans"/><br /><br />
<asp:Button ID="btnRemoveFeature" runat="server" Text="Remove From Feautured Caravans" CommandName="RemoveToCaravans" Enabled="false" ToolTip="Remove from Featured Caravans" Width="210" Height="30" ForeColor="#1D91BD" />
<br /> <br />
<asp:CheckBox runat="server" ID="chkOffline" Checked='<%#Eval("offline") %>' /> <label>Set This Caravan in Offline mode</label>
</div>
</ItemTemplate>
<SeparatorTemplate>
<div class="descSeparator"></div>
</SeparatorTemplate>
</asp:DataList>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,这应该可以工作:
Checked='<%#Eval("offline") %>'
,如果它不起作用,则一定是另一个问题。或者,您可以在
ItemDataBound
事件中执行相同的操作。喜欢..First of all, this should work:
Checked='<%#Eval("offline") %>'
, if it does not work, it must be a different problem.Alternatively you can do the same in the
ItemDataBound
event. like..您可以做的是,将值存储在而不是复选框中,然后在 ItemDatabound 事件中添加从标签中查找复选框的值,并将选中或未选中的值分配给复选框。
What you can do is, store the value in instead of the checkbox and then on the ItemDatabound event you can add find the value of the checkbox from the label and assign the value of checked or not to checkbox.