如何将复选框中的项添加到ASP.NET的列表框中? [英] How to add items from check box into list box in ASP.NET?

查看:62
本文介绍了如何将复选框中的项添加到ASP.NET的列表框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//设计html源

<%@ Page Language =C#AutoEventWireup =trueCodeBehind =CheckBoxExercise.aspx.csInherits =DropDownListBoxControl.CheckBoxExercise%>



<!DOCTYPE html>







< title>



.auto-style1 {

宽度:100%;

}

.auto-style2 {

宽度:281px;

}

.auto-style3 {

宽度:108px;

}

.auto-style6 {

margin-bottom:0px;

}

.auto-style7 {

宽度:108px;

text-align:center;

身高:128px;

}

.auto-style8 {

text-align:left;

}

.auto-style9 {

font-size:x-large;

}

.auto-style10 {

宽度:281px;

身高:128px;

}

.auto-style11 {

身高:128px;

}

.auto-style12 {

宽度:108px;

字体大小:x-large;

}

.auto-style13 {

宽度:281px;

字体大小:x-large;

}









//design html source
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxExercise.aspx.cs" Inherits="DropDownListBoxControl.CheckBoxExercise" %>

<!DOCTYPE html>



<title>

.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 281px;
}
.auto-style3 {
width: 108px;
}
.auto-style6 {
margin-bottom: 0px;
}
.auto-style7 {
width: 108px;
text-align: center;
height: 128px;
}
.auto-style8 {
text-align: left;
}
.auto-style9 {
font-size: x-large;
}
.auto-style10 {
width: 281px;
height: 128px;
}
.auto-style11 {
height: 128px;
}
.auto-style12 {
width: 108px;
font-size: x-large;
}
.auto-style13 {
width: 281px;
font-size: x-large;
}










< asp:Panel ID =Panel1 runat =serverHeight =168pxWidth =281px>




<asp:Panel ID="Panel1" runat="server" Height="168px" Width="281px">


     ;&NBSP;&NBSP;&NBSP;&NBSP;& NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;      

< asp:CheckBoxList ID =lstCoursesrunat =serverAutoPostBack =TrueHeight =40pxWidth =260px>




                                                               
<asp:CheckBoxList ID="lstCourses" runat="server" AutoPostBack="True" Height="40px" Width="260px">





< asp:Button ID =btnAddrunat =serverCssClass =auto-style6OnClick =btnAdd_ClickText => />







< asp:按钮ID =btnAllrunat =serverOnClick = btnAll_ClickText =>> />

< asp:Panel ID =Panel2runat =serverHeight =168pxWidth =267px >

< asp:ListBox ID =lstAddItemrunat =serverHeight =145pxSelectionMode =MultipleWidth =221px>



    < asp:Label ID =lblDetailsrunat =serverCssClass =auto-style9>

    < asp:Button ID =btnRemoverunat =serverCssClass =auto-style9OnClick =btnRemove_ClickText =Remove/>

   

< asp:按钮ID =btnRemove0runat =serverCssClass =auto-style9Text =全部清除/>







<asp:Button ID="btnAdd" runat="server" CssClass="auto-style6" OnClick="btnAdd_Click" Text=">" />




<asp:Button ID="btnAll" runat="server" OnClick="btnAll_Click" Text=">>" />
<asp:Panel ID="Panel2" runat="server" Height="168px" Width="267px">
<asp:ListBox ID="lstAddItem" runat="server" Height="145px" SelectionMode="Multiple" Width="221px">

    <asp:Label ID="lblDetails" runat="server" CssClass="auto-style9">
    <asp:Button ID="btnRemove" runat="server" CssClass="auto-style9" OnClick="btnRemove_Click" Text="Remove" />
   
<asp:Button ID="btnRemove0" runat="server" CssClass="auto-style9" Text="Clear All" />






我尝试过:





What I have tried:

code file..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DropDownListBoxControl
{
    public partial class CheckBoxExercise : System.Web.UI.Page
    {
        List<listitem> courses = new List<listitem>()
        {
            new ListItem {Text="ASP.NET", Value="3000" },
            new ListItem {Text="SQL Server", Value="2000" },
            new ListItem {Text="MVC",Value="1400" }
        };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (var item in courses)
                {
                    lstCourses.Items.Add(item);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lstAddItem.Items.Clear();
            if(lstAddItem.Items.Contains(lstCourses.SelectedItem))
            {
                lblDetails.Text = "Course is Exists...";

            }
            else
            {                        
           lstAddItem.Items.Add(lstCourses.SelectedItem);
                
            }
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            lstAddItem.Items.Clear();
            foreach(var item in courses)
            {
                lstAddItem.Items.Add(item);
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            
            lstAddItem.Items.Remove(lstAddItem.SelectedItem);
            lblDetails.Text = "Course Removed ......";

        }
    }
}

推荐答案

检查更正



check the corrections

List<ListItem> courses = new List<ListItem>()
       {
           new ListItem {Text="ASP.NET", Value="3000" },
           new ListItem {Text="SQL Server", Value="2000" },
           new ListItem {Text="MVC",Value="1400" }
       };
       protected void Page_Load(object sender, EventArgs e)
       {

           if (!Page.IsPostBack)
           {
               foreach (var item in courses)
               {
                   lstCourses.Items.Add(item);
               }
           }
       }

       protected void btnAdd_Click(object sender, EventArgs e)
       {
           lstAddItem.Items.Clear();
           foreach (ListItem item in lstCourses.Items)
           {
               if (item.Selected)
                   lstAddItem.Items.Add(item);
           }


       }

       protected void btnAll_Click(object sender, EventArgs e)
       {
           lstAddItem.Items.Clear();
           foreach (var item in courses)
           {
               item.Selected = true;
               lstAddItem.Items.Add(item);
           }
           foreach (ListItem item in lstCourses.Items)
               item.Selected = true;

       }

       protected void btnRemove_Click(object sender, EventArgs e)
       {

           lstAddItem.Items.Remove(lstAddItem.SelectedItem);
           lblDetails.Text = "Course Removed ......";

       }
       protected void btnRemoveAll_Click(object sender, EventArgs e)
       {

           lstAddItem.Items.Clear();
           lblDetails.Text = "Items Cleared......";

       }


如果我是正确的,你想要的是获得一个选中的复选框列表并添加一些控件的特定属性到列表框。



您可以在添加按钮点击事件中尝试这样的事情:

If I am correct, what you want is to get a list of selected checkboxes and add some specific attribute of the control to a list-box.

You can try something like this in your add button click event:
foreach (Control control in form1.Controls)
        {
            if (control is CheckBox && control.Checked)
            {
                //add the item to your list.
            }
        }



这只是代码的粗略骨架,你可能需要稍微调整它才能工作。



祝你有个美好的一天。


It is just a rough skeleton of the code, you may have to tweak a bit for it to work.

Have a good day.


这篇关于如何将复选框中的项添加到ASP.NET的列表框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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