简体   繁体   中英

Read a dynamically created textbox in a Gridview

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

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.

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. 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.

Thanks again.

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

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.

Add Textbox TemplateField Column To GridView Programmatically

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. That way, the control will always be there and you don't have to worry about the lifecycle stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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