中继器中的动态Checkboxlist,ASP.NET [英] Dynamic Checkboxlist in repeater, ASP.NET

查看:82
本文介绍了中继器中的动态Checkboxlist,ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Repeater 中有一个 CheckBoxList ,而我的代码来自这里中继器ASP.NET中的动态下拉列表

I have a CheckBoxList in a Repeater and the code I have is from here Dynamic dropdownlist in repeater, ASP.NET.

如果这样做:

<asp:CheckBoxList ID="chklWorkType" runat="server" OnDataBinding="chklWorkType_DataBinding"></asp:CheckBoxList>


protected void chklWorkType_DataBinding(object sender, System.EventArgs e)
{
    CheckBoxList chk = (CheckBoxList)(sender);

    chk.Items.Add(new ListItem("nem 1", "1"));
    chk.Items.Add(new ListItem("num 2", "2"));

    chk.SelectedValue = chk.DataValueField;
}

这是我的错误消息:


System.ArgumentOutOfRangeException:'chklWorkType'的SelectedValue无效,因为它不在项目列表中。

System.ArgumentOutOfRangeException: 'chklWorkType' has a SelectedValue which is invalid because it does not exist in the list of items.


推荐答案

DataValueField 获取或设置数据字段提供每个列表项的值的源。所以通常是列名或类似名称。但是您将此名称用作 SelectedValue 不存在,因为您没有将其赋值,所以它是 String.Empty

如果要选择第一个项目,则可以使用它:

You could use this, if you want the first item to be selected:

chk.SelectedValue = "1";

这是第一个 ListItem new ListItem( nem 1, 1))。

当然,您也可以使用< a href = https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.listcontrol.selectedindex(v=vs.110).aspx rel = nofollow> SelectedIndex

Of course you could also use the SelectedIndex:

chk.SelectedIndex = 0;

这篇关于中继器中的动态Checkboxlist,ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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