问题
I'm trying to create a table like figure. When I click Edit I need textbox of that row in column change to enable.
My event has this code. The problem is GVBookDetails.FindControl
is returning null and I can't understand why because I have that control.
protected void btnEditQuantity_Click(object sender, EventArgs e)
{
int productID = Convert.ToInt32((sender as Button).CommandArgument); // get productID from EditButton
Book book = (Book)Session["BookID"]; // object instance to use in edit query
TextBox textBox = GVBookDetails.FindControl("tbQuantityEdit") as TextBox;
textBox.Enabled = true;
int quantity = Convert.ToInt32(textBox.Text);
}
回答1:
It looks like you are trying to find the TextBox in the GridView itself. Not the row that the button and the textbox are in. You can use the NamingContainer of the sender to find the TextBox.
protected void btnEditQuantity_Click(object sender, EventArgs e)
{
//cast the sender back to a button
Button cb = sender as Button;
//get the current gridviewrow from the button namingcontainer
GridViewRow row = cb.NamingContainer as GridViewRow;
//use findcontrol to locate the textbox in that row
TextBox tb = row.FindControl("tbQuantityEdit") as TextBox;
//do something with the textbox
tb.Text = "TextBox found!";
}
来源:https://stackoverflow.com/questions/53710472/manipulate-textbox-on-gridview-c-sharp