在gridview C#上操作文本框 [英] Manipulate textbox on gridview C#

查看:29
本文介绍了在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.

我的活动具有此代码.问题是 GVBookDetails.FindControl 返回null,我不明白为什么,因为我拥有该控件.

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);
}

推荐答案

您似乎正在尝试在GridView本身中找到TextBox.不是按钮和文本框所在的行.您可以使用发件人的NamingContainer查找文本框.

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!";
}

这篇关于在gridview C#上操作文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆