locked
Dynamically Generate Table and Update Database RRS feed

  • Question

  • User1307192253 posted

    I have dynamically generated table, with loaded data from db.

     

    * I assign  3 columns,  1 for description, 2 for current value , 3 for New value....

    * then I want to update 2nd column's Current value by 3rd column's value (this values user will enter) 

    table look like this

    ------------------------------------- 

    description         Value 

    userID                    205

    UserName                Prasan

    ------------                    -------

    ------------                    -------

     

    rows can be increase thats y  I thought to generate table dynamically               

     -------------------------------------

    ( Table in database only have  2 columns (1st & 2nd Columns) can use 1st column for reference when update table ) 

    i already done this.........

     Table tbl   = new Table();

      Panel1.Controls.Add(tbl);

      foreach(DataRow dataRow in DS.Tables[0].Rows)
                {
                    tr          = new TableRow();

                    for (int Column = 0; Column < tblCols; Column++)
                    {
                        tc      = new TableCell();
                        txtBox  = new TextBox();
                        lbl     = new Label();


                       if (Column == 0)
                        {                     
                            tc.Controls.Add(lbl);                       
                            lbl.ID             = "lbl"+Row;
                            lbl.Text         = dataRow[0].ToString();    //  description for current values   
                 
                        }
                        else
                        {
                         
                            if (Column == 1)
                            {
                                txtBox.ID                   = "txtCurrent" + Row;
                                txtBox.Text               = dataRow[1].ToString();
                                txtBox.ReadOnly      = true;                               // read only Current values from db           
                            }

                            else if(Column == 2)
                            {                        
                                    txtBox.ID   = "txtNew" + Row;                 //    New Value for replace with Current Value
                            }


                            tc.Controls.Add(txtBox);


                        }
                    
                        tr.Cells.Add(tc);     // Add the TableCell to the TableRow
                    }
                  
                    tbl.Rows.Add(tr);  // Add the TableRow to the Table
                    Row++;
                }
          
              
                ViewState["dynamictable"]           = true;     // This parameter helps determine in the LoadViewState event,

     

     

    Does anyone know a better way for handling that?

    Monday, July 14, 2008 1:01 PM

Answers

  • User-330204900 posted

    In that case you could as long as you are adding server controls to the page you should be able to loop through the controls:

    foreach (Control c in placeholder.Controls)
    {
        if (c.ID == ControlId)
        {
        }
    }

    I would surround the table with a placeholder. the above is simplistic you may need to test if a control has sub controls and itterate through them too.

    Hope this helps [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2008 11:21 AM

All replies

  • User-330204900 posted

    Hi prasanucsc, I'm not being obstructive are you using Dynamic Data, if you are you may wish to look at Dynamic Data Futures which has a DynamicObjectDataSourceTest which you could use to construct your model.

     

    Monday, July 14, 2008 1:27 PM
  • User-797310475 posted

    Hi,

    it does not seem like you are using ASP.NET Dynamic Data. You might have better luck on the general data access forum.

    Monday, July 14, 2008 1:32 PM
  • User526484161 posted

    Do you really need to dynamically create table? if not then you can achieved this feature using gridview with template fields that will help you input from the user, calculating values using other columns and save data with less coding.

    Monday, July 14, 2008 4:17 PM
  • User1307192253 posted

     yep , i must create the dynamic table.... and pass those new text boxes values to the SP (sql)

    Monday, July 14, 2008 11:31 PM
  • User-319574463 posted

     You have posed an intriguing problem - one that I would like to investigate once I have fixed some other peoples problems. Should you fix the problem yourself, please post a note to say so.

    And now just to clarify your requirement:

    • Which version of SQL Server are you using?
    • Which version of Visual studio are you using?
    Tuesday, July 15, 2008 7:32 AM
  • User-330204900 posted

    I still believe this is in the wrong forum! Which Forum Did you post to originally

    Tuesday, July 15, 2008 7:35 AM
  • User1307192253 posted

     hay...

    Yep ... I'm using VS 2005 and SQL server 2005....

     

    thanks

    regards.

    samee 

    Tuesday, July 15, 2008 7:46 AM
  • User-319574463 posted

     Why the need for variable data type?

    Wednesday, July 16, 2008 1:42 AM
  • User1307192253 posted

     i tried to add date time  picker  after the  text box, when dataType = TimeDate

    &  i thought  when i added that field i could validate by identifying whether text box values are date or not...

     ........................................................................

    thanks for reply....................... 

    Wednesday, July 16, 2008 9:28 AM
  • User-330204900 posted

    Have you tried DynamicData and DynamicDataFutures?

    Wednesday, July 16, 2008 12:27 PM
  • User-611396106 posted

     Hi,

     

    I am trying to implement something similar to what you did. I created a dynamic html table using the data from sqlserver. the dynamic html table contains textboxes which allow user to update the cell value. I also have a SAVE button which allow user to save the changes to the database. My problem is in the on click event of the SAVE button, I cannot find the textboxes in the table (in fact, I cannot find the table control). Also when the page returns (after postback), the dynamic table  disappeared. I know it has to do with the page event lifecycle as well as the viewstate of the table. The create dynamic table code is placed under the OnInit event as below


    protected override void OnInit(EventArgs e)
            {
                createDynamicTable();

           }

     What did I do wrong? Can you give me some pointers how did you maintain the viewstate of the table and the changed data?

     

    Thanks for any help in advance

               

    Tuesday, August 12, 2008 11:30 PM
  • User-330204900 posted

    I know it has to do with the page event lifecycle

    You are right, but that is the reason that you can't see it after clicking your button because you have posted back to the server. How are you creating your table on the page?

    Wednesday, August 13, 2008 5:13 AM
  • User-611396106 posted

    Basically, I call the createDynamicTable routine in the OnInit event as seen below.

    protected override void OnInit(EventArgs e)
            {
                createDynamicTable();       }

     

    private void CreateDynamicTable()
    {
        Table tbl   = new Table();
        Panel1.Controls.Add(tbl);
        foreach(DataRow dataRow in DS.Tables[0].Rows)
        {
            tr = new TableRow();
            for (int Column = 0; Column < tblCols; Column++)
            {
                tc      = new TableCell();
                txtBox  = new TextBox();
                lbl     = new Label();
                if (Column == 0)
                {                     
                    tc.Controls.Add(lbl);                       
                    lbl.ID = "lbl"+Row;
                    lbl.Text= dataRow[0].ToString();    //  description for current values                
                }
                else
                {
                    if (Column == 1)
                    {
                        txtBox.ID = "txtCurrent" + Row;
                        txtBox.Text= dataRow[1].ToString();
                        txtBox.ReadOnly= true;   // read only Current values from db           
                    }
                    else if(Column == 2)
                    {                        
                        txtBox.ID   = "txtNew" + Row;//    New Value for replace with Current Value
                    }
                    tc.Controls.Add(txtBox);
                }

                tr.Cells.Add(tc);     // Add the TableCell to the TableRow
            }

            tbl.Rows.Add(tr);  // Add the TableRow to the Table
            Row++;
        }
    }

    The challenge is how to preserve the table after user clicking the button. Also if the value in the text boxes (inside the dynamic table) has been changed, how can I preserve those values?

     

    Thanks again for any tips

    Wednesday, August 13, 2008 8:10 AM
  • User-330204900 posted

    It looks to me like you may be better using standard DynamicData and adding a custom IAutoFieldGenerator to the GridView etc. I the Row/ColumnGerator (IAutoFieldGenerator) you can add Dynamic Fields with DynamicControls inside and give the DynsmicControl a UIHint to set the FieldTemplate to be used.

    It just seems like you are trying to do what DynamicData already does [:|]

    Hope this helps [:D]

    Wednesday, August 13, 2008 9:32 AM
  • User-611396106 posted

     Would the approach you suggested work with three different datasets?

    The column comes from dsCol, the row comes from dsRow and the cell comes from dsCell. What I was trying to accomplish is to create a "cross tab" table.

     

    Also, I am using framework 2.0


    Wednesday, August 13, 2008 9:57 AM
  • User-330204900 posted

    Is there a reason why you can't use ASP.Net 3.5 SP1 with Dynamic Data, if not then download Visual Studio 2008 SP1 and install then create a Dynamic Data website see David Ebbo's video from MIX08 Dynamic Data at MIX, and upcoming changes beare in  mind that it is from some time ago and it does a lot mor now!

    Hope this helps [:D]

    Wednesday, August 13, 2008 10:29 AM
  • User-611396106 posted

     Unfortunately, I have to stay with 2.0. Do you have any idea who to accomplish what I needed in 2.0?

    Wednesday, August 13, 2008 10:50 AM
  • User-330204900 posted

    In that case you could as long as you are adding server controls to the page you should be able to loop through the controls:

    foreach (Control c in placeholder.Controls)
    {
        if (c.ID == ControlId)
        {
        }
    }

    I would surround the table with a placeholder. the above is simplistic you may need to test if a control has sub controls and itterate through them too.

    Hope this helps [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2008 11:21 AM
  • User-611396106 posted

     the problem is, after post back, i wont be able to find the control...

    Wednesday, August 13, 2008 11:58 AM
  • User-330204900 posted

    Err! you will because I wrote a Crystal Reports Server that dynamically created the parameters of the reports and retrieved the values from them after post back [:D] just wrap everything in a literal or placeholder control and use it to itterate through the controls collection.

    It will work because it does work [:D]

    Wednesday, August 13, 2008 1:15 PM
  • User-611396106 posted

     you are absolutely right. The reason I couldn't find the control in the first hand was because the textbox control is within the tablecell control, which is within the tablerow control of a table control. I just need to go a few level down before the loop.

     

    Thanks all for your help!!

    Thursday, August 14, 2008 7:08 AM