根据数据列表中的列结果将复选框设置为选中/取消选中

发布于 2024-11-08 18:30:25 字数 3164 浏览 0 评论 0原文

我在数据列表中有 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='<%#"£&nbsp;"+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") %>'  /> &nbsp; <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪少女 2024-11-15 18:30:25

首先,这应该可以工作:Checked='<%#Eval("offline") %>',如果它不起作用,则一定是另一个问题。

或者,您可以在 ItemDataBound 事件中执行相同的操作。喜欢..

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
 if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
       DataRow dr = ((DataRowView)e.Item.DataItem).Row;
       ((CheckBox)e.Item.FindControl("chkOffline")).Checked = Convert.ToBoolean(dr["chkOffline"]);
     }
    }
}

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

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
 if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
       DataRow dr = ((DataRowView)e.Item.DataItem).Row;
       ((CheckBox)e.Item.FindControl("chkOffline")).Checked = Convert.ToBoolean(dr["chkOffline"]);
     }
    }
}
卖梦商人 2024-11-15 18:30:25

您可以做的是,将值存储在而不是复选框中,然后在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文