gridview中模板列中绑定了LinkButton,RowDataBound事件中给LinkButton添加脚本((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();");'btnl'为LinkButton自己的ID时找不到对象,提示document.getElementById( )对象为空,是不是gridview中的button不能注册自己的click()方法啊??当注册其他按钮的click事件时正常运行,代码如下:((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btn2').click();");   事件代码
    protected void btn2_Click(object sender, EventArgs e)
    {       txt1.Text = "click2";
    }程序正常运行,设断点查看也正常,但就是页面上文本框里不显示值,不知道是什么原因??

解决方案 »

  1.   

    ((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('"+btn2.ClientID+"').click();");如果btn2不再dataList和Gridview之类的控件内的话,可以试试上面的方法
      

  2.   

    ((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();");======>>>>>>>LinkButton btn1=((LinkButton)e.Row.FindControl("btnl"));
    btn1.Attributes.Add("onclick", "document.getElementById('"+btn1.ClientID+"').click();");
      

  3.   


    ((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('"+((LinkButton)e.Row.Cells[8].FindControl("btnl")).ClientID+"').click();");
    //这样写看看
      

  4.   

    e.Row.Cells[8].FindControl和e.Row.FindControl两者的作用相同,可以不加cells[8]。具体查询findcontrol机制。
      

  5.   

    protected void LinkButton1_OnClick(object sender, EventArgs e)
    {
        GridViewRow gvr = (sender as LinkButton).NamingContainer as GridViewRow;
        LinkButton LinkButton1 = sender as LinkButton ;
      
    }
      

  6.   

    加if(e.Row.RowType == DataControlRowType.DataRow)
      

  7.   

      protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("test");
                for (int i = 0; i < 10; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = "测试";
                    dt.Rows.Add(dr);
                }
                this.GridView1.DataSource = dt;
                this.GridView1.DataBind();
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button button=(Button) e.Row.FindControl("Button1");
                button.Attributes.Add("onclick","alert('测试成功!')");
            }
        }
      

  8.   

    有没有判断rowType
    在header row应该是找不到那个control
      

  9.   


    脚本已经注册成功了,其他的功能都能实现,就是click事件有问题
      

  10.   

     if (e.Row.RowType == DataControlRowType.DataRow)
            {            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    LinkButton btn1 = ((LinkButton)e.Row.FindControl("btnl"));                btn1.Attributes.Add("onclick", "document.getElementById('" + btn1.ClientID + "').click();");
                }
            }这样也没用,触发不了后台事件
      

  11.   


    现在的问题是注册自己的click事件不触发后台事件,注册其他button的click能触发后台事件,但页面不显示效果
      

  12.   

    if (e.Row.RowType == DataControlRowType.DataRow)
            {            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                  ((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();");            }
            }
      

  13.   

    我想实现的是点击gridview中的按钮,后台能执行其click事件
      

  14.   

    那你干嘛  要用.Attributes.Add
    直接双击  Button不就进入它的后台事件了吗
      

  15.   

    gridview能不能双击里面的按钮- -
      

  16.   

    gridview中添加了LinkButton 比如A,后台在gridview行绑定事件中给A注册了click事件,运行时就是不执行后台A的click事件。
    如果给A注册B的click事件,能执行后台B的click事件,但页面没效果。讲的清楚了吧
      

  17.   


    你看我12楼的代码,就是你的建议,不过一样不能触发后台click事件
    我得说一下,按钮在gridview中,不是随便一个按钮
      

  18.   


    ((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();");这句话,好像无意义,给LinkButton加了个onclick事件,来执行自己的click(),可不就是直接双击LinkButton实现的方法吗
    另外一个,document.getElementById('btnl')得不到你想要的LinkButton,因为btn1是给每一行加的,在客户端,不存在这个btn1,会自动给每一行的这个按钮生成一个ClientID,如果想FindControl
    应该linkButton lbtn1=e.Row.Cells[8].FindControl("btnl") as LinkButton;
    lbtn1.Attributes["onclick"]=string.Format("document.getElementById('{0}').click()",lbtn1.ClientID)
      

  19.   


    前台代码        <asp:GridView ID="GridView1" ShowFooter="false" BorderColor="Black" OnRowDataBound="GridView1_RowDataBound"  runat="server" AutoGenerateColumns="False"  Font-Size="12px" Width="549px"  AllowPaging="True" AutoBack="true">           <Columns>
                <asp:BoundField DataField="ID" HeaderText="编号" />
                <asp:BoundField DataField="EmpID" HeaderText="账号" />
                <asp:BoundField DataField="EmpRealName" HeaderText="姓名" />
                <asp:BoundField DataField="EmpSex" HeaderText="性别" />
                <asp:BoundField DataField="EmpAddress" HeaderText="住址" />
                <asp:BoundField DataField="EmpZipCode" HeaderText="邮编" />
                <asp:BoundField DataField="EmpBirthday" HeaderText="生日" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />
                <asp:BoundField DataField="EmpSalary" HeaderText="薪水" DataFormatString="{0:c}" HtmlEncode="False" />
                
                <asp:TemplateField>
                <ItemTemplate >
                <asp:LinkButton ID="btnl" runat="server" Text="test"></asp:LinkButton>
                </ItemTemplate>            </asp:TemplateField>
              </Columns>
              <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
              <RowStyle HorizontalAlign="Center" />
              <PagerStyle HorizontalAlign="Center" BackColor="#FFFFCC" BorderStyle="None" BorderWidth="0px" ForeColor="#330099" />
                <PagerSettings Visible="False" />
            </asp:GridView>能双击吗??
      

  20.   


                <ItemTemplate >
                <asp:LinkButton ID="btnl" runat="server" Text="test"></asp:LinkButton>
                </ItemTemplate>
    这个不是一个普通的LinkButton?
    点击他想做什么操作,双击去实现他的动作有什么问题
      

  21.   

    加到GridView中后能双击吗,双击的对象是Gridview而不是LinkButton
      

  22.   

    你那不是个模板列吗
    右击GridView,有个编辑模板列,就看到那个LinkButton了,可以双击了吧