Thursday 16 October 2014

How To Add Dynamic Controls Into Gridview in Asp.net

How to add dynamic controls to the Grid view in the Code Behind page. when the page is refresh or the page is Post Back the dynamically created controls will disappear from Grid View, this sample will show how to resolve this issue.

Step 1. Create a C# "ASP.NET Web Application" Name it Add_Dynamic_Control_to_Grid-view".
Step 2.  Add a Grid-view Control to Default.aspx page then rename it to "MyData". This page will bind the data and show the dynamically created Link Button Control.
In order to realize this page.
First, we need to bind data to MyData:
HTML
<asp:GridView ID="MyData" runat="server" AutoGenerateColumns="False" OnDataBound="MyData_DataBound" 
           DataKeyNames="Id" DataSourceID="SqlDataSource1"           <Columns               <asp:TemplateField> 
                   <ItemTemplate                   </ItemTemplate> 
               </asp:TemplateField> 
               <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" 
                   SortExpression="Id" /> 
               <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
               <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />       
           </Columns> 
       </asp:GridView> 
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
           SelectCommand="SELECT [Id], [Name], [Age] FROM [CustomerInfo]"></asp:SqlDataSource> 
 

Then, we need to call DataBound method to add dynamically created Control.


C#
        protected void MyData_DataBound(object sender, EventArgs e) 
        { 
            AddLinkButton(); 
        } 
        private void AddLinkButton() 
        { 
            foreach (GridViewRow row in MyData.Rows) 
            { 
                if (row.RowType == DataControlRowType.DataRow) 
                { 
                    LinkButton lb = new LinkButton(); 
                    lb.Text = "Approve"; 
                    lb.CommandName = "ApproveVacation"; 
                    lb.Command += LinkButton_Command; 
                    row.Cells[0].Controls.Add(lb); 
                } 
            } 
        } 
 

To test the LinkButton, we add an event for the LinkButton .


C#
protected void LinkButton_Command(object sender, CommandEventArgs e) 
        { 
            if (e.CommandName == "ApproveVacation") 
            { 
                //This is to test  
                LinkButton lb = (LinkButton)sender; 
                lb.Text = "OK"; 
            } 
        } 
Add LinkButton in the Page_Init event and override the OnInit method.
C#
protected void Page_Init(object sender, EventArgs e) 
       { 
           MyData.DataBind(); 
       } 
       protected void Page_Load(object sender, EventArgs e) 
       { 
           if (!IsPostBack) 
           { 
               AddLinkButton(); 
           } 
       } 
       protected override void OnInit(EventArgs e) 
       { 
           base.OnInit(e); 
       } 

0 comments::

Post a Comment

Copyright © DotNet-Ashok