Hi ,

I need to create a page that has databound controls . eg.

Say i have a row with the following columns
1. TrackKey VARCHAR(50)
2. State VARCHAR(50)
3. Reason VARCHAR(50)
4. DateChanged DATETIME

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

Now for each row in the table, the controls should be like this
1. TrackKey - Label
2. State - dropdownlist
3. Reason textbox
4. DateChanged - 3 dropdownlists [years, months , days]

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

I am able to dynamically load these controls, but due to postback i cannot save the information that changed.

can anyone give me some advice on how to change this ?

here is my code for loading:

void GenerateList()
        {
            // This method will generate a table for editing states

            // Dataset for storing values
            DataSet setGetTrackings = new DataSet();

            // In the grid - the controls
            Label TrackKey = new Label();
            DropDownList SetState = new DropDownList();
            TextBox reason = new TextBox();
            DropDownList Days = new DropDownList();
            DropDownList Months = new DropDownList();
            DropDownList Years = new DropDownList();
            HtmlTableCell htmlCell = null;
            HtmlTableRow htmlRow = null;

            HandleStates entry = new HandleStates();

            // finally we will count the rows for inserting
            int rowcount = 2;

            int IndexCount = 0;

            //Set the tables.
            if (setGetTrackings != null && setGetTrackings.Tables.Count > 0)
            {
                foreach (DataRow row in setGetTrackings.Tables[0].Rows)
                {
                    // Create the controls.
                    TrackKey = new Label();
                    SetState = new DropDownList();
                    htmlCell = null;
                    htmlRow = null;
                    reason = new TextBox();
                    Days = new DropDownList();
                    Months = new DropDownList();
                    Years = new DropDownList();

                    #region Populate Controls
                    // Set the values
                    TrackKey.Text = row[0].ToString();
                    SetState.Items.Add(row[7].ToString());
                    reason.TextMode = TextBoxMode.MultiLine;

                    // Add more Items To The DropDown
                    SetState.Items.Add("--New State--");
                    SetState.Items.Add("Visiting x Branch");
                    SetState.Items.Add("Visiting y Branch");
                    SetState.Items.Add("Received");
                    SetState.Items.Add("Ready To Go");
                    SetState.Items.Add("Not Ready To Go");
                    SetState.Items.Add("Submitted");
                    SetState.Items.Add("Acquired");
                    SetState.Items.Add("Denied");

                    // Adding attributes to the dropdown

                    int IDDay = 10000000 + rowcount;
                    int IDMonth = 11000000 + rowcount;
                    int IDYear = 12000000 + rowcount;
                    int IDTetbox = 13000000 + rowcount;

                    SetState.ID = rowcount.ToString();
                    reason.ID = IDTetbox.ToString();
                    Days.ID = IDDay.ToString();
                    Months.ID = IDMonth.ToString();
                    Years.ID = IDYear.ToString();

                    int D1 = 1;
                    int D2 = 32;

                    while (D1 < D2)
                    {
                        Days.Items.Add(D1.ToString());
                        D1++;
                    }

                    int M1 = 1;
                    int M2 = 13;

                    while (M1 < M2)
                    {
                        Months.Items.Add(M1.ToString());
                        M1++;
                    }

                    string CurrentYear = string.Format("{0:yyyy}", DateTime.Now);

                    int Y1 = int.Parse(CurrentYear);
                    int Y2 = int.Parse(CurrentYear) + 3;

                    while (Y1 < Y2)
                    {
                        Years.Items.Add(Y1.ToString());
                        Y1++;
                    }

                    #endregion

                    // Create the row
                    htmlRow = new HtmlTableRow();

                    // Populate the cells

                    #region Populate the main cells
                    // Create the track key cell.
                    htmlCell = new HtmlTableCell();
                    htmlCell.Attributes.Add("class", "UserManageField");
                    htmlCell.Controls.Add(TrackKey);
                    htmlRow.Cells.Add(htmlCell);

                    // Create the SetState cell.
                    htmlCell = new HtmlTableCell();
                    //htmlCell.Attributes.Add("class", "FormtextBox");
                    htmlCell.Controls.Add(SetState);
                    htmlRow.Cells.Add(htmlCell);
                    #endregion

                    tblTrackings.Rows.Add(htmlRow);

                    rowcount++;

                    // New Row To insert hidden fields
                    htmlRow = new HtmlTableRow();

                    #region Blank cells
                    // adding seven blank columns
                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);
                    #endregion


                    // Add some attributes
                    reason.Style.Add("visibility", "hidden");
                    Days.Style.Add("visibility", "hidden");
                    Months.Style.Add("visibility", "hidden");
                    Years.Style.Add("visibility", "hidden");

                    // Create the date cell.
                    htmlCell = new HtmlTableCell();
                    reason.Text = "This is a hidden textbox";
                    htmlCell.Controls.Add(reason);
                    htmlCell.Controls.Add(Days);
                    Days.Text = "1";
                    htmlCell.Controls.Add(Months);
                    Months.Text = "1";
                    htmlCell.Controls.Add(Years);
                    Years.Text = "2009";
                    htmlRow.Cells.Add(htmlCell);

                    tblTrackings.Rows.Add(htmlRow);

                    rowcount++;

                    // New Row To insert hidden fields
                    htmlRow = new HtmlTableRow();

                    #region Blank cells
                    // adding seven blank columns
                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);

                    htmlCell = new HtmlTableCell();
                    htmlCell.InnerText = " ";
                    htmlRow.Cells.Add(htmlCell);
                    #endregion

                    // Create the date cell.
                    htmlCell = new HtmlTableCell();
                    htmlCell.Controls.Add(Days);
                    Days.Text = "1";
                    htmlCell.Controls.Add(Months);
                    Months.Text = "1";
                    htmlCell.Controls.Add(Years);
                    Years.Text = "2009";
                    htmlRow.Cells.Add(htmlCell);

                    tblTrackings.Rows.Add(htmlRow);

                    rowcount++;

                    #region Script
                    SetState.SelectedIndex = 0;
                    Days.SelectedIndex = 0;
                    Months.SelectedIndex = 0;
                    Years.SelectedIndex = 0;

                    SetState.Attributes.Add("onChange", "SetVisibility();");

                    StringBuilder scriptBuilder = new StringBuilder();

                    scriptBuilder.Append("<script type='text/javascript' language='javascript'>");

                    scriptBuilder.Append("function SetVisibility()");
                    scriptBuilder.Append("{");

                    scriptBuilder.AppendFormat("var ddSetstate = document.getElementById('{0}'); ", SetState.ClientID);
                    scriptBuilder.Append("var ddSetstate_selectedelement = ddSetstate.options[ddSetstate.selectedIndex].text; ");
                    scriptBuilder.AppendFormat("var ReasonTextBox = document.getElementById('{0}'); ", reason.ClientID);
                    scriptBuilder.AppendFormat("var dayCBO = document.getElementById('{0}'); ", Days.ClientID);
                    scriptBuilder.AppendFormat("var monthCBO = document.getElementById('{0}'); ", Months.ClientID);
                    scriptBuilder.AppendFormat("var yearCBO = document.getElementById('{0}'); ", Years.ClientID);

                    scriptBuilder.Append("if (ddSetstate_selectedelement == 'Not Ready To Go') ");
                    scriptBuilder.Append(" { ");
                    scriptBuilder.Append("     ReasonTextBox.style.visibility = ''; ");
                    scriptBuilder.Append("     dayCBO.style.visibility = 'hidden'; ");
                    scriptBuilder.Append("     monthCBO.style.visibility = 'hidden'; ");
                    scriptBuilder.Append("     yearCBO.style.visibility = 'hidden'; ");
                    scriptBuilder.Append(" } ");

                    scriptBuilder.Append("else if (ddSetstate_selectedelement == 'Successfully Submitted') ");
                    scriptBuilder.Append(" { ");
                    scriptBuilder.Append("     dayCBO.style.visibility = ''; ");
                    scriptBuilder.Append("     monthCBO.style.visibility = ''; ");
                    scriptBuilder.Append("     yearCBO.style.visibility = ''; ");
                    scriptBuilder.Append("     ReasonTextBox.style.visibility = 'hidden'; ");
                    scriptBuilder.Append(" } ");

                    scriptBuilder.Append(" else ");
                    scriptBuilder.Append(" { ");
                    scriptBuilder.Append("     dayCBO.style.visibility = 'hidden'; ");
                    scriptBuilder.Append("     monthCBO.style.visibility = 'hidden'; ");
                    scriptBuilder.Append("     yearCBO.style.visibility = 'hidden'; ");
                    scriptBuilder.Append("     ReasonTextBox.style.visibility = 'hidden'; ");
                    scriptBuilder.Append(" } ");

                    scriptBuilder.Append("JSetStates();");

                    scriptBuilder.Append(" } ");

                    scriptBuilder.Append("</script>");

                    base.ClientScript.RegisterClientScriptBlock(typeof(void), "Myscripts", scriptBuilder.ToString());
                    #endregion
                }
            }

PLEASE HELP !!!!!!!

Are you creating the controls on the postback? If you don't then you won't see the updated data.

my friend u are creating controls on the fly..

i think you should save the data cumming frm database to an object..

and updating the data update the object...and load them again..

i think using session u can figure out the situation..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.