简体   繁体   中英

Populate CheckBoxList inside Datalist ItemTemplate from database SQL Server 2012 asp.net C#

I have a table of diseases in database MS SQL Server 2012. disease_id int disease_name varchar(100). What i want is to populate the checkboxlist from db. My file .aspx part is :

<div style="width: 100%; float: left">
    <br />
    <br />
    <asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" Width="350px">
        <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <ItemTemplate>
            <asp:CheckBoxList ID="chkbxlistDiseases" runat="server" RepeatDirection="Vertical" AutoPostBack="true" DataTextField='<%#Eval("disease_name") %>' DataValueField='<%#Eval("disease_name")%>'>
            </asp:CheckBoxList>
        </ItemTemplate>
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    </asp:DataList>
</div>

and My file .cs part is :

void PopulateDiseases()
{
    SqlConnection con = new SqlConnection(str_con);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("select disease_name from diseases", con);
    sda.Fill(dt);
    CheckBoxList checklist = DataList1.FindControl("chkbxlistDiseases") as CheckBoxList;
    foreach (DataRow dr in dt.Rows)
    {
        checklist.Items.Add(dr["0"].ToString());
    }
}

the error is :

 {"Object reference not set to an instance of an object."}

at this line of code

checklist.Items.Add(dr["0"].ToString());

Try using

checklist.Items.Add(dr["disease_name"].ToString());

or

checklist.Items.Add(dr[0].ToString());

Also check if checklist is null. If that is the case, you should use a recursive FindControl method you can easily find on StackOverflow

In order to find the CheckBoxList, you must look for it in each item of the DataList. The code below shows how you can do it. I have set the field disease_id as the value of each check box, but you can put back disease_name if you prefer.

The markup:

<ItemTemplate>
    <asp:CheckBoxList ID="chkbxlistDiseases" runat="server" RepeatDirection="Vertical" AutoPostBack="true" DataTextField='disease_name' DataValueField='disease_id' />
</ItemTemplate>

The code-behind:

void PopulateDiseases()
{
    SqlConnection con = new SqlConnection(str_con);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("select disease_id, disease_name from diseases", con);
    sda.Fill(dt);
    foreach (DataListItem item in DataList1.Items)
    {
        CheckBoxList checklist = item.FindControl("chkbxlistDiseases") as CheckBoxList;
        checklist.DataSource = dt;
        checklist.DataBind();
    }
}

The function PopulateDiseases must be called after DataList1 has been filled.

An alternative approach would be to populate each CheckBoxList in the ItemDataBound event handler of the DataList. In that scenario, the disease table ( dt ) could be declared and filled outside of the event handler to avoid multiple calls to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM