简体   繁体   English

在Gridview中读取动态创建的文本框

[英]Read a dynamically created textbox in a Gridview

I am dynamically adding a textbox to certain rows (one column only) of a gridview. 我正在将文本框动态添加到gridview的某些行(仅一列)。 I add the controls with this insdie of a test condition (works fine): 我用这个测试条件插件添加了控件(可以正常工作):

        TextBox txtASIN = new TextBox();
        txtASIN.ID = "TxtASIN" + e.Row.RowIndex;
        e.Row.Cells[4].Controls.Add(txtASIN);
        int i = e.Row.Cells[4].Controls.Count; //TEST: This returns 1 correctly

I want the user to be able to enter values into one or more of these textboxes and then update the database with a single button click (not one click for each row). 我希望用户能够在一个或多个这些文本框中输入值,然后单击一次按钮即可更新数据库(每一行都不要单击一次)。 The problem I'm having is how to access those values on a button click event. 我遇到的问题是如何在按钮单击事件上访问这些值。 I did a simple test this way to try to see the value in the second row but get null in temp1 (I am certain there is a value entered in that textbox): 我用这种方法做了一个简单的测试,试图在第二行中看到该值,但在temp1中得到null(我确定在该文本框中输入了一个值):

    protected void btnUpdate1_Click(object sender, EventArgs e)
    {
        TextBox temp = (TextBox)GridView2.Rows[1].FindControl("txt1");
        string temp1 = temp.Text;
        int i = GridView2.Row.Cells[4].Controls.Count; //TEST: This returns 0 incorrectly        }

Once I can make this work, I can iterate through the rows and do what I need to do with the values. 一旦可以完成这项工作,就可以遍历行并执行需要对值进行的操作。 I don't know if the text entered in the textbox is actually readable without a postback but I'm otherwise stumped. 我不知道是否在文本框中输入的文本在没有回发的情况下实际上是可读的,但否则我很困惑。 Open to better suggestions on how to do this. 公开寻求有关如何执行此操作的更好建议。

Thanks. 谢谢。

EDIT: Here is where I am now. 编辑:这是我现在的位置。 I can see the textboxes in my column fine. 我可以看到我专栏中的文本框。 I put a break in on a button that attempts to read them and this is what I'm seeing. 我在试图读取它们的按钮上插入了一个中断,这就是我所看到的。 If I check GridView2.Rows[0].Controls.Count, I get 8, which is the correect number of columns. 如果我检查GridView2.Rows [0] .Controls.Count,我得到8,这是正确的列数。 If I check GridVeiw2.Rows[0].Cells[4].Controls.Count, I get 0, which is wrong because the textbox is there. 如果我检查GridVeiw2.Rows [0] .Cells [4] .Controls.Count,我得到0,这是错误的,因为文本框在那里。 I can get a Count of 1 right after I dynamically create it but not when I perform a subsequent button click. 在动态创建计数后,我可以立即获得Count值1,但在随后单击按钮时却不能。

Can anyone explain this? 谁能解释一下? I feel if I can get past this holdup, I can get the rest done. 我觉得如果我能克服这个难题,我可以完成剩下的一切。

Thanks again. 再次感谢。

You need to assign an ID to the TextBox controls and then access them by that ID in FindControl(). 您需要为TextBox控件分配一个ID,然后在FindControl()中通过该ID访问它们。 Also, make sure you're adding the controls in the Page's Init() method of the life-cycle. 另外,请确保要在生命周期的Page的Init()方法中添加控件。 That way it gets added to ViewState. 这样,它将被添加到ViewState。

TextBox txt1 = new TextBox();
txt1.ID = "txt1"; 
e.Row.Cells[4].Controls.Add(txt1);

EDIT: I just remembered another possible solution. 编辑:我只记得另一个可能的解决方案。 Instead of programatically creating the TextBox controls in the code-behind just create a TemplateField in the GridView. 无需以编程方式在背后的代码中创建TextBox控件,而只需在GridView中创建一个TemplateField。

Add Textbox TemplateField Column To GridView Programmatically 以编程方式将Textbox TemplateField列添加到GridView

I would try a different approach, putting the text box in the html markup of the page and then control the visible or readonly property of it on the ItemDataBound event. 我会尝试另一种方法,将文本框放在页面的html标记中,然后在ItemDataBound事件上控制其可见或只读属性。 That way, the control will always be there and you don't have to worry about the lifecycle stuff. 这样,控件将始终存在,而您不必担心生命周期问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM