当前位置:编程学习 > C#/ASP.NET >>

c#语言ASP.NET开发checkboxlist遍历

C#语言。  如图,  假如我选择了   周星驰   成龙。  点击确定后,在Label1中可以输出    你喜欢的明星是:周星驰 成龙

补充:上面是个checkboxlist
		
答案:方法一,大概是你要的功能。

方法二,实现了当你取消选择的时候,自动去掉Label1中的相关内容

根据你的需要选吧,不过思路都是差不多的。

 

方法一:

for (int i=0; i<checkboxlist1.Items.Count; i++)
         {
            if (checkboxlist1.Items[i].Selected)
            {
               Label1.Text += checkboxlist1.Items[i].Text + "<br />";
            }
         }

方法二:

private void CheckBoxList1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string DQChk="",StrDQChk="";//初始化当前选定的值
            string PreChk=""; //上一次选定的值
            if (ViewState["strChk"]!=null) PreChk=Convert.ToString(ViewState["strChk"]);
            if (PreChk!="")//不是第一次点选
            {
                for (int i=0;i<CheckBoxList1.Items.Count;i++)//得到当前选定值
                {
                    if (CheckBoxList1.Items[i].Selected) StrDQChk=CheckBoxList1.Items[i].Value+",";
                }
               
                if (StrDQChk.Length>((string)ViewState["strChk"]).Length)//增加选项时
                {
                    DQChk=CompStr(StrDQChk,(string)ViewState["strChk"]);
                }
                else//取消选项时
                {
                    DQChk=CompStr((string)ViewState["strChk"],StrDQChk);
                }
                ViewState["strChk"] = StrDQChk;
            }
            else//第一次点选
            {
                DQChk=CheckBoxList1.SelectedValue;
                ViewState["strChk"] = DQChk+",";
            }
        }

        //比较上次选定值和当前选定值的区别得出当前选项
        private string CompStr(string str1,string str2)
        {
            string re="";
            string[] arystr=str1.Split(',');
            for (int i=0;i<arystr.Length;i++)
            {
                if (str2.IndexOf(arystr[i]+",")==-1) re=arystr[i];
                break;
            }
            return re;
        }

   
 string str = "";
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected == true)
            {
                str += item.Text + " ,";
            }
        }
        Label1.Text = str;

string nstr="";

nstr=" 你喜欢的明星是:“+CheckBoxList1.text;

页面代码

 

<asp:CheckBoxList ID="cblStar" runat="server" Width="94px">
                <asp:ListItem Value="周星驰"></asp:ListItem>
                <asp:ListItem>成龙</asp:ListItem>
                <asp:ListItem>谢霆锋</asp:ListItem>
                <asp:ListItem>佟大为</asp:ListItem>
                <asp:ListItem>陆毅</asp:ListItem>
                <asp:ListItem>刘烨</asp:ListItem>
            </asp:CheckBoxList>
            <asp:Label ID="lblShow" runat="server"></asp:Label><br />
            <asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text=" 确 定 " />&nbsp;<br />

 

 

后台代码

 

 protected void btnOK_Click(object sender, EventArgs e)
    {
        string outStr="您喜欢的明星是:";
        for (int i = 0; i < cblStar.Items.Count; i++)
        {
            ListItem li = cblStar.Items[i];
            if (li.Selected)
            {
                outStr += li.Value + " ";
            }
        }
        lblShow.Text = outStr;
    }

上一个:asp与asp.net有什么区别?
下一个:asp编程的是用什么语言来实现编程

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,