我在checkboxlist中添加了一些自定义属性,但我想知道为什么我无法检索自定义属性的值。它将像这篇文章jQuery - find control using custom attribute。但我想要的是通过自动回发和后面的代码检索。
int temp = 0;
foreach (DataRow rows1 in ds_ss_equipments_data.Tables[0].Rows)
{
cblEquip.Items.Add(new ListItem(rows1["name"].ToString() + " " + rows1["quota"].ToString() + " X " + rows1["price"].ToString(), rows1["id"].ToString()));
cblEquip.Items[temp].Attributes["price"] = rows1["price"].ToString();
cblEquip.Items[temp].Attributes["id"] = rows1["id"].ToString();
cblEquip.Items[temp].Attributes["quota"] = rows1["quota"].ToString();
temp += 1;
}
foreach (ListItem li in cblEquip.Items)
{
if (li.Selected)
{
equip += (Convert.ToDecimal(li.Attributes["price"]) * Convert.ToInt32(li.Attributes["quota"]));
}
}
答案 0 :(得分:3)
蒂姆提供的链接可能会解决您的问题。只是关于编程风格的说明。这个temp
变量看起来很奇怪。试试这个
foreach (DataRow rows1 in ds_ss_equipments_data.Tables[0].Rows)
{
var listItem = new ListItem(rows1["name"].ToString() + " " + ...)
listItem.Attributes["price"] = rows1["price"].ToString();
listItem.Attributes["id"] = rows1["id"].ToString();
listItem.Attributes["quota"] = rows1["quota"].ToString();
cblEquip.Items.Add(listItem);
}
更容易理解。
并替换此
rows1["name"].ToString() + " " + rows1["quota"].ToString() + " X " + rows1["price"].ToString()
通过这个
String.Format("{0} {1} X {2}", rows1["name"], rows1["quota"], rows1["price"])
创建项目看起来会更漂亮
string caption = String.Format(
"{0} {1} X {2}",
rows1["name"],
rows1["quota"],
rows1["price"]
);
var listItem = new ListItem(caption, rows1["id"].ToString())