Manipulate textbox on gridview C#

梦想的初衷 提交于 2021-01-29 08:52:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!