在 gridview C# 上操作文本框
Manipulate textbox on gridview C#
我正在尝试创建一个 table 样的图形。当我单击“编辑”时,我需要更改列中该行的文本框才能启用。
我的活动有这个代码。问题是 GVBookDetails.FindControl
返回 null,我不明白为什么,因为我有那个控制权。
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);
}
您似乎正试图在 GridView 本身中查找 TextBox。不是按钮和文本框所在的行。
您可以使用发件人的 NamingContainer 来查找 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!";
}
我正在尝试创建一个 table 样的图形。当我单击“编辑”时,我需要更改列中该行的文本框才能启用。
我的活动有这个代码。问题是 GVBookDetails.FindControl
返回 null,我不明白为什么,因为我有那个控制权。
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);
}
您似乎正试图在 GridView 本身中查找 TextBox。不是按钮和文本框所在的行。 您可以使用发件人的 NamingContainer 来查找 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!";
}