Infolink

 

Search This Blog

Aug 30, 2012

ASP.Net Repeater dynamically using C# code

ASP.Net Repeater Databound Control can be generated dynamically using C# code. You can render the data items in dynamically generated Repeater Control. ASP.Net Panel Control can be used to add Repeater Control dynamically inside it as a child control. Method of database connectivity and Databinding using DataSource object of Repeater Control is same as we discussed in the previous article about ASP.Net Repeater Control Databinding using DataSource. While creating a dynamic Repeater control, you also have to generate its ItemTemplate, SeparatorTemplate and HeaderTemplate dynamically to display the data items retrieved from the database. In C# code behind use the DataSource object to bind the data with dynamic Repeater Control and add it as a child control of ASP.Net Panel to render all stuff on the web page.

HTML Code to Generate ASP.Net Repeater Control Dynamically

<asp:Panel ID="Panel1" runat="server">  
</asp:Panel> 

C# Code to Generate ASP.Net Repeater Control Dynamically


// Repeater Control Databinding using Datasource  
Repeater Repeater1 = new Repeater();  
Repeater1.DataSource = myDataSet;  
Repeater1.DataBind();  
  
foreach (RepeaterItem repeatItem in Repeater1.Items)  
{  
    // if condition to add HeaderTemplate Dynamically only Once  
    if (repeatItem.ItemIndex == 0)  
    {  
        RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);  
        HtmlGenericControl hTag = new HtmlGenericControl("h4");  
        hTag.InnerHtml = "Employee Names";  
        repeatItem.Controls.Add(hTag);  
    }  
  
    // Add ItemTemplate DataItems Dynamically  
    RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);  
    Label lbl = new Label();  
    lbl.Text = string.Format("{0} {1} <br />", myDataSet.Tables[0].Rows[ repeatItem.ItemIndex ]

    [ "FirstName" ], myDataSet.Tables[0].Rows[ repeatItem.ItemIndex ][ "LastName" ]);  
    repeatItem.Controls.Add(lbl);  
  
    // Add SeparatorTemplate Dynamically  
    repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);  
    LiteralControl ltrlHR = new LiteralControl();  
    ltrlHR.Text = "<hr />";  
    repeatItem.Controls.Add(ltrlHR);  
}  
  
// Add Repeater Control as Child Control  
// of Panel Control  
Panel1.Controls.Add(Repeater1);


Aug 28, 2012

General Page Life-Cycle Stages

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.


In general terms, the page goes through the stages outlined in the following :

1 Page request   : The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.
 

2 Start :  In the start stage, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.

3  Initialization : During page initialization, controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to the page if applicable. 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.
4  Load : During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.

5  Validation : During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.

6 Postback event handling  : if the request is a postback, any event handlers are called.

 7 Rendering : Before rendering, view state is saved for the page and all controls. During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Response property.

8  Unload :  Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.
Related Posts Plugin for WordPress, Blogger...