开发者

Setting checkbox to be checked/unchecked depending on the column result in 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='<%#"£&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>


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"]);
     }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜