[英]Manipulate textbox on gridview C#
我正在嘗試創建一個如圖所示的表。 當我單擊“編輯”時,我需要更改該行的文本框才能啟用。
我的活動有此代碼。 問題是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查找文本框。
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!";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.