Tuesday, May 1, 2012

Create and Bind Data using Repeater Control in asp.net(Part - 1)

By using the repeater control in asp.net applications.I have saved a lot of time instead of creating Dynamic controls.There is lot of features available with this control.I will try to cover all the features of Repeater control in Asp.net
The following are the questions I would like to cover in this article regarding Repeater control

1.How to create a Repeater Control

2.How to Bind the Data in Repeater Control

3.How to Create dynamic controls using repeater control

4.How to handle the (Textbox,dropdownlist, etc) Postback events in repeater control

5.How to Insert data in database using repeater control.(Part - 3 In Progress...)

6.How to Edit and update the data using Repeater control(Part - 3 In Progress)



So lets start to clear all the Questions


1.How to create a Repeater Control 


First,Create a New Asp.net web site Next,Drag and drop one repeater control from your toolbox in Deault.aspx Page which is available in Data tab Then your default.aspx page will be look like this



Next ,Add the Item Template in between the repeater node. Now the code will be look like this


 



Now add the controls in repeater control Here i am adding 6 Item Templates bcoz i have 6 controls (3 labels,2 dropdownlists,1 Textbox)

            
        
--Select-- * --Select-- * *
Thats it..Now we have successfully created the Repeater control

Next Question 2. 


2.How to Bind the Data in Repeater Control 


Open the default.aspx page and bind the field in the following way for every control For Country :

For State :
  
For City :
  


Next ,Open the Default.aspx.cs Page and follow the procedure In the page load Event write the following code
  
  protected void Page_Load(object sender, EventArgs e)
    {        
        if (!Page.IsPostBack)
        {                    
            BindInformation();           
        }       
    }
    
    Next,Write the following code for BindInformation Method
    
    private void BindInformation()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        try
        {          
                SqlCommand com = new SqlCommand("select * from vw_Information", con);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                da.Fill(ds);
                {
                    Repeater1.DataSource = ds.Tables[0];
                    Repeater1.DataBind();
                }          
        }
        catch (Exception ex)
        {
           throw ex;
        } 
    } 
    
    
Repeater_control Thats it.Now we have successfully displayed the information using Repeater control 


Check the Part-2 for 3 and 4 Questions 



1.How to create a Repeater Control

2.How to Bind the Data in Repeater Control

3.How to Create dynamic controls using repeater control

4.How to handle the (Textbox,dropdownlist, etc) Postback events in repeater control

5.How to Insert data in database using repeater control.(Part - 3 In Progress...)

6.How to Edit and update the data using Repeater control(Part - 3 In Progress)

Labels: , , , ,