Tuesday 29 May 2012

DropDownList


DropDownList is used to create a drop-down list in which each item is defined by listitems.
DropDownList is used when you want to bound the user such that he can select an option from a list of options.
Each item in drop-down list is defined by ListItem element.Inother words we can say that each item is created by ListItem control in asp.net.For example:    
<asp:dropdownlist ID="Dropdownlist1" runat="server">
        <asp:ListItem>United States</asp:ListItem>
        <asp:ListItem>Germany</asp:ListItem>
        <asp:ListItem>Japan</asp:ListItem>
        <asp:ListItem>China</asp:ListItem>
        <asp:ListItem>Russia</asp:ListItem>
        <asp:ListItem>Pakistan</asp:ListItem>
        <asp:ListItem>India</asp:ListItem>
        <asp:ListItem>Australia</asp:ListItem>
        <asp:ListItem>London</asp:ListItem>
        <asp:ListItem>Canada</asp:ListItem>
</asp:dropdownlist>    
Here you can see DropDownList contains a ListItem control from which you can add your values.
How to Add Values Manually in DropDownLIst?
To manually add values in drop-down list you can use the previious code else follow this picture you can also add values graphically.


How to Add Values Programmatically in DropDownLIst?
If you want to set values to drop-down list at run time then you can use the following code at page load event or some other place wher you want.

protected void Page_Load(object sender, EventArgs e)
        {
            RadioButtonList1.Items.Add(new ListItem("C#","C#"));
            RadioButtonList1.Items.Add(new ListItem("VB","VB"));
            RadioButtonList1.Items.Add(new ListItem("J#","J#"));
            RadioButtonList1.Items.Add(new ListItem("C++", "C++"));
        }



How to Add Values in DropDownLIst from Database?
if you want to get values from database and show them in drop-down list then use the following pocedure.
First of all place your DropDownList control into web form.After placing the control a small tag will appear with the control click on it.Then two options will appear select or click Choose Data Source.


if your data source already exists then select it else create a new data source.


Some Commonly Used Properties of DropDownList
Selected Value Property:
If you to get a selected value from the drop-down list then you will you the SelectedValue property.This property returns the string value of the selected item in drop-down list.
Label1.Text =  DropDownList.SelectedValue;


Selected Index Property:
SelectedIndex property is used to Gets or Sets the index of the selected item.This property returns the index value as integer;
int a = DropDownList.SelectedIndex;


Selected Item Property:
Selected Item property is used to gets the selected item with the lowest index in the list control.


To see more properties use the following link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist_properties.aspx





Friday 25 May 2012

RadioButtonList

RadioButtonList is an ASP.NET control which is used to provide a list of radiobuttons for a input from the user.The user can check only a single option from a multiple options provides by the RadionButtonList.RadionButtonList is similar to RadioButton control but the difference is that you can add individual RadioButton controls to a page and work with them singly.In simple words you group two or many individual buttons  together.On the otherhand RadioButtonList control a single control that treate as a parent control for a collection of radio button list items.RadioButtonlist control derived from a base ListControl Class and therefore it works like a ListBoxx,DropDownList and CheckBoxList controls.


How to Add RadioButton options Manully?
To add an Radio Button options manully follow first Add RadioButtonList control into your page.When you add a control into your page then a small tag will appear with the tag, click on it.


After click following picture will appear then click on Edit Items then a List Collection Editor box will appear.By clicking on Add button you can add a new option by specifying by setting the Text and Value properties.

How to Add Radio Button options Programmatically?
To add a ListItem into RadioButtonList, firstly you add a RadioButtonList control into your page then goto code behind file by pressing F7.At page_Load event write down the following code.


        protected void Page_Load(object sender, EventArgs e)
        {
            RadioButtonList1.Items.Add(new ListItem("C#","C#"));
            RadioButtonList1.Items.Add(new ListItem("VB","VB"));
            RadioButtonList1.Items.Add(new ListItem("J#","J#"));
            RadioButtonList1.Items.Add(new ListItem("C++", "C++"));
        }


How To a Bind RadioButtonList control to Database?
We can bind a RadioButtonList control to a Database by providing sqlDataSource.

Saturday 19 May 2012

CheckBoxList

Checkboxlist is used to provide a list of checkboxes that can be created dynamically by binding it to the data source.Checkbox is used to select a single value while checkboxlist is used to get a group of information from the user.
How t Bind a Checkboxlist to Datasource:

To bind a checkboxlist to datasource first add the checkboxlist control into your page.When the checkboxlist control is added to your page then a smart tag appears which allows you to specify a datasource or add a items manually to the checkboxlist.Also click on Edit Items to open the ListItem collection editor and add items as shown infigure below:



How to Add items Programmatically in CheckBoxLIst:
If you want to add items to checkboxlist programmatically, use the following code.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            cblMulti.Items.Add(new ListItem("Items 1""Items 1"));
            cblMulti.Items.Add(new ListItem("Items 2""Items 2"));
            cblMulti.Items.Add(new ListItem("Items 3""Items 3"));
        }
    }
Here in the code cblmulti.Items.Add take the augument of New ListItem whereas ListItem takes two argument first is Text and Second is Value.

Sunday 13 May 2012

Button

Asp.Net Button is used to submit information to the server.By default asp.net button is a Submit Button.When the user click on button then the button post back the web page to the server.To include as.net button in your project goto toolbox and double click on Button control.


  <asp:Button ID="Button1" runat="server" Text="Button" /> 


Some Useful Properties of Asp.Net Button
Access Key Property:
Gets or Sets the access key that allows you to quickly navigate to web server control.The default value of access key is Empty which shows that this property is not set.AccessKey is used to specify the keyboard shortcut for the web server control.This allow you to access the control quickly and easily by pressing the ALT key and that key you specified on the keyboard.For example, setting the access key of a control to the string "B" shows that the user can navigate to the control by pressing ALT+B.
Remember that only a single character is allowed for the access key.If you set access key to the value that is neither null, Empty nor a single character then the exception will be thrown.


<asp:Button id="button1" accessKey="B" />


ClientID Property:
Gets the control ID for HTML markup that is generated by Asp.Net. When the web server control is rendered as an HTML element then id attribute of HTML element is set to the value of ClientID property then the clientID property is used accesscthe HTML element at client side scripting by using document.getElementById  method.


ClientIDMode Property: 
Gets or Sets the algorithm that is used to generate the value of ClientID property.Asp.Net provides several algorithms to define how to generate the value of clientID property.The possible values are AutoID,Static,Predictable and Inherit.


CommandName Property:
Gets or Sets the Command name associated with Button control that is passed to the Command Event.
When you have a many button controls on your web page then use the CommandName property to find out the command name associated with each button control.You can set the any string with CommandName property that identifies the command toperform then by programmatically you can get the command name of button control and perform action.
For example:

void CommandBtn_Click(Object sender, CommandEventArgs e) 
{


switch(e.CommandName)
         {

            case "Asceding":

               // Call the method to sort the list in ascending order.
               Sort_List((String)e.CommandArgument);
               break;

            case "descending":
              //Call the method to sort the list in descending order.
              Sort_List((String)e.CommandArgument);  
break; default: // The command name is not recognized. Display an error message. Message.Text = "Command name not recogized."; break; }

}


Enabled Property:
Gets or Sets the the value indicating whether the web server control is enabled.


ID Property:
Gets or Sets the identifier to the web server control.


OnClientClick Property:
Gets or sets the client-side script that executes when a Button control's click event is raised.


PostBackUrl Property:
Gets or sets the URL of the page to post to from the current page when the Button control is clicked.


TabIndex Property:


Gets or Sets the tab index of the web server control.


Text Property:


Gets or Sets the text to be displayed on the button control.


ToolTip Prpperty:


Gets or Sets the text to be displayed when the mouse pointer hover over the web server control.


Some Useful Methods of Button



DataBind() Method:


Binds a data source to the invoked server control and all its child controls. 


Dispose() Method:


Enables a server control to perform final clean up before it is released from memory.


GetPostBackOption() Methid:


This method creates an object of PostBackOptions that shows the postback behaviour of button control.The PostBackOptions object is then passed to the ClientScriptManager.GetPostBackEventReference(PostBackOptions) method, which obtain a reference to a client-side script function that, when invoked, causes the server to post back to the page.You can override the GetPostBackOptions method in your derived class to modify the postback options for the button control.


OnClick() Method:


Callls the Click event of button control.


OnCommand() Method:


Calls the command event of button control.


OnDataBinding() Method:


Calls the DataBinding event of button control.


OnInit() Method:


Calls the Init event.


OnLOad() Method:


Calls the Load event.


OnUnload() Method:


Calls the unload event.


OpenFile() Method:


Gets a stream as a argument to read a file.


Some Useful Events of Button


Click Event:


Fires when button control is clicked.


Command Event:


Fires when button control is clicked.


DataBinding Event:


Fires when the server controls binds to a data source.


Init Event:


Occurs when server control is initialized.

Saturday 12 May 2012

Radio Button

Radio buttons are used when there is a list of two or more item that are mutually exclusive and the user need to select exactly one item. By using radio buttons, clicking a non-selected radio button will deselect whatever other radio button was previously selected in the list of items.To add a radio button into your project goto toolbox and double click on RadioButtons.
For example:

<asp:RadioButton ID="RadioButton1" runat="server" Text= "Radiobutton" Checked="true"/>




Some Useful Properties of Radio Buttons

ID Property:
Radiobutton.ID property id used to set the ID of  Radio button control.


protected void Page_Load(object sender, System.EventArgs e)
{
   // Set a ID to a Radiobutton
    Radiobutton.ID = "chkUserName";
}

Text Property:
We can set a value to  Radiobutton by using   Radiobutton.Text  property.For example


protected void Page_Load(object sender, System.EventArgs e)
{
   // set a text to Radiobutton
     Radiobutton1.Text = "Male";
    Radiobutton2.Text = "Female";
}

Enabled Property:
Radiobutton.Enabled property enable the state of   Radiobutton control..Initially its value is  true.

protected void Button1_Click(object sender, System.EventArgs e)
{
     Radiobutton.Enabled = false;
}

Visible Property:


Radiobutton.Visible property is used to set the visiblity of a  Radiobutton.If its value is true then  Radiobutton will be visible otherwise it will be hide for user.


protected void Button1_Click(object sender, System.EventArgs e)
{
   Radiobutton1.Visible = false;
}

Height & Width Property:
You can use the Height property along with the Width property  to size an object to specific dimensions.


protected void Button1_Click(object sender, System.EventArgs e)
{
     Radiobutton.Height = 100px;
     Radiobutton.Width = 100px;
}


CssClass Property:
You can use the CssClass property to specify the CSS(Cascadding Style Sheet) class to render on the client for the Web Server control. This property will render on browsers for all controls. In simple words you can add CssClass  by using CssClass property by just giving a cssclass name.

<asp:RadioButton ID="checkbox1" runat="server" Text="Subjects"  CssClass="HomePage"></asp:RadioButton>


Some Useful Events of RadioButton


Init Event:
This event fires when the page has been initialized.During page initialization, controls on the page are accessible and each control's UniqueID property is set. A master page and themes are also applied to the page if required. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.


protected void  RadioButton_Init(object sender, EventArgs e)
{
   Here you can write your logic...
}

Load Event:

This event fires when the web page has been loaded.During load, the opposite happens to the Init Event, all the postback data has been set and all the controls are ready and can fire their different events. The top container, the content page loads first as it can change the master page and user controls, then the master page and in the end the leaf controls.

protected void  RadioButton _Load(object sender, System.EventArgs e)
{
   Here You can write your code.
}


CheckedChanged Event:
This event occurs when the user changed the checked property of the control.

Some Useful Methods of RadioButton

Focus Method:

How to Set Focus on Asp.Net  ?

Radiobutton.Focus() method is use to set the control on a  Radiobutton.Focus method is helpful to reduce the use of mouse and user can easily get a control by using keyboard.


AT Server Side : 

protected void Page_Load(object sender, EventArgs e)
{
   Radiobutton.Focus();
}


At Client Side:

<%@ Page Language="C#" %>
<html>
<head runat="server">
  <title>Test Page</title>
</head>
<body>
  <form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
      <asp: Radiobutton  ID="Checkbox1" runat="server"></asp:Radiobutton>
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>
</body>
</html>


Databind() Method:
Radiobutton.Databind() binds the datasource to the invoked server control and all its child       controls.


Dispose() Method:
Checkbox.Dispose()  enables a server control to perform a final cleanup before it is released from memory.


To see more properties of Radiobutton click the link given below.
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton_properties.aspx

Friday 11 May 2012

Checkbox

Checkbox is used to select multiple items including zero, one or more at a same time .In other words we can say each checkbox is independent to other checkboxes so checking one checkbox does not matter the other unchecked boxes.To add a checkbox in your project goto toolbox and double click on Checkbox control.

          <asp:CheckBox ID="CheckBox1" runat="server" Text="Checkbox" Checked="true"/>

By using checked property is true you can make checkbox initially checked and by specifying false you can make checkbox initially unchecked.By using text property you can display text with your checkbox as given below.



Some Useful Properties of Checkbox:


ID Property:
Checkbox.ID property id used to set the ID of checkbox control.


protected void Page_Load(object sender, System.EventArgs e)
{
   // Set a ID to a Checkbox
   checkbox.ID = "chkUserName";
}



Text Property:
We can set a value to checkbox by using  Checkbox1.Text  property.For example


protected void Page_Load(object sender, System.EventArgs e)
{
   // set a text to checkbox
    Checkbox1.Text = "Remeber me";
}

Enabled Property:
Checkbox.Enabled property enable the state of  Checkbox control..Initially its value is true.

protected void Button1_Click(object sender, System.EventArgs e)
{
    checkbox1.Enabled = false;
}

Visible Property:


Checkbox.Visible property is used to set the visiblity of a checkbox.If its value is true then checkbox will be visible otherwise it will be hide for user.




protected void Button1_Click(object sender, System.EventArgs e)
{
   checkbox1.Visible = false;
}

Height & Width Property:
You can use the Height property along with the Width property  to size an object to specific  dimensions.




protected void Button1_Click(object sender, System.EventArgs e)
{
    chekbox1.Height = 100px;
    chekbox1.Width = 100px;
}


CssClass Property:
You can use the CssClass property to specify the CSS(Cascadding Style Sheet) class to render on the client for the Web Server control. This property will render on browsers for all controls. In simple words you can add CssClass by using CssClass property by just giving a cssclass name.




  <asp:Label ID="checkbox1" runat="server" Text="Subjects"  CssClass="HomePage"></asp:Label>



Some Useful Events of Checkbox


Init Event:
This event fires when the page has been initialized.During page initialization, controls on the page are accessible and each control's UniqueID property is set. A master page and themes are also applied to the page if required. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.


protected void Checkbox1_Init(object sender, EventArgs e)
{
   Here you can write your logic...
}

Load Event:

This event fires when the web page has been loaded.During load, the opposite happens to the Init Event, all the postback data has been set and all the controls are ready and can fire their different events. The top container, the content page loads first as it can change the master page and user controls, then the master page and in the end the leaf controls.

protected void Checkbox_Load(object sender, System.EventArgs e)
{
   Here You can write your code.
}

Some Useful Methods of Label

Focus Method:

How to Set Focus on Asp.Net  ?

Checkbox1.Focus() method is use to set the control on a Checkbox.Focus method is helpful to reduce the use of mouse and user can easily get a control by using keyboard.


AT Server Side : 

protected void Checkbox_Load(object sender, EventArgs e)
{
  Checkbox1.Focus();
}


At Client Side:

<%@ Page Language="C#" %>
<html>
<head runat="server">
  <title>Test Page</title>
</head>
<body>
  <form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
      <asp:Checkbox ID="Checkbox1" runat="server"></asp: Checkbox >
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>
</body>
</html>


Databind() Method:
Checkbox.Databind() binds the datasource to the invoked server control and all its child       controls.


Dispose() Method:
Checkbox.Dispose()  enables a server control to perform a final cleanup before it is released from memory.




To see more properties of checkbox click the link given below:


http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox_properties.aspx