不是的CheckBoxList选择的设置与视图状态禁用 [英] CheckboxList not setting Selected with Viewstate disabled

查看:164
本文介绍了不是的CheckBoxList选择的设置与视图状态禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个似乎正确的加载和所做的一切,只是当我做了回发了的CheckBoxList,它不会有 Item.Selected 属性集。我已经视图状态整个页面禁用。

I have a CheckboxList that seems to load and do everything right, except for when I do a postback, it will not have the Item.Selected property set. I have viewstate disabled for the entire page.

我加载它,像这样(在的Page_Load 每个负载):

I load it like so(inside Page_Load on every load):

foreach (DataRow service in d.Tables[0].Rows)
{
  cblServices.Items.Add(new ListItem((string)service["description"], service["id"].ToString()));
}

我的标记很简单:

My markup is simple:

<asp:CheckBoxList runat="server" ID="cblServices" Width="300px"></asp:CheckBoxList>

然后,我用的基本是这样的(在服务器端_Click事件按钮)

and then, I use basically something like this(in a _Click serverside event for a button)

foreach(ListItem item in cblServices.Items){
  if(item.Selected){
    MyLabel.Text+="selected: "+item.Value+item.Text;
  }
}

和MyLabel从未加入任何文字。我可以用它确实达到_Click的foreach循环调试验证,但没有项目被选中过。可能是什么这个原因?

and MyLabel never has any text added to it. I can verify with the debugger that it does reach the _Click's foreach loop, but no item is ever selected. What could be the cause of this?

推荐答案

如果你填充它在每次调用的Page_Load,不仅当 Page.IsPostback = FALSE 然后你CH375复位回发客户选择。

If you're filling it on every Page_Load call, not only when Page.IsPostback = false then you're reseting the client selection on postback.

修改
您应该添加在preINIT或Init事件您的项目,那么你就能够正确地保持选定的项目。

EDIT You should add your items on the PreInit or Init event, then you'll be able to keep the selected items correctly.

protected void Page_Init(object sender, EventArgs e)
{
    foreach (DataRow service in d.Tables[0].Rows)
    ...
}

这篇关于不是的CheckBoxList选择的设置与视图状态禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆