簡體   English   中英

在gridview C#上操作文本框

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM