阅读在GridView动态创建的文本框文本框、动态、GridView

2023-09-07 16:34:54 作者:我的五杀你抢不了¥

我是动态地添加一个GridView的文本框的某些行(一列)。我这个insdie的测试条件添加控件(正常工作):

I am dynamically adding a textbox to certain rows (one column only) of a 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

我希望用户能够输入值到一个文本框,这些或更多,然后更新与一个单一的按一下按钮(不点击的每一行)数据库。我遇到的问题是如何访问一个按钮单击事件的值。我做了一个简单的测试,这种方法试试,看看该值在第二排,但得到空的temp1(我敢肯定有在文本框中输入的值):

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):

    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.

感谢。

编辑:这就是我现在所在。我可以看到在我的专栏精细的文本框。我把一个破发在上一个按钮,尝试阅读,这是我所看到的。如果我检查GridView2.Rows [0] .Controls.Count,我得到8,这是列correect数量。如果我检查GridVeiw2.Rows [0] .Cells [4] .Controls.Count,我得到0,这是错误的,因为文本框是存在的。之后我动态地创建它,但我可以计数1而不是当我进行后续按钮点击。

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. If I check GridVeiw2.Rows[0].Cells[4].Controls.Count, I get 0, which is wrong because the textbox is there. I can get a Count of 1 right after I dynamically create it but not when I perform a subsequent button click.

谁能解释一下吗?我觉得如果我能突破这个持枪抢劫,我可以得到休息完成。

Can anyone explain this? I feel if I can get past this holdup, I can get the rest done.

再次感谢。

推荐答案

您需要一个ID分配给TextBox控件,然后通过该ID访问他们的FindControl()。此外,请确保您要添加的控件在页面的Init生命周期()方法。这样,它被添加到ViewState的。

You need to assign an ID to the TextBox controls and then access them by that ID in FindControl(). Also, make sure you're adding the controls in the Page's Init() method of the life-cycle. That way it gets added to ViewState.

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

编辑:我只是想起另一种可能的解决方案。相反,在$ C $编程创建文本框控件的C-背后只是在GridView中创建一个模板列。

I just remembered another possible solution. Instead of programatically creating the TextBox controls in the code-behind just create a TemplateField in the GridView.

Add文本框模板列列到GridView控件编程