问题描述:
 GridView1绑定了检索出来的数据库的字段,其中有一个字段PID。根据用户的需求,可能在检索时检索到了PID字段,从而绑定到了GridView1,也有可能没有检索到,从而没有在GridView1里面。
现在的问题是,如果GridView1里面有PID字段,则我想让用户点击一下PID字段的某个内容,就能读出这个内容,读到TextBox1.text里面,并且执行一段代码——dhfgfgfhgfjdf。
  求上述问题的代码。或设置。
  在线等,问题描述不清楚的地方请问我。
   有老各位!!

解决方案 »

  1.   

        //绑定链接
        protected void CPSGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string PID= string.Empty;        if (e.Row.RowType == DataControlRowType.DataRow)
            {            PID= e.Row.Cells[0].Text.Trim();            e.Row.Cells[3].Text = "<a href=\"javascript:JSFunction('" + PID + "')\">" + PID + "</a>";
            }
        }
     function JSFunction(PID)
        {
            document.getElementById('TextBox1').value = PID;
        }
    如有疑問請跟帖
      

  2.   

    //筆誤,下面兩個Cells[]中的數字應該是一樣的。
    //請將Cells[]中的index對應為你自己的Index,
      PID= e.Row.Cells[0].Text.Trim();  e.Row.Cells[3].Text = "<a href=\"javascript:JSFunction('" + PID + "')\">" + PID + "</a>";
      

  3.   

    要执行的代码是js还是后台执行?
    点击pid字段某个内容,是用什么方法选择
      

  4.   

    回复楼上:click 事件选择,是C#的代码,不是JS后台。,一楼的还是不太明白,我的意思就是给PID字段能够弄成button按钮那样的,一点击就能执行一段代码,并且代码中需要用到所点击的字段的内容。我现在需要的就是把字段弄成能够响应单击事件的代码或设置,在就就是能够读出所点击的内容!!!!!
    求代码,最好给点注释。有劳各位。完了再加分。
      

  5.   

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyCommand")
            {
                Button button = (Button)e.CommandSource;
                GridViewRow row = (GridViewRow)button.Parent.Parent;
                string a = row.Cells[1].Text.ToString();//获得第一个单元格的值   
                string b = this.GridView1.DataKeys[row.DataItemIndex].Values[0];//获得DataKeys的值   
            }
        }  
    参考
      

  6.   

     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
           
                if (e.Row.FindControl("Button1") != null)
                {
                    Button btn = (Button)e.Row.FindControl("Button1");
                    btn.Click += new EventHandler(btn_Click);
                }
            }
        }    private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            GridViewRow gvr = (GridViewRow)btn.Parent.Parent;
            string pk = GridView1.DataKeys[gvr.RowIndex].Value.ToString();        this.Text1.Text = pk;
        }