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); 
       } 

Related Posts:

  • How To Use Grid View Page Index Changing In Asp.Net Here I Will Explain How to use Page Index/Pagination In Grid View.With various types of paging in ASP.NET. We use C# code to bind the SQL data with a Grid View control and use the following simple steps to mak… Read More
  • Auto Generate Row Number in GridView The GridView control is the successor to the DataGrid and extends it in a number of ways. While developing GridView control in ASP.NET, programmers often requires to display row number in GridView controls. Auto Generate… Read More
  • How To Use Cookies ASP.NET. Cookies provide the ability to store small amounts of data on a clients machine. It is often used to store user preferences, session variables, or identity. When the user visits your Web site another time, the application c… Read More
  • What Is Delegate Here I will explain what is Delegate and types of Delegate. Single Cast Multi Cast Asynchronous Delegate is a type known as pointer type looks like method and perform like type derived from super type delegate.Delegate is… Read More
  • How To Copy Whole Database From One Computer To Another By Using Generate Script What is Generate Script?      This is a process in which we can generate the script of the existing database. And can use easily on other client system without loosing any Data. Why it is different from … Read More

0 comments::

Post a Comment

Copyright © 2025 DotNet-Ashok