[RESOLVED] Gwtting values from dynamic fields

Hello, anyone know of anyway to get the value of this dynamic data? I've been very stress trying to get the value of this fields. but I dont know how should I code upon the submit button to get the dynamic value, I'm doing a 3 tier, with dataset XSD, using can anyone advise me what to do?

By the way, its a button to generate the dynamic fields, and each dynamic fields id is "dynamic field name" + i, so please kindly go through the codes and give me a solution~ its urgent! thanks in advance!

Its quite long though :x

protected void btnGen_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < Convert.ToInt32(tbNum.Text); i++)
        {

            // label and textbox needed
            Label lblSch = new Label();
            TextBox tbSch = new TextBox();
            Label lblWsGrp = new Label();
            DropDownList ddlWsGrp = new DropDownList();
            RadioButtonList rblTra = new RadioButtonList();
            Label lblSTime = new Label();
            Label lblETime = new Label();
            TextBox tbSTime = new TextBox();
            TextBox tbETime = new TextBox();
            Label lblstar = new Label();
            Label space = new Label();
            space.Text = " ";
            Label lbltime = new Label();
            lbltime.Text = "-example format:08:00/13:15-";
            Label lbltime2 = new Label();
            lbltime2.Text = "-example format:08:00/13:15-";
           

            //calendar control for sess date
            AjaxControlToolkit.CalendarExtender CeSess = new AjaxControlToolkit.CalendarExtender();
            CeSess.ID = "SessDate" + i;
            CeSess.Format = "dd/MM/yyyy";
            CeSess.TargetControlID = "tbSch" + i;
            CeSess.TodaysDateFormat = "dd/MM/yyyy";
            CeSess.Enabled = true;
            CeSess.CssClass = "cal_Theme1";
            CeSess.EnableViewState = true;

           //sess date
            lblSch.ID = "lblSch" + i;
            lblSch.Text = "Session Date : ";
            lblSch.EnableViewState = true;

            tbSch.ID = "tbSch" + i;
            tbSch.Width = 210;
            tbSch.Height = 27;
            tbSch.EnableViewState = true;

            //workshop grp
            lblWsGrp.ID = "lblwsgrp" + i;
            lblWsGrp.Text = "Workshop Group : ";
            lblWsGrp.EnableViewState = true;

            ObjectDataSource obddl = new ObjectDataSource();
            obddl.ID = "obddl" + i;
            obddl.TypeName = "BusinessLogic.RegBLL";
            obddl.SelectMethod = "GetAllReg";
            obddl.DataBind();

            ddlWsGrp.ID = "ddlWsGrp" + i;
            ddlWsGrp.Height = 27;
            ddlWsGrp.Width = 210;
            ddlWsGrp.Items.Add("1");
            ddlWsGrp.Items.Add("2");
            //ddlWsGrp.DataSourceID = obddl.ID;
            //ddlWsGrp.AppendDataBoundItems = true;
            //ddlWsGrp.DataTextField.Equals("wsGrp");
           // ddlWsGrp.DataValueField.Equals("regID");
            ddlWsGrp.EnableViewState = true;

            //Start Time
            lblSTime.ID = "ST" + i;
            lblSTime.Text = "Start Time : ";
            lblSTime.EnableViewState = true;

            tbSTime.ID = "strt" + i;
            tbSTime.Width = 210;
            tbSTime.Height = 27;
            tbSTime.EnableViewState = true;

            //End Time
            lblETime.ID = "ET" + i;
            lblETime.Text = "End Time : ";
            lblETime.EnableViewState = true;

            tbETime.ID = "endt" + i;
            tbETime.Width = 210;
            tbETime.Height = 27;
            tbETime.EnableViewState = true;           
           

            //table to store data
            TableRow trow = new TableRow();
            TableRow trow2 = new TableRow();
            TableRow trow3 = new TableRow();
          

            //row1
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();
            TableCell tc4 = new TableCell();
            TableCell tc45 = new TableCell();
            //row2
            TableCell tc25 = new TableCell();
            TableCell tc26 = new TableCell();
            TableCell tc27 = new TableCell();
            TableCell tc28 = new TableCell();
            //row3
            TableCell tc29 = new TableCell();
            TableCell tc30 = new TableCell();
            TableCell tc31 = new TableCell();
            TableCell tc32 = new TableCell();           

            tc1.Controls.Add(lblSch);
            tc2.Controls.Add(tbSch);
            tc3.Controls.Add(lblWsGrp);
            tc4.Controls.Add(ddlWsGrp);
            tc45.Controls.Add(space);

           tc25.Controls.Add(lblSTime);
            tc26.Controls.Add(tbSTime);
            tc27.Controls.Add(lblETime);
            tc28.Controls.Add(tbETime);

            tc29.Controls.Add(space);
            tc30.Controls.Add(lbltime);
            tc31.Controls.Add(space);
            tc32.Controls.Add(lbltime2);

          trow.Cells.Add(tc1);
            trow.Cells.Add(tc2);
            trow.Cells.Add(tc3);
            trow.Cells.Add(tc4);
            trow.Cells.Add(tc45);
             trow2.Cells.Add(tc26);
            trow2.Cells.Add(tc27);
            trow2.Cells.Add(tc28);
            trow3.Cells.Add(tc29);
            trow3.Cells.Add(tc30);
            trow3.Cells.Add(tc31);
            trow3.Cells.Add(tc32);

            this.table3.Rows.Add(trow);
            this.table3.Rows.Add(trow2);
            this.table3.Rows.Add(trow3);
        }
    }

 

 

 

I think you can create a loop on your table rows then a nested loop with the cells , if you're using foreach you wont need the id.

Hi there, thanks for replying,

Do you have an example which i can refer to? thanks :) 

I dont have exact code but it's something like this :

foreach (
TableRow
x in Table1.Rows)
{

     

foreach (
TableCell y 
 in 
TableRow.Cells
)
    {

             foreach(Control ctrl in y .Controls)
              {
               }

       }

    }

}


Hi, am i right to declare it this way? because i need to get the values from 4 fields, namely 3 textboxes and from 1 dropdownlist :), and then to pass it to insert into my database :)

foreach (TableRow row in table3.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Control c in cell.Controls)
                    {
                        for(int r= 0 ; r <cell.Controls.Count; r++)
                        {
                          
                            if(c.GetType() == typeof(TextBox))
                            {
                                if((c.ID.Contains("tbSch")))
                                {
                                    TextBox tbSch = c as TextBox;
                                    string sessDate = tbSch.Text;
                                }
                                else if ((c.ID.Contains("strt")))
                                {
                                    TextBox tbST = c as TextBox;
                                    string sTime = tbST.Text;
                                }
                                else if ((c.ID.Contains("endt")))
                                {
                                    TextBox tbET = c as TextBox;
                                    string sTime = tbET.Text;
                                }
                         
                            }
                            else if (c.GetType() == typeof(DropDownList))
                            {
                                DropDownList ddlWs = c as DropDownList;
                                string wsGrp = ddlWs.SelectedValue.ToString();
                            }
                        }
                    }
                }
            }

foreach (TableRow row in table3.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Control c in cell.Controls)
                    {
                            if(c.GetType() == typeof(TextBox))
                            {
                                if((c.ID.Contains("tbSch")))
                                {
                                    TextBox tbSch = c as TextBox;
                                    string sessDate = tbSch.Text;
                                }
                                else if ((c.ID.Contains("strt")))
                                {
                                    TextBox tbST = c as TextBox;
                                    string sTime = tbST.Text;
                                }
                                else if ((c.ID.Contains("endt")))
                                {
                                    TextBox tbET = c as TextBox;
                                    string sTime = tbET.Text;
                                }
                         
                            }
                            else if (c.GetType() == typeof(DropDownList))
                            {
                                DropDownList ddlWs = c as DropDownList;
                                string wsGrp = ddlWs.SelectedValue.ToString();
                            }
                    
                    }
                }
            }


you're having an extra loop you're already looping on controls so no need for the last nested for , else than this I guess you're right .



Hey, thanks for the reply, I tried debugging, but I cannot seem to see the value, lol so im not sure whether its containing values :) 

Hi,

According to your code and description ,I would like to suggest you to check the link below for the similar thread which provides some solutions for the same issue.

http://forums.asp.net/p/1646901/4281893.aspx

Hope this helps.

thoughts on "[RESOLVED] Gwtting values from dynamic fields"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>