Wednesday 1 May 2013

Listcontrols in ASP.NET


Listcontrols in ASP.NET

By Admin7. January 2013 13:07
Listcontrols:
1)listcontrol is a collection of listitems each listitem is assigned with a unique index
2)listitem will contain text & value, only text will be visible to user and value can be used with in programming
3)listcontrols can be used to manage more number of values, This makes developer job easier with in programming
4)asp.net supports following list controls
1)checkboxlist control
2)radiobuttonlist control
3)dropdownlist control
4)listbox control
5)bulletedlist control

Common properties to listcontrols:
1)items-it can be used to store listitems
2)selectedindex-it returns user selected listitem index
3)selectedvalue-it returns user selected listitem value
4)selecteditem.text-it returns user selected listitem text
5)items.count-it returns number of list items present within list control
6)repeatdirection-specify number of listitems to be displayed with in a row
7)repeatdirection-specify direction to display list items, Default is vertical
8)items[index].selected-it returns true if specified index listitem is selected by user else returns false
9)items[index].text-it returns specified index listitem text
10)items[index].value-it returns specified index listitem value

Note: a bove 8,9,10 properties can be used with the listcontrol supporting multiple listitems selection

Checkboxlist control:
1)Checkboxlist control is a collection of checkbox listitems
2)This control supports multiple listitems selection

Radiobutton list control:
1)It is a collection of radiobutton listitems
2)It allows user only single listitem selection

Example on listcontrols

Goto visual studio
Start->run->devenv
It will display main window of visual studio
File menu->new->website->visual c#->select asp.net empty website
Weblocation->e:\aspnet\peerssite[drive:\dir\websitename]
Visual studio create a folder with website name, in this folder website related files will be placed

Add webform
Goto view menu and select solution explorer
Right click on website path and select add new item
Select webform
Give name as register.aspx

Goto design part Add Name (Textbox), Courses(CheckboxList),Paymentmode(radiobutton list), Register(button) and Label control

Check box list1 properties:
Repeat columns-2
Items->…..[click]
                         
                 Click on add button
                      
          Listitem-text-asp.net
                           Value-3000
          Listitem-text-C#
                           Value-2000
                         Text-Vb.net
                          Value-2000
                         Text-sqlserver
                         Value-1000

Radiobuttonlist1 properties
  Repeatdirection-horizontal
         Items……[click]
                     
     Listitem-text-cash
                      Value-cash
     Listitem-text-creditcard
                      Value-creditcard
                      Text-cheque
                       Value-cheque

Register button clicklogic       

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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string courses = "";
        int totalfee = 0;
        string name = "";
       string pmode = "";
       name = TextBox1.Text;
       for (byte i = 0; i < CheckBoxList1.Items.Count; i++)
       {
           if (CheckBoxList1.Items[i].Selected)
           {
               courses = courses + CheckBoxList1.Items[i].Text + "<br>";
               totalfee = totalfee + int.Parse(CheckBoxList1.Items[i].Value);
           }
       }
       pmode = RadioButtonList1.SelectedValue;
       Label4.Text = "name:" + name + "<br>courses:" + courses + "<br>totalfee:" + totalfee + "<br>payment:" + pmode;
    }
}

No comments:

Post a Comment