我在前台用了一个GRIDVIEW其中有个linkbutton模板列,点击该列中某一项即弹出窗口,并把此项的值传给该弹出窗口。可实验时发现每次都必须要点击到第二次才会弹出窗口,点击第一次时没反应,请问大家有人知道什么原因么?
附带部分代码:
前台:
<asp:GridView id="FGridView" runat="server" CssClass="Note" Width="100%" HeaderStyle-BackColor="AliceBlue" BorderColor="Thistle" BorderStyle="Ridge" AutoGenerateColumns="False"     OnRowCommand="FGridView_RowCommand"  OnRowCreated="FGridView_RowCreated" DataKeyNames="jgms">
      
                <Columns>  
                    
                     <asp:TemplateField  HeaderText="体征描述">
                    <ItemTemplate>
                    <asp:LinkButton    ID="miaoshu"   CommandName="ms" runat="server" Text='<%#Eval("JGMS") %>' ></asp:LinkButton>
                    </ItemTemplate>
                    </asp:TemplateField> 
                   
                </Columns>   
    </asp:GridView>后台:
 protected void FGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {        String miaoshu = "";
        e.CommandName.ToString();
       
        LinkButton bb = new LinkButton();
       
        if (e.CommandName == "ms")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = FGridView.Rows[index];
            
            bb = (LinkButton)row.Cells[1].FindControl("miaoshu");
            miaoshu = bb.Text;
            FGridView.Rows[index].Cells[1].Attributes.Add("OnClick", "javascript:window.open('cbzd.aspx?jg="+miaoshu+"','','width=400,height=300,toolbar=no,menubar=no,resizable=yes, top=200,left=1000');");
         }
  }

解决方案 »

  1.   

          FGridView.Rows[index].Cells[1].Attributes.Add("OnClick",   "javascript:window.open('cbzd.aspx?jg="+miaoshu+"','','width=400,height=300,toolbar=no,menubar=no,resizable=yes,   top=200,left=1000');"); 
                      } 
    这个绑定不应该放在RowCommand中,而是应该放在RowCreate或Rowdatabound中
      

  2.   

    楼上分析的很对,应该在行生成这个生命周期点上赋值javascript属性.
      

  3.   

    给你例子,你自己照着写吧
      string strDialogSettings = "Center:yes;Resizable:yes;DialogHeight:800px;DialogWidth:720px;Status:no;scrollbars:no";
                    ((LinkButton)(e.Item.Cells[4].Controls[0])).Attributes.Add("onclick", "ShowDialog('CompanyFile.aspx?CommodityID=" + this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString() + "','" + strDialogSettings + "');location.reload(true);");//
      

  4.   

    这个就是弹出子窗口的啊.你在location.reload(true);");前加上window.close()就可以实现关闭子窗口,刷新父窗口了着段代码放在Rowdatabound事件中
      

  5.   

    哦,似乎明白了,还有点问题,你这里用的是Datagrid,所以一些属性是不同的,this.DataGrid1.DataKeys[e.Item.ItemIndex],这里e.Item.ItemIndex在gridview中应该怎么表示呢?
      

  6.   

    我试了用e.Row.RowIndex来代替,不过运行提示超出参数有效范围,是什么原因呢?
      

  7.   

    呵呵,第一次是加入属性,第二次是响应前台“onclick”事件,只要在绑定数据的时候加入属性就可以了
      

  8.   

    楼上的高手,我的问题就在于,响应onclick事件时所传的参数是要在点击事件时才获得的,所以不知怎么像你说的那样实现,可否给于指点?
      

  9.   

    在gridview rowdatabound中为linkbutton动态添加客户端单击事件: 
    protected void FGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton bb = (LinkButton)e.Row.FindControl("miaoshu");
                e.Row.Cells[1].Attributes.Add("onclick", "javascript:window.open('cbzd.aspx?jg=" + bb.Text + "','','width=400,height=300,toolbar=no,menubar=no,resizable=yes,   top=200,left=1000');"); 
            }
        }