<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listbox.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> <h2 style=" color:blue; font-style:normal "> How to use ListBox and <br /> ClearSelection method in asp.net </h2> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Font-Size="Medium" Font-Italic="true"> <asp:ListItem Value="HI">HI</asp:ListItem> <asp:ListItem Value="HOW">HOW</asp:ListItem> <asp:ListItem Value="ARE">ARE</asp:ListItem> <asp:ListItem Value="YOU">YOU</asp:ListItem> </asp:ListBox><br /> <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor=" blue" ></asp:Label> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Select atleast one" ControlToValidate="ListBox1"></asp:RequiredFieldValidator><br /><br /> <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30" Font-Bold="true" ForeColor="blue" /> <asp:Button ID="Button2" runat="server" Text="Clear" onclick="Button2_Click" Height="30" Font-Bold="true" ForeColor="blue"/> </div> </form> </body> </html>
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { var sSkill = ""; ListBox project = FindControl("ListBox1") as ListBox; foreach (ListItem oItem in project.Items) { if (oItem.Selected) { if (sSkill.Length == 0) sSkill = oItem.Value; else sSkill = sSkill + " " + oItem.Value; } } Label1.Text = sSkill; } protected void Button2_Click(object sender, EventArgs e) { ListBox1.ClearSelection(); Label1.Text = ""; RequiredFieldValidator1.Enabled = false; } }
No comments:
Post a Comment