简体   繁体   English

从数据库SQL Server 2012 asp.net C#填充Datalist ItemTemplate内的CheckBoxList

[英]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). 我在数据库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 : 我的文件.aspx部分是:

<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 : 我的文件.cs部分是:

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. 还要检查checklist是否为空。 If that is the case, you should use a recursive FindControl method you can easily find on StackOverflow 如果是这种情况,则应使用可以在StackOverflow上轻松找到的递归FindControl方法

In order to find the CheckBoxList, you must look for it in each item of the DataList. 为了找到CheckBoxList,必须在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. 我已将字段disease_id设置为每个复选框的值,但是如果愿意,可以放回disease_name

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. PopulateDiseases DataList1之后必须调用函数PopulateDiseases

An alternative approach would be to populate each CheckBoxList in the ItemDataBound event handler of the DataList. 一种替代方法是在DataList的ItemDataBound事件处理程序中填充每个CheckBoxList。 In that scenario, the disease table ( dt ) could be declared and filled outside of the event handler to avoid multiple calls to the database. 在这种情况下,可以声明疾病表( dt )并将其填充到事件处理程序之外,以避免对数据库的多次调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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