1

I have a web page where a checkboxlist is created dynamically from a XML file.

The XML looks like this:

<properties>
   <property id="abc" value="150" text="mumbo jumbo" />
   <property id="def" value="150" text="more mumbo jumbo" />

I am creating my checkboxlist like this:

if (checkBoxes.Count > 0)
{
    chkServices.DataSource = checkBoxes;
    chkServices.DataTextField = "text";
    chkServices.DataValueField = "value";
    chkServices.DataBind();
}

I would like to get the id going with those individual checkboxes but can't seem to figure it out.

Any ideas?

4

2 回答 2

1

在 DataBind 之后是这样的:

foreach (ListItem c in chkServices.Items)
{
    c.Attributes.Add("ID", "abc");
}
于 2013-10-02T05:48:47.203 回答
1

如果你创建一个 CheckboxList 控件并给它一个“test”的 ID,那么它应该给列表中的复选框一个 ID,格式为 parentid + _ + itemindex,所以“test_0”、“test_1”、“test_2”等。

请记住,要获取每个复选框的 ID 以便在 javascript 中使用它,您可能需要使用 CheckboxList.ClientID + _ + itemindex。

于 2013-10-02T05:30:13.087 回答