zoukankan      html  css  js  c++  java
  • 在GridView中使用FindControl

    DataRowView dv =(DataRowView)e.Row.DataItem;
    string id=dv.Row["ProjectID"].ToString();

    1、在行绑定(RowDataBound)事件中使用

       

        //获得行数据绑定中的TextBox1
        protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // 对于在RowDataBound中Find,可以用if (e.Row.RowType == DataControlRowType.DataRow)来限制Find的范围,因为Find默认是在HeaderTemplate中找,如果不限定范围,在HeaderTemplate中找不到,自然就返回null,然后就出错了,DataControlRowType枚举中的DataRow确定是数据行.
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    TextBox tb = (TextBox)e.Row.FindControl("TextBox1");
            //    tb.Text = "databind";
            //}


            //如果在DataGrid的页眉和页脚:

            //if (e.Row.RowType == DataControlRowType.Header)
            //{
            //    TextBox tbheader = (TextBox)e.Row.FindControl("txtHeader");
            //    tbheader.Text = "Head";
            //}
            ((TextBox)this.gv1.Controls[0].Controls[0].FindControl("txtHeader")).Text = "Head";

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                TextBox tbfooter = (TextBox)e.Row.FindControl("txtFooter");
                tbfooter.Text = "Footer";
            }
        }

    2、在行命令(RowCommand)事件中使用

        

        //行命令时间中找到TextBox1
        //如果使用GridView默认的模式,e.CommandArgument自动棒定为该行的Index,这时候只要指定gridview1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("xxx")就可以了,但是如果转化为Template,e.CommandArgument并不会自动绑定任何值,需要手动绑定,可以在<ItemTemplate></ItemTemplate>手动写CommandArgument="<%# ((GridViewRow) Container).RowIndex %>",把这个行的 Index绑定绑定到该e.CommandArgument就可以了.
        protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "change")
            {
                TextBox tb = (TextBox)this.gv1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("TextBox1");
                
                Response.Write(tb.Text);

      GridViewRow row = ((Button)e.CommandSource).Parent.Parent as GridViewRow;
               Label lblnumber = (Label)row.Cells[5].FindControl("lblNumber");


            }
        }

  • 相关阅读:
    MongoDB中级---->关联多表查询
    Java爬虫,信息抓取的实现
    Android Java汉字转拼音总结
    Android使用Activity用作弹出式对话框
    利用Theme自定义Activity间的切换动画
    ListView滑动删除 ,仿腾讯QQ
    CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒
    玩转Web之easyui(三)-----easy ui dataGird 重新指定url以获取不同数据源信息
    rsyslogd: error during parsing file /etc/rsyslog.conf, on or before line 55: warnings occured in fil
    升级automake和autoconf
  • 原文地址:https://www.cnblogs.com/momjs/p/6274357.html
Copyright © 2011-2022 走看看