日期:2014-05-18  浏览次数:20567 次

gridview点击某行获取值的问题
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
  try
  {

  this.TextBox5.Text = "4";

  this.TextBox4.Text = this.GridView1.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.Message);
  }
  }

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {

  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  //鼠标移动到每项时颜色交替效果
  e.Row.Attributes["onmouseover"] = "e=this.style.backgroundColor;this.style.backgroundColor='#cccccc'";
  e.Row.Attributes["onmouseout"] = "this.style.backgroundColor=e";
  //设置悬浮鼠标指针形状为"小手"  
  e.Row.Attributes["style"] = "Cursor:hand";
  //鼠标点击某行即选中某行
  e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
  }

为什么我点击gridview某一行,this.TextBox4.Text获取不到值

------解决方案--------------------
GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text;

------解决方案--------------------
探讨

GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text;

------解决方案--------------------
你是不是事件搞错了
------解决方案--------------------
很简单,用RowDataBound事件,将我代码中的控件换成你的就Ok了
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "thiscolor = this.style.backgroundColor,this.style.backgroundColor='LightCyan'");
e.Row.Attributes.Add("onmouseout", " this.style.backgroundColor = thiscolor");
e.Row.Style["cursor"] = "hand";
}

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("OnDblClick", "javascript:window.opener.document.Form1.jhbh.value='" + e.Row.Cells[0].Text.ToString().Trim() + "';" +
"window.opener.document.Form1.Button1.click();" +
"window.close();");
}
}
------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
this.TextBox4.Text = this.GridView1.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();//这行应该放到这里面才对吧??并确定cell[2]是不是你想传递值的行
}