Can someone tell me how to retrieve the textbox within a gridview in asp.net c#? I have a bunch of data being displayed in a gridview and I want it to be editable. I have added a edit command field to it and i开发者_开发问答t gives me this edit link. When I press this link, a textbox appears, but I how do I get the reference of that textbox so I can get whatever is inside the textbox and change it before calling the row update handler?
Make use of FindControl
method to find the textbox you want......
in your rowedit event of gridview is
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");
}
You can access an element in a gridview by knowing the row that it resides within RowIndex
and the element name "text_textbox"
TextBox text = ((TextBox)contacts.Rows[RowIndex].FindControl("text_textbox")).Text;
精彩评论