Create Grids in Runtime + + Cannot get value

Posted on 2010-09-02
Last Modified: 2013-11-10
Dear EE Members

We are using VS 2008

We have create a grid X, on runtime inside a repeater control, dynamically (on repeaters item databound control), as we donot know the number of columns and rows in X and it is dependent on the data in the database.  

The issue is all works fine, but we have not been able to capture the value of any control bound to the grid on runtime
label, text or otherwise.

How we tried


Object reference error comes when we run it as per above.

Pleae advise
dtStructured = new StructuredBL().InsertStructureRows(dtStructured);

                    GridView gv = (GridView) pnlStructured.FindControl("gv");

                    gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);

                    foreach (DataColumn dc in dtStructured.Columns)


                        TemplateField tf = new TemplateField();

                        string ctlType = "";

                        if (dc.DataType.Name == "String") { ctlType = "Label"; }

                        else { ctlType = "TextBox"; }

                        tf.HeaderText = dc.ColumnName;

                        tf.FooterStyle.CssClass = dc.Caption;



                    gv.DataSource = dtStructured;


        void gv_RowDataBound(object sender, GridViewRowEventArgs e)


            if (e.Row.RowType == DataControlRowType.DataRow)


                for(int i=0; i<e.Row.Cells.Count; i++)


                    HtmlInputText hit = new HtmlInputText();

                    hit.ID = "CTL" + i.ToString();




            //throw new NotImplementedException();


        protected void cmdSaveDraft_Click1(object sender, EventArgs e)


            foreach (RepeaterItem ri in rptFillDataForm.Items)


                    GridView gv = (GridView)ri.FindControl("gv");

                    MyCollection<StructuredValue> myStrValues = new MyCollection<StructuredValue>();

                    StructuredValue strVal;

                    for (int j=0; j<gv.Columns.Count; j++)


                        for(int row=0; row<gv.Rows.Count; row++)


                            strVal = new StructuredValue();

                            Control ctl = gv.Rows[row].FindControl("txt"+ gv.Columns[j].HeaderText);

                            if(ctl.GetType().Name != "TextBox") { break; }

                            strVal.ColID = Convert.ToInt32(gv.Columns[j].FooterStyle.CssClass);

                            strVal.RowID = row + 1;

                            strVal.DataFormID = objDataFormIndicator.DataForm.ID;

                            strVal.IndicatorID = objDataFormIndicator.Indicator.ID;

                            strVal.Value = Convert.ToDouble(((TextBox)ctl).Text);	// THIS IS WHERE I AM TRYING TO CAPTURE VALUE FROM TEXTBOX




                    int iResult = new StructuredBL().InsertStructuredValues(myStrValues, ((User)Session["CurrentUser"]).ID);

Open in new window

Question by:mivbinfotech
  • 2

Author Comment

Comment Utility
To add to this the .text value is still there even after post back, but we cannot use the values of any controls in that grid.

Accepted Solution

mivbinfotech earned 0 total points
Comment Utility
We needed to save the values in a session and then rebind the grid, as controls generated on run time tend to dissappear on Post Back.

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now