简体   繁体   English

在gridview C#上操作文本框

[英]Manipulate textbox on gridview C#

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. 问题是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);
}

It looks like you are trying to find the TextBox in the GridView itself. 似乎您正在尝试在GridView本身中找到TextBox。 Not the row that the button and the textbox are in. You can use the NamingContainer of the sender to find the 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