My Profile

Friday, February 4, 2011

Creating ASP.Net Controls

We can create ASP.Net controls in 2 ways:

  • Mark Up Style   [preferred]
  • Using Code / Object oriented style

Markup style syntax in ASP.Net:

<TagPrefix : TagName ID = "<controlname>" runat = "server" ......../>

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

           <Nit : Header ID = "Header1" runat = "server" />

All "asp" TagPrefix denotes they are pre-defined ASP.Net controls. Whereas, any other TagPrefix denotes they are User defined controls.

ID and runat attributes are mandatory.

Classical ASP doesn't have any controls to develop user interface.

Note: ID should be meaningfull and we must follow some conventions to name them like 

"btnAdd" for Button with Add as Text / Purpose.

"txtName" for TextBox for accepting Name.

"lstCource" ListBox for List of courses.

Placing Controls in Forms:

  • <div> stands for division and it is a container where we can place other controls inside it. Once designed we can provide positioning option like centre, left, right etc., for <div>.
  • The other method is to use table, where we can create rows/columns and place controls inside it. Positioning options can be given to table also. 
  • Nested tables, div's can also be used for effective output.


Let's see one example to understand this.

before going to Example, we'll see two controls which we are going to use in our control.

ListBox / BulletedListBox: 
  • ListBox is used to display multiple values.
  • BulletedListBox is used to display multiple values like label for single values.

Now we'll example.

  • Design a form as following.

  • Coding
Under Button click:

        BLstRes.Items.Add("User Information");
        BLstRes.Items.Add("Username :" + TxtName.Text);
        BLstRes.Items.Add("Email Id :" + TxtEmail.Text);
        if (LstCourse.SelectedIndex >= 0)
            BLstRes.Items.Add("Course :" + LstCourse.SelectedValue);
            BLstRes.Items.Add("Course : Not Selected");


** If you like this post please like our page in facebook


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More