Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear all

when i create a textbox dynamically for the particular column, it would created but when i trying to get the textbox value's it will shows the object reference is not set to an instance of object

protected void gridUptMark_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 3; i < e.Row.Cells.Count; i++)
                {
                    TextBox tbox = new TextBox();
                    TableCell cell = e.Row.Cells[i];
                    tbox.ID = "txt";
                    cell.Controls.Add(tbox);
                }
            }
        }


C#
protected void Update_Click(object sender, EventArgs e)
        {
            string test;
            for (int i = 0; i < gridUptMark.Rows.Count; i++)
            {
                for (int j = 0; j < gridUptMark.Rows[i].Cells.Count; j++)
                {
                    TableCell cell = (TableCell)gridUptMark.Rows[i].Cells[j];
                    test = cell.FindControl("txt").ToString();
                }
            }
        }
Posted

1 solution

Hello,
You first have to be sure that your gridview will be recreated after each postback. If you press the update button, it causes a post back and the dynamically created Textboxes will be lost. So let's say we have this dummy data table:
C#
DataTable DummyDT()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("FirstValue", Type.GetType("System.Int32"));
    dt.Columns.Add("SecondValue", Type.GetType("System.Int32"));
    dt.Columns.Add("Result", Type.GetType("System.Int32"));
    dt.Columns.Add("Placeholder1", Type.GetType("System.Int32"));
    dt.Columns.Add("Placeholder2", Type.GetType("System.Int32"));
    dt.Columns.Add("Placeholder3", Type.GetType("System.Int32"));

    for (int i = 0; i <= 10; i++)
    {
        DataRow dr;
        dr = dt.NewRow();
        dr["FirstValue"] = i;
        dr["SecondValue"] = i * i;
        dr["Result"] = i + i * i;
        dr["Placeholder1"] = 0;
        dr["Placeholder2"] = 0;
        dr["Placeholder3"] = 0;
        dt.Rows.Add(dr);
    }

    return dt;
}

You'll have to rebind it to your Gridview after each postback:
C#
protected void Page_Load(object sender, EventArgs e)
{
    gridUptMark.DataSource = DummyDT();
    gridUptMark.DataBind();
}

(You will not have this problem with SQLDatasource, ObjectDataSource and so on..)
Assured this, you can create your textbox in the rowdatabound event, but you'll get an error in your code because you are creating textboxes with the same ID, so here's the fix:
C#
tbox.ID = "txt" + i;

Finally, you can retreive your values. But your code had some problems here too, here's a fix:
C#
protected void Update_Click(object sender, EventArgs e)
    {
        string test;
        for (int i = 0; i < gridUptMark.Rows.Count; i++)
        {
            for (int j = 3; j < gridUptMark.Rows[i].Cells.Count; j++)
            {
                TableCell cell = (TableCell)gridUptMark.Rows[i].Cells[j];
                test = ((TextBox)cell.FindControl("txt" + j)).Text;
                Response.Write(test);
            }
        }
    }

j starts from 3 because in the first 3 cells you don't have textboxes, that's why you had the System.NullReferenceException error (important: Findcontrol("txt" + j) or it won't find your textboxes).
And also your line:
C#
test = cell.FindControl("txt").ToString();

would return the string "System.Web.UI.WebControls.TextBox" instead of the textbox value.
Hope it helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900