Click here to Skip to main content
11,706,455 members (34,242 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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

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();
Posted 1-Jul-13 6:40am
Edited 1-Jul-13 17:59pm
v3

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 197
1 Sergey Alexandrovich Kryukov 141
2 chainerlt 110
3 Mika Wendelius 80
4 ppolymorphe 71
0 OriginalGriff 9,070
1 Sergey Alexandrovich Kryukov 8,413
2 CPallini 5,189
3 Maciej Los 4,746
4 Mika Wendelius 3,676


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 13 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100