如何从 CheckboxList 获取更新(更改的项目)?
我的页面中有一个复选框列表,其数据源在 PreLoad() 事件中以编程方式设置:
protected void Page_PreLoad()
{
if (!Page.IsPostBack)
{
CheckBoxList1.DataSource = NoK.AcceptedNoks((Guid)Membership.GetUser().ProviderUserKey);
CheckBoxList1.DataTextField = "FullName";
CheckBoxList1.DataValueField = "NoKId";
CheckBoxList1.DataBind();
}
foreach (ListItem chk in CheckBoxList1.Items)
{
if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value))
{
chk.Selected = true;
}
}
}
正如您在 foreach 中看到的那样,将检查是否必须检查某个项目。而且效果很好。这意味着最终用户可以编辑列表项目,并且默认情况下某些项目已被选中。现在我想通过单击按钮来获取项目:
protected void UpdateRightBtn_Click(object sender, EventArgs e)
{
var SelectedNokIds =
CheckBoxList1.Items
.OfType<ListItem>()
.Where(li =>
li.Selected == true)
.Select(l => new Guid(l.Value));
}
但是 SelectedNokIds
中的项目仍然是旧项目,如果用户更改复选框,则 SelectedNokIds
中不会出现任何效果。 为什么???
请帮忙!
I have a Checkbox list in my page and its datasource set programatically in PreLoad() event:
protected void Page_PreLoad()
{
if (!Page.IsPostBack)
{
CheckBoxList1.DataSource = NoK.AcceptedNoks((Guid)Membership.GetUser().ProviderUserKey);
CheckBoxList1.DataTextField = "FullName";
CheckBoxList1.DataValueField = "NoKId";
CheckBoxList1.DataBind();
}
foreach (ListItem chk in CheckBoxList1.Items)
{
if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value))
{
chk.Selected = true;
}
}
}
as you see in foreach will check for whether an item must be checked or Not. and it works nice. this means that end-user can Edit list Items and by default some of Item has been checked. now I want to get items by clicking a Button:
protected void UpdateRightBtn_Click(object sender, EventArgs e)
{
var SelectedNokIds =
CheckBoxList1.Items
.OfType<ListItem>()
.Where(li =>
li.Selected == true)
.Select(l => new Guid(l.Value));
}
but the Items in SelectedNokIds
are still Old Items and if user change checkboxes no effect apeares in SelectedNokIds
. Why???
Please Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来这是因为您在回发时再次重新设置值,从而有效地清除了用户的选择。仅当不是回发时才需要初始化这些值。
It looks like it is because you are re-setting the values again at postback, effectively clearing the user's selection. You need to only initialize the values when it is not a postback.
foreach (ListItem chk in CheckBoxList1.Items){ if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value)) { chk.Selected = true; } 此行在每次页面加载时都会
触发,这样就会重置选择(至少是那些将 selected 设置为 true 的选择)。那不应该也在 !Page.IsPostback 内吗?在更新按钮中,您可以在那里重新绑定...
如果您需要弄清楚发生了什么变化,您需要再次查询数据源中的项目,并根据新的选择列表交叉引用这些项目。
foreach (ListItem chk in CheckBoxList1.Items){ if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value)) { chk.Selected = true; }}
This line fires on every page load, so that would reset the selection (at least the ones that set selected to true). Shouldn't that be within !Page.IsPostback too? And in the update button, you could rebind there...
If you need to figure out what changed, you need to query the items in the data source again, and cross-reference those against the new selection list.