0

I have a GridView and a select link in the GrdiView, when the item is selected I want it to read the contents to a textbox below the GridView. 我能想到的唯一方法是访问选择链接的 onClick 函数的背后代码。问题是我不确定如何做到这一点。

更新的果酱方法:

C# 代码

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    GridView1.SelectedIndex = e.NewSelectedIndex;
    TextBox1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
}

正面代码

<asp:GridView ID="GridView1" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
GridLines="None">

更新我的方法:

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    int i = GridView1.SelectedIndex;
    ds.Tables["Comments"].Rows[i]["Comment"] = TextBox1.Text;
}

也试过这个,但我得到一个错误-> System.IndexOutOfRangeException:位置-1没有行。

谢谢。

4

3 回答 3

0

您需要将 OnSelectedIndexChanged 事件与您的 gridview

<asp:GridView OnSelectedIndexChanged="GridView1_SelectedIndexChanged" />

然后在后面的代码中你可以这样做:

void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
  {

    GridViewRow row = CustomersGridView.SelectedRow;// you will get the selected row
    someLabel.Text =  row.Cells[0].Text;
  }
于 2012-05-16T19:43:39.440 回答
0

附加到 selectedindexchanged 事件后,您还需要设置一些数据键,以便您可以从网格中检索值。在此处查看示例。

void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{

  // Determine the index of the selected row.
  int index = CustomersGridView.SelectedIndex;

  // Display the primary key value of the selected row.
  Message.Text = "The primary key value of the selected row is " +
    CustomersGridView.DataKeys[index].Value.ToString() + ".";

}
于 2012-05-16T20:50:04.657 回答
0

最好使用网格视图的 oORowCommand 事件并将命令参数和命令名称作为属性值传递给同一事件。然后我认为它会工作。

于 2012-05-17T10:05:10.833 回答