.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

create link button dynamically in gridview

Posted By:      Posted Date: October 06, 2010    Points: 0   Category :ASP.Net

Hi All,

I am trying  to add/insert linkbutton dynamically inside gridview row, below is the code

protected void Page_Load(object sender, EventArgs e)
        DataTable dt = new DataTable();
        DataColumn dcol = new DataColumn("Measure", typeof(System.String));

        DataColumn dcol1 = new DataColumn("ID",typeof(LinkButton));
        DataRow drow = dt.NewRow(); 

            LinkButton btn = new LinkButton();
            btn.ID = "testbtn";
            btn.Text = "1";
            btn.Visible = true;


            //drow = dt.Rows[0];

            drow[0] = "testmeasure";
            drow[1] = btn;

        catch{ }


        GridView1.DataSource = dt;


Problem I facing is that the data table is getting created fine with teh linkbutton as well but the gridview is not showing the linkbutton. upon executing I am only the column which is of string type.

Please help me in this case. what I am doing wrong? please help. this is urgent.


View Complete Post

More Related Resource Links

Dynamically Adding button in GridView



I am using the following to dynamically add button to the cell of gridview, condition is that if the value of the fourth column is empty, then in place of empty cell add the button. But nothing is happening.  I am making use of the following logic in  RowDataBound event in gridview.

protected void gdvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)

for( int i=0;i<=dataSet.Tables[0].Rows.Count;i++ )
                if (dataSet.Tables[0].Columns[3] == null)
                    Button button = new Button();
                    button.Text = "ADD";
                   DataColumn dataColumn = new dataColumn();

Gridview Link Button control click event


Respected Reader,

I have  Web page that uses LinkButton control inside Template Fields of Gridview.

I am trying to work on LinkButton Click event and trying to preserve the Value of LinkButton that user click, and based on that value, i want to refresh tha page data.

I used CommandName and CommandArgument property of Linkbutton, but i can use that property only in GridView_RowCommand method by checking e.CommandName expression. But the problem with that Gridview_RowCommand event fire after all other page_load activity. So the value never used in the page_load event to refresh the data in all other control.

I also tried to use OnClick Method, Session variable and Hidden variable. but when i run Response.Redirect method from Gridview_RowCommand, it clear all the Session and Hidden variable values. also tried (Response.Redirect(,false)) method but never got success.

Any suggestion will be appricated in advance.

Add Dynamically TextBox And DropDownList In Gridview on Button Click Wich is placed outside of Gridv


Hi All,

   I have Gridview and Button Control on Form.i want to Add Dynamically TextBox And DropDownList In Gridview on Button Click Wich is placed outside of Gridview.

When i click on Button -> One TextBox and One Dropdownlist which contains values bydefault 1,2,3 should bind to gridview.    Button is placed outside Gridview.

Thanks in advance.

how to change the color of disabled link button in gridview



how to change the color of disabled link button in gridview?.. 

Create GridView Dynamically with any Number


according to my case i want to show from  1 to 7 gridviews at every query from database
so i made 7 methods and also 7 gridview ,but this is bad idea
how can i ceate it dynamically for only gridview and only method?
thanks so much
here is my gridview
<asp:GridView ShowHeader="False" ID="gv_statistics_gr4" runat="server" AutoGenerateColumns="False" onrowdatabound="gv_statistics_gr4_RowDataBound" > <RowStyle BackColor="#FFFF00" /> <Columns> <asp:TemplateField HeaderText="name"> <ItemTemplate> <asp:Label ID="img_vvvitemname10" runat="server" Text='<%#Eval("name")%>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="count "> <ItemTemplate> <asp:Label ID="lbl_sum" runat="server" Text='<%#Eval("count") %>' /> </ItemTemplate>

How to Create WPF button dynamically + route button event dynamically





 Window1 : Window





 sender, RoutedEventArgs e)
  // do something





 sender, RoutedEventArgs e)
   string xml = "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'";
xml += " xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'";
xml += " Height='26' Margin='45,0,0,30'";
xml += " Name='button3'";
xml += " VerticalAlignment='Bottom'";
xml += " HorizontalAlignment='Left' Width='65'>Test</Button>";

Stream strm = new MemoryStream(ASCIIEncoding.Default.GetBytes(xml)); Button myButton = (Button)System.Windows.Markup.XamlReader

Problem to group radio button across gridview rows

In general no one can easily group or make a single selection from radio button list inside gridview rows. There are a lot of way to make a single or unique selection from list. Here i want to share how i can address this problem in the most easiest way. Googling the problem most of the cases i saw the grouping in horizontal way. Thats why i tried to group the radio buttons in vertical manner. Let i have a requirement like below:

Maintain HTML Radio Button Selection Inside GridView After Postback

Long time ago I wrote an article about getting the value of the selected RadioButtons inside the GridView control. The code did not addresssed the postback issue which means that the selected radio button was cleared after the postback occurs.

A Simple and Extensible Radio Button Style GridView

The GridView is one of the significant controls in ASP.NET 2.0 and later versions. No doubt it reduces a lot of work time. However, customizing the GridView control is not an easy task. For example, it's hard to add a radio button column to the GridView. This article demonstrates the reason why a radio button column cannot act as normal and how to create a simple and extensible radio button GridView.

Create ToolTip for GridView Header

The following allows you to add a tooltip to the header links of your GridView, a feature that is lacking.

Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List

This is an example of an editable gridview containing different ASP.NET controls. When user wants to edit values in gridview, she/he can use these controls to edit existing values in Gridview Edit Mode.

Using the Code

Dynamically Create Controls in ASP.NET by Using Visual C# .NET

This article demonstrates how to dynamically create a control for an .aspx page. The sample project does the following:
Creates two TextBox controls.
Verifies that the contents (TextBox.text) and the attributes of the TextBox are saved across posts to the server.
Illustrates handling events that are posted by a dynamically-created control

Dynamically Create Controls in ASP.NET with Visual Basic .NET

This step-by-step article describes how to dynamically create controls for an ASPX Web page.

The sample project does the following:
It creates two TextBox controls.
It verifies that the TextBox contents (TextBox.text) and attributes are saved across posts to the server.
It describes how events that are posted by a dynamically created control are handled.

Html Link Button


Hi All,

I would like to create a html link button(in aspx page) so that the page could not post back and some controls to display on clicking link button.

Can any one of you help me regarding this issue?

Let me know if you have any queries.



Gridview with Image Button


i have a gridview and i want to perform edit and delete with an image button

but unable to figure out the events and how i m gonna make my grid into edit mode ??

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                <asp:TemplateField HeaderText="S.No">
                        <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
                <asp:TemplateField HeaderText="Edit">
                        <asp:ImageButton ID="ImageButton1" runat="server" ImageAlign="Middle" 
                            ImageUrl="~/image/ico_edit.gif" CommandName="Edit" 
                            onclick="ImageButton1_Click" />
                <asp:TemplateField HeaderText="Delete">

How to dynamically create user controls ?



can you explain how to create user control dynamically in a web form ?When i am creating server controls of user controls are not instantiated and it is not getting initialized .when i am tyying to initialize its properties error occurs tha object instance not created .

Any suggestion ? 

Dynamically creating Gridview Template - Linkbutton Click not working



I have written some code to dynamically generate template columns for gridview which works well. However, each cell in the gridview has to be a linkbutton, which when clicked does a db update and redirects to a specific url with some parameters in it.

I have attached a click event handler to the linkbutton in the InstantiateIn method but the event does not seem to fire.

Could someone please help? the code is below -

//Dynamically creating the Grid

protected void btnAnalyze_Click(object sender, EventArgs e)
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend