We help IT Professionals succeed at work.

How to set the checked property of an ItemTemplate checkbox of a datalist

Medium Priority
789 Views
Last Modified: 2012-10-06
<asp:DataList ID="dlstEvalItems" runat="server" DataKeyField="EvalItemID"
                            DataMember="DefaultView" DataSourceID="dscEvalItems"
                            BorderColor="Black" BorderStyle="Solid"  BackColor="#FFFF99"
                            Caption="Financials" CaptionAlign="Top"
                            HeaderStyle-BackColor="#FFFF99" HeaderStyle-BorderColor="Black"
                            HeaderStyle-BorderStyle="Solid" SelectedItemStyle-BackColor="#33CC33"
                            SeparatorStyle-BorderStyle="Solid"
                            OnItemCommand="EvaluateItem"

                            GridLines="Both"
                               CellPadding="3"
                               CellSpacing="0"  
                             Font-Size="Medium">
                       
                            <ItemTemplate>
                                           
                                      <%# Eval("EvalItemName")%>
                                         
                                      <asp:CheckBox ID="chkEvaluatedItem" runat="server" />
                                      <asp:Button ID="btnItemSelect" runat="server" Text="Select"  CommandName="EvaluateItem" />
                         
                            </ItemTemplate>
                                   
 </asp:DataList>


Code behind:
 protected void cmdSaveResults_Click(object sender, EventArgs e)
   {
       Control chkEvalItem = FindControl("chkEvaluatedItem");
// Need to set the checked property to true

Thanks,
Comment
Watch Question

Commented:
Please have a look at this sample

Author

Commented:
It works until I get here:

       // EvalComplete checkbox = checked.

       string Dl = "Datalist Items Checked:<br />";
       foreach (DataListItem dli in dlstEvalItems.Items)
       {
           CheckBox chk = (CheckBox)dli.FindControl("chkEvaluated");
           if (chk.Checked)  //Object reference not set to an instance of an object.
           // Use the new keyword
           {
               Dl += (chk.Text + "<br />");
           }
       }
       DlLiteral.Text = Dl;

What do I need to change?

Thanks,

Author

Commented:
My mistake,

CheckBox chk = (CheckBox)dli.FindControl("chkEvaluated");

should have been
CheckBox chk = (CheckBox)dli.FindControl("chkEvaluatedItem");


Thanks,

Author

Commented:
My mistake,

CheckBox chk = (CheckBox)dli.FindControl("chkEvaluated");

should have been
CheckBox chk = (CheckBox)dli.FindControl("chkEvaluatedItem");


Thanks,

Explore More ContentExplore courses, solutions, and other research materials related to this topic.