ASP.NET Gridview的编辑文本框

时间:2011-06-01 19:07:16

标签: c# asp.net gridview

有人能告诉我如何在asp.net c#中的gridview中检索文本框吗?我有一堆数据显示在gridview中,我希望它可以编辑。我已经为它添加了一个编辑命令字段,它给了我这个编辑链接。当我按下此链接时,会出现一个文本框,但我如何获取该文本框的引用,以便我可以获取文本框中的任何内容并在调用行更新处理程序之前更改它?

2 个答案:

答案 0 :(得分:1)

利用FindControl方法找到您想要的文本框......

gridview的rowedit事件中的

void AuthorsGridView_RowUpdating (Object sender, GridViewUpdateEventArgs e)
  {
    // The GridView control does not automatically extract updated values 
    // from TemplateField column fields. These values must be added manually 
    // to the NewValues dictionary.

    // Get the GridViewRow object that represents the row being edited
    // from the Rows collection of the GridView control.
    int index = AuthorsGridView.EditIndex;
    GridViewRow row = AuthorsGridView.Rows[index];

    // Get the controls that contain the updated values. In this
    // example, the updated values are contained in the TextBox 
    // controls declared in the edit item templates of each TemplateField 
    // column fields in the GridView control.
    TextBox lastName = (TextBox)row.FindControl("LastNameTextBox");
    TextBox firstName = (TextBox)row.FindControl("FirstNameTextBox");


  }

答案 1 :(得分:0)

您可以通过知道它位于RowIndex中的行和元素名称"text_textbox"

来访问gridview中的元素
TextBox text = ((TextBox)contacts.Rows[RowIndex].FindControl("text_textbox")).Text;