Click here to Skip to main content
15,895,709 members

How to add control to gridview on RowCreated.

maverick12131 asked:

Open original thread
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var firstCell = e.Row.Cells[4];
                firstCell.Controls.Clear();
                Button btn_chk = new Button();
                btn_chk.Text = firstCell.Text;
                btn_chk.CommandName = "btn_chk";
                //firstCell.Controls.Add(new HyperLink { NavigateUrl = "Default.aspx?Search="+firstCell.Text+"|"+e.Row.Cells[3].Text, Text= firstCell.Text });
                firstCell.Controls.Add(btn_chk);
            }
        }
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }

I am able to add a button in RowDataBound event but not in RowCreated event.

Actual Problem:Data is binded to grid only first time of page load.
When the button is added on RowDataBound then on click of that button, the control doesn't render on consequent postbacks

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<string> years = new List<string>();
                for (int i = 0; i > -6; i--)
                {
                    years.Add(DateTime.Now.AddYears(i).ToString("yyyy"));
                }
                drpYear.DataSource = years;
                drpYear.DataBind();
                DataSet dt = GetExistingRecords();
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

So tell me a way to add controls to a grid in code behind(i don't want to use Template fields)
Which will be rendered even on the second postback();
Tags: C#, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900