高分求教怎样删除自定义控件中checkboxlist里面的item

Jiazi840207 Jiazi840207 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
怎样删除checkboxlist里面的item
我做了一个多选题题目控件,用于一个考试系统。每次提交答案后,我想把已有的item删除,于是我调用了clear()方法,但是控件并没有从页面中消失
下面是我的控件的代码
namespace hanlin.program.Components.problems
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

/// <summary>
/// MultiChoice 的摘要说明。
/// </summary>
public class MultiChoice : System.Web.UI.UserControl,IProblem
{
protected System.Web.UI.WebControls.Label txtproblem;
protected System.Web.UI.WebControls.CheckBoxList choices;
private char curindex='A';
private Int64 id;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
public Int64 uid
{
get
{
return id;
}
set
{
id=value;
}
}
public StringDictionary getChoices()
{
StringDictionary sd=new StringDictionary ();
foreach (ListItem li in choices.Items )
{

sd.Add (li.Text ,li.Value );
}
return sd;
}
/// <summary>
/// 用于自增题目选项的标号(a,b,c等)
/// </summary>
private char index
{
get
{
char i=curindex;
//curindex+=1;

return i;
}
}
private void IncreIndex()
{

curindex=(char)((byte)curindex+1);
}
/// <summary>
/// 为题目添加选项
/// </summary>
/// <param name="s"></param>
///

public void init()
{

//choices.Items.Clear ();


//choices.Controls .Clear ();
}
virtual public void addChoice(string s)
{

ListItem li=new ListItem (index.ToString ()+". "+s,index.ToString ());
IncreIndex();

choices.Items.Add (li);

}
/// <summary>
/// 题干
/// </summary>
virtual public string problem
{
get
{
return txtproblem.Text ;
}
set
{
txtproblem.Text =value;
}
}

/// <summary>
/// 获取所选的若干构成的标号字符串
/// </summary>
/// <param name="e"></param>
///
public string choice
{
get
{
string s=" ";
foreach (ListItem li in choices.Items )
{

if(li.Selected )s+=li.Value ;

}
curindex='A';
choices.Items.Clear ();//获取答案后,已有的选项就被删除
return s.Trim ();
}
set
{//

if(value==null)
return;
foreach (ListItem li in choices.Items )
{

if(value.IndexOf(li.Value )>=0)//contains it
li.Selected =true;
}
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

...全文

相关推荐

ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法

三个控件都有一个Items集合,可以用 RepeatLayout 和 RepeatDirection 属性来控制列表的呈现形式

weixin_38499732

获取checkboxlist选中的value和text

获取checkboxlist选中的value和text.

brio8425

vb.NET基础知识,案例,控件实用代码,全面基础

vb.NET(vb)基础知识,案例,控件实用代码,全面基础

前面是谁

asp.net 获取Datalist中Checkbox的值的小结

最近开发过程中遇到一个小问题,要获取checkbox的值,在网上搜索了一下,发现基本上都是用JS实现的,现在我将自己的做法记录一下,以便以后继续使用。

weixin_38643407

用户控件的创建

//default.aspx &lt;form id="form1" runat="server"&gt; 请输入书名: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="增加" /><br /> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br /> <asp:Button ID="Button2" runat="server" Text="删除" /> &lt;/form&gt; //default.aspx.cs public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { ListBox1.Items.Add(TextBox1.Text); } protected void Button2_Click(object sender, EventArgs e) { ListBox1.Items.Remove(ListBox1.Text); } } //default2.aspx &lt;form id="form1" runat="server"&gt; <div> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem Value="国家"></asp:ListItem> <asp:ListItem Value="民族"></asp:ListItem> <asp:ListItem Value="文化"></asp:ListItem> </asp:CheckBoxList> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox></div> <asp:Button ID="Button1" runat="server" Text="Button" /> &lt;/form&gt; //default2.aspx.cs public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) {

cangzhoulnn
评论 12
收藏 已收藏
分享
返回顶部
Jiazi840207
.NET社区 62,272 成员 668,968 内容