多个RadioButtonList和checkbuttonlist做选择项后处理选择值

maslino 2009-04-27 01:31:18
页面中有多个RadioButtonList和checkbuttonlist,目前测试是用控件的SelectedIndexChanged 来处理选择项的值。
这样以来每个控件几乎都要重复这一段代码,那位达人弄个处理的函数什么都,只要是RadioButtonList和checkbuttonlist这出动作就是执行并返回值。
我努力过,但是不会在控件名变化后,改变分、当前控件的名称,获取该值。

我目前用的RAD的事件代码
this.Label1.Text = this.RadioButtonList1.SelectedValue;

chk的时间代码

this.Label3.Text = "";
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected == true)
{

this.Label3.Text += this.CheckBoxList1.Items[i].Value + "|";
}
}

...全文
155 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
maslino 2009-04-28
  • 打赏
  • 举报
回复
谢谢了,再提示之下差不多达到美满的结果
蓝海D鱼 2009-04-27
  • 打赏
  • 举报
回复
这样作 是最好的,前面的 不能体现取消的效果,每次都遍历 一下 所有控件的 选择 状态

protected void Button1_Click(object sender, EventArgs e)
{
string ss="";
GetCheckedinfo(CheckBoxList1,ref ss);
GetCheckedinfo(CheckBoxList2, ref ss);
GetCheckedinfo(this.RadioButtonList1, ref ss);
this.Label1.Text = ss;
}
private void GetCheckedinfo(ListControl mycontrol,ref string ss)
{
int count = mycontrol.Items.Count;
for (int i = 0; i < count; i++)
{
if (mycontrol.Items[i].Selected)
{
ss += mycontrol.Items[i].Value + "|";
}
}
}
蓝海D鱼 2009-04-27
  • 打赏
  • 举报
回复
Button1 加点击事件

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


把ss作为 页面类的变量 就可以了

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
private string ss = "";
protected void Page_Load(object sender, EventArgs e)
{

}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListControl mycontrol = (ListControl)sender;
for (int i = 0; i < mycontrol.Items.Count; i++)
{
if (mycontrol.Items[i].Selected == true)
{

ss += mycontrol.Items[i].Value + "|";
}
}

}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = ss;
}
}

maslino 2009-04-27
  • 打赏
  • 举报
回复
这样些MS只能得到最后一个相应控件的值,我是要保存所有的控件的值!
蓝海D鱼 2009-04-27
  • 打赏
  • 举报
回复
两种控件都是 自ListControl 继承,可以一起操作,下面的代码是 示例, 具体怎么处理 你加一下就好了, 用同一个事件 响应 多个控件的 事件


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
<asp:ListItem>111</asp:ListItem>
<asp:ListItem>222</asp:ListItem>
<asp:ListItem>333</asp:ListItem>
<asp:ListItem>444</asp:ListItem>
<asp:ListItem>555</asp:ListItem>
<asp:ListItem>666</asp:ListItem>
</asp:CheckBoxList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:CheckBoxList ID="CheckBoxList2" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
<asp:ListItem>2111</asp:ListItem>
<asp:ListItem>2222</asp:ListItem>
<asp:ListItem>2333</asp:ListItem>
<asp:ListItem>2444</asp:ListItem>
<asp:ListItem>2555</asp:ListItem>
<asp:ListItem>2666</asp:ListItem>

</asp:CheckBoxList>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
<asp:ListItem>222</asp:ListItem>
<asp:ListItem>333</asp:ListItem>
<asp:ListItem>444</asp:ListItem>
<asp:ListItem>555</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListControl mycontrol = (ListControl)sender;
string ss = "";
for (int i = 0; i < mycontrol.Items.Count; i++)
{
if (mycontrol.Items[i].Selected == true)
{

ss += mycontrol.Items[i].Value + "|";
}
}
this.Label1.Text = ss;

}
}



111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧