如何在RowCreated上向gridview添加控件。 [英] How to add control to gridview on RowCreated.

查看:90
本文介绍了如何在RowCreated上向gridview添加控件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
        {

        }



我能够添加RowDataBound事件中的按钮但不在RowCreated事件中。



实际问题:数据仅在第一次加载页面时绑定到网格。

当在RowDataBound上添加按钮然后单击该按钮时,控件不会在随后的回发中呈现




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

推荐答案

这可能对你有帮助



http://msdn.microsoft .com / zh-cn / library / system.web.ui.webcontrols.gridview.rowcreated.aspx [ ^ ]
this may help you

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated.aspx[^]


这篇关于如何在RowCreated上向gridview添加控件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆