ASP.Net/Asp Control/CheckBoxList — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:53, 26 мая 2010
Содержание
Define and use asp checkboxlist in code behind (C#)
<%@ Page language="c#" src="CheckBoxTest.aspx.cs" AutoEventWireup="false" Inherits="CheckBoxTest" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
Choose your favourite programming languages:<br>
<br>
<asp:CheckBoxList id="chklst" runat="server" /><br>
<br>
<asp:Button id="cmdOK" Text="OK" runat="server" /><br>
<br>
<asp:Label id="lblResult" runat="server" />
</form>
</body>
</HTML>
<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class CheckBoxTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.CheckBoxList chklst;
protected System.Web.UI.WebControls.Button cmdOK;
protected System.Web.UI.WebControls.Label lblResult;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack == false)
{
chklst.Items.Add("C");
chklst.Items.Add("C++");
chklst.Items.Add("C#");
chklst.Items.Add("Visual Basic 6.0");
chklst.Items.Add("VB.NET");
chklst.Items.Add("Pascal");
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmdOK_Click(object sender, System.EventArgs e)
{
lblResult.Text = "You chose:<b>";
foreach (ListItem lstItem in chklst.Items)
{
if (lstItem.Selected == true)
{
// Add text to label.
lblResult.Text += "<br>" + lstItem.Text;
}
}
lblResult.Text += "</b>";
}
}
--%>
Get selected item from asp:checkboxlist (C#)
<script runat="server" language="C#">
void Page_Load()
{
string msg = "You have selected the following items:<br />";
if (check1.Items[0].Selected) {
msg = msg + check1.Items[0].Text + "<br />";
}
if (check1.Items[1].Selected) {
msg = msg + check1.Items[1].Text + "<br />";
}
if (check1.Items[2].Selected) {
msg = msg + check1.Items[2].Text + "<br />";
}
Message.Text = msg;
}
</script>
<html>
<head>
<title>Check Box List Example</title>
</head>
<body>
<asp:label id="Message" runat="server" />
<br /><br />
Which city do you wish to look at hotels for?
<br /><br />
<form runat="server">
<asp:checkboxlist id="check1" runat="server">
<asp:listitem id="option1" runat="server" value="Madrid" />
<asp:listitem id="option2" runat="server" value="Oslo" />
<asp:listitem id="option3" runat="server" value="Lisbon" />
</asp:checkboxlist>
<br /><br />
<input type="Submit">
</form>
</body>
</html>
Get selected value from asp:checkboxlist (VB.net)
<script runat="server" language="vb">
Sub Page_Load()
Dim msg As String = "You have selected the following items:<br />"
If check1.Items(0).Selected Then msg = msg & check1.Items(0).Text & "<br />"
If check1.Items(1).Selected Then msg = msg & check1.Items(1).Text & "<br />"
If check1.Items(2).Selected Then msg = msg & check1.Items(2).Text & "<br />"
Message.Text = msg
End Sub
</script>
<html>
<head>
<title>Check Box List Example</title>
</head>
<body>
<asp:label id="Message" runat="server" />
<br /><br />
Which city do you wish to look at hotels for?
<br /><br />
<form runat="server">
<asp:checkboxlist id="check1" runat="server">
<asp:listitem id="option1" runat="server" value="Madrid" />
<asp:listitem id="option2" runat="server" value="Oslo" />
<asp:listitem id="option3" runat="server" value="Lisbon" />
</asp:checkboxlist>
<br /><br />
<input type="Submit">
</form>
</body>
</html>
Loop through asp:checkboxlist (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim MyItem as ListItem
lblMessage.Text = ""
For Each MyItem in cbl1.Items
If MyItem.Selected = True Then
lblMessage.Text = lblMessage.Text _
& MyItem.Text & "<BR>"
End If
Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic CheckBoxList Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
/>
<BR><BR>
<asp:checkboxlist
id="cbl1"
runat="server"
cellpadding="5"
cellspacing="5"
repeatcolumns="3"
repeatdirection="Horizontal"
repeatlayout="Table"
textalign="Right"
>
<asp:ListItem value="12">Blue</asp:ListItem>
<asp:ListItem value="11">Red</asp:ListItem>
<asp:ListItem value="2">Green</asp:ListItem>
<asp:ListItem value="32">Purple</asp:ListItem>
<asp:ListItem value="8">Black</asp:ListItem>
<asp:ListItem value="15" Selected>Gold</asp:ListItem>
</asp:checkboxlist>
<BR><BR>
<asp:button
id="butOK"
text="OK"
type="Submit"
onclick="SubmitBtn_Click"
runat="server"
/>
</form>
</BODY>
</HTML>
Set asp:CheckBoxList Repeat Direction (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<title>Selection Control Example</title>
<script runat="server">
Sub Page_Load()
MyCheckBox1.Checked = True
MyRadioButton1.Checked = False
MyListBox.SelectionMode = ListSelectionMode.Multiple
MyDropDownList.SelectedIndex = 1
MyCheckBoxList.RepeatDirection = RepeatDirection.Horizontal
MyRadioButtonList.RepeatLayout = RepeatLayout.Table
End Sub
</script>
</head>
<body>
<h1>Selection Control Example</h1>
<form runat="server">
<asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
<asp:tablerow runat="server">
<asp:tablecell runat="server">
CheckBox Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:checkbox id="MyCheckBox1"
text="Vanilla" runat="server" />
<asp:checkbox id="MyCheckBox2"
text="Chocolate" runat="server" />
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
RadioButton Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:radiobutton id="MyRadioButton1" groupname="Group1"
checked=True text="Yes" runat="Server"/>
<asp:radiobutton id="MyRadioButton2" groupname="Group1"
text="No" runat="Server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
ListBox Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:listbox id="MyListBox" runat="server">
<asp:listitem value="Vanilla" selected="true">Vanilla</asp:listitem>
<asp:listitem value="Chocolate">Chocolate</asp:listitem>
<asp:listitem value="Strawberry">Strawberry</asp:listitem>
</asp:listbox>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
DropDownList Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server">
<asp:listitem value="Single" selected="true">Single</asp:listitem>
<asp:listitem value="Multiline">Multiline</asp:listitem>
<asp:listitem value="Password">Password</asp:listitem>
</asp:dropdownlist>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
CheckBoxList Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:checkboxlist id="MyCheckBoxList"
repeatdirection="vertical" runat="server">
<asp:listitem value="Vanilla" text="Vanilla"/>
<asp:listitem value="Chocolate" text="Chocolate"/>
<asp:listitem value="Strawberry" text="Strawberry"/>
</asp:checkboxlist>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
RadioButtonList Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:radiobuttonlist id="MyRadioButtonList" repeatdirection="Horizontal" runat="server">
<asp:listitem value="Female" text="Female" selected="true"/>
<asp:listitem value="Male" text="Male"/>
</asp:radiobuttonlist>
</asp:tablecell>
</asp:tablerow>
</asp:table>
</form>
</body>
</html>