Alt*_*our 3 asp.net checkboxlist
我试图从复选框列表中获取多个值并将它们添加到列表中,但是即使列表包含适当的计数值,选中的值也始终为false.
要填充的代码:
Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
HelpingOthersEntities helpData = new HelpingOthersEntities();
List<LookupStoreLocationsByUserName> theDataSet = helpData.LookupStoreLocationsByUserName(userGuid).ToList<LookupStoreLocationsByUserName>();
locCkBox.DataSource = theDataSet;
locCkBox.DataTextField = "slAddress";
locCkBox.DataValueField = "storeLocationID";
locCkBox.DataBind();
Run Code Online (Sandbox Code Playgroud)
添加到列表的代码:
List<int> locList = new List<int>();
for (int x = 0; x < locCkBox.Items.Count; x++){
if(locCkBox.Items[x].Selected){
locList.Add(int.Parse(locCkBox.Items[x].Value));
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我无法进入items.selected
我的价值总是假的.
我试过填充回发的复选框,但我得到相同的结果.我的列表给了我适当.Count数量的值,但是items.selected= false?
我已经尝试过foreach循环添加到列表中,但我反复得到相同的结果.我错过了什么活动吗?
Nei*_*l F 11
我将在这里猜测并说你的代码在页面加载事件中被调用,所以你有类似下面这样的东西.
private void Page_Load()
{
Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
HelpingOthersEntities helpData = new HelpingOthersEntities();
List<LookupStoreLocationsByUserName> theDataSet = helpData.LookupStoreLocationsByUserName(userGuid).ToList<LookupStoreLocationsByUserName>();
locCkBox.DataSource = theDataSet;
locCkBox.DataTextField = "slAddress";
locCkBox.DataValueField = "storeLocationID";
locCkBox.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,那么您有效地在每个请求上写回发回数据.要对此进行排序,您只需要在不是回发时执行数据绑定,因此您需要将上面的代码更改为
private void Page_Load()
{
if (!IsPostBack)
{
Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
HelpingOthersEntities helpData = new HelpingOthersEntities();
List<LookupStoreLocationsByUserName> theDataSet = helpData.LookupStoreLocationsByUserName(userGuid).ToList<LookupStoreLocationsByUserName>();
locCkBox.DataSource = theDataSet;
locCkBox.DataTextField = "slAddress";
locCkBox.DataValueField = "storeLocationID";
locCkBox.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您应该能够测试项目的Selected属性.我也可能会更改你用来测试所选代码的代码
List<int> locList = new List<int>();
foreach(var item in locCkBox.Items)
{
if(item.Selected)
{
locList.Add(int.Parse(item.Value));
}
}
Run Code Online (Sandbox Code Playgroud)
或者如果您的.NET版本具有LINQ可用
List<int> locList = new List<int>();
(from item in locCkBox.Items where item.Selected == true select item).ForEach(i => locList.Add(i.Value));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6304 次 |
| 最近记录: |