Solved

Create Grids in Runtime + Asp.net + Cannot get value

Posted on 2010-09-02
2
431 Views
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.Columns.Add(tf);

                    }

                    gv.DataSource = dtStructured;

                    gv.DataBind();









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

                    e.Row.Cells[i].Controls.Add(hit);

                }

            }

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

                            myStrValues.AddItems(strVal);

                        }

                    }

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

Open in new window

0
Comment
Question by:mivbinfotech
  • 2
2 Comments
 
LVL 2

Author Comment

by:mivbinfotech
ID: 33585004
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.
0
 
LVL 2

Accepted Solution

by:
mivbinfotech earned 0 total points
ID: 33593406
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

861 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

30 Experts available now in Live!

Get 1:1 Help Now