Pau Gacusan
Pau Gacusan

Reputation: 67

Is there a way to get the values of Dynamically created Textbox inside a Dynamically created GridView? Asp.net

I have an aspx page used for evaluation. One of the requirements for the page is to create multiple Gridviews to act as the evaluation form. The Gridview is populated by the questions from my database and textbox alongside the questions. So Column1 = Questions and Column2 = Textbox. The number of Gridviews created depends on the value that the user typed inside a textbox.

My problem is, since it is dynamically created, I don't know how am I suppose to retrieve the values typed in the textbox. I need them to be saved on my database. How should I do this? Or is there a better approach to this?

My Code:

protected void btnOk_Click(object sender, EventArgs e)
{
    btnSave.Visible = true;
    int parCounter = Convert.ToInt32(participants.Text);
    DataTable dt = new DataTable();

    using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString))
    {
        string sql = "select * from evalForm";

        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }

    for (i = 1; i <= parCounter; i++)
    {
        GridView objGV = new GridView();
        objGV.AutoGenerateColumns = false;
        objGV.ID = "GV"+i;

        for (int col = 0; col < dt.Columns.Count; col++)
        {
            BoundField boundfield = new BoundField();                
            boundfield.DataField = dt.Columns[col].ColumnName.ToString();
            boundfield.HeaderText = dt.Columns[col].ColumnName.ToString();

            objGV.Columns.Add(boundfield);               
        }

        TemplateField tempfield = new TemplateField();
        tempfield.HeaderText = "Score";
        objGV.Columns.Add(tempfield);

        objGV.RowDataBound += new GridViewRowEventHandler(myGrid_RowDataBound);

        compName.Visible = true;
        fromDate.Visible = true;
        toDate.Visible = true;
        participants.Visible = true;

        objGV.DataSource = dt;
        objGV.DataBind();
        Panel1.ContentTemplateContainer.Controls.Add(objGV);
    }        
}

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
            TextBox txtScore = new TextBox();
            txtScore.ID = "tbScore";
            e.Row.Cells[2].Controls.Add(txtScore);
    }
}

Upvotes: 0

Views: 1219

Answers (1)

VDWWD
VDWWD

Reputation: 35544

To find any Dynamic controls you have to use FindControl. This also works for GridView controls.

//loop the dynamic gridviews
for (i = 1; i <= parCounter; i++)
{
    //use findcontrol and cast back to a gridview
    GridView gv = Panel1.ContentTemplateContainer.FindControl("GV" + i);

    //loop all the rows in the gridview
    foreach (GridViewRow row in gv.Rows)
    {
        //use findcontrol again to find the textbox
        TextBox tb = row.FindControl("tbScore") as TextBox;
    }
}

Upvotes: 1

Related Questions