Link to home
Start Free TrialLog in
Avatar of JordanBlackler
JordanBlackler

asked on

How to Save Dynamically created controls

Hello all -
This is on the web using C#.

When a user clicks the Add button it creates 3 labels and 3 textboxes. The most they would click the Add button would be 12 times, anyway...

So, they click the add button, then they would enter text into the 3 textboxes. When the click the Add button again it keeps the data that was previously entered.
So when that's all done, they hit the save button, which saves the values to a SharePoint List.
(I know the ID's of each control.)

When they go back to the SharePoint list to edit the document, is there a way to get those dynamic fields back?

Here is my code that deals with creating the fields:

protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtTotalAmount.Focus();
        int count = 0;
        if (ViewState["count"] != null)
        {
            count = (int)ViewState["count"];
        }
        count++;

        ViewState["count"] = count;
        CreateControls();
    }

private void CreateControls()
    {
        int count = 0;
        if (ViewState["count"] != null)
        {
            count = (int)ViewState["count"];
        }

        while (PlaceHolder1.Controls.Count < count * 10)
        {
            //Create Table
            Literal tableInfo = new Literal();
            tableInfo.Text = "<table>";
            PlaceHolder1.Controls.Add(tableInfo);
            tableInfo = new Literal();
            tableInfo.Text = "<tr><td>";
            PlaceHolder1.Controls.Add(tableInfo);

            // -----INVOICE-----
            //Create First Label
            Label _lblInvoice = new Label();
            PlaceHolder1.Controls.Add(_lblInvoice);
            _lblInvoice.ID = "lbl_Invoice" + PlaceHolder1.Controls.Count.ToString();
            _lblInvoice.Text = "Invoice #:";

            //Create First TextBox
            TextBox _txtInvoice = new TextBox();
            PlaceHolder1.Controls.Add(_txtInvoice);
            _txtInvoice.ID = "txt_Invoice" + PlaceHolder1.Controls.Count.ToString();
            _txtInvoice.Text = "";
            _txtInvoice.Height = 18;
            _txtInvoice.Width = 100;

            // -----AMOUNT-----
            //Create Second Label
            Label _lblAmount = new Label();
            PlaceHolder1.Controls.Add(_lblAmount);
            _lblAmount.ID = "lbl_Amount" + PlaceHolder1.Controls.Count.ToString();
            _lblAmount.Text = "Amount:";

            //Create Second TextBox
            TextBox _txtAmount = new TextBox();
            PlaceHolder1.Controls.Add(_txtAmount);
            _txtAmount.ID = "txt_Amount" + PlaceHolder1.Controls.Count.ToString();
            _txtAmount.Text = "";
            _txtAmount.Height = 18;
            _txtAmount.Width = 100;

            // -----DATE-----
            //Create Third Label
            Label _lblDateRecieved = new Label();
            PlaceHolder1.Controls.Add(_lblDateRecieved);
            _lblDateRecieved.ID = "lbl_DateRecieved" + PlaceHolder1.Controls.Count.ToString();
            _lblDateRecieved.Text = "Date:";

            //Create Third TextBox
            TextBox _txtDateRecieved = new TextBox();
            PlaceHolder1.Controls.Add(_txtDateRecieved);
            _txtDateRecieved.ID = "txt_DateRecieved" + PlaceHolder1.Controls.Count.ToString();
            _txtDateRecieved.Text = "";
            _txtDateRecieved.Height = 18;
            _txtDateRecieved.Width = 100;

            //Close Table
            tableInfo = new Literal();
            tableInfo.Text = "</td></tr>";
            PlaceHolder1.Controls.Add(tableInfo);
            tableInfo = new Literal();
            tableInfo.Text = "</table>";
            PlaceHolder1.Controls.Add(tableInfo);
        }
    }

protected void Page_Load(object sender, EventArgs e)
    {                
        CreateControls();
    }

Thanks for any info!!
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

when you say go back to the sharepoint list do you mean, you want to render an asp page which interprets the data in the SP List? or do you want to actually do something when you visit the list in sharePoint?
Avatar of JordanBlackler
JordanBlackler

ASKER

Sorry about that.
When they go to the sharepoint list to find there document, there is a link that brings them back to the the aspx page that they filled out.
ie: http://myserver/Testing/Testingaspx?ID=10
I use the ID field in sharepoint to get the current row of the document and then i load the data back into the aspx page. But i'm in the process of trying to figure out how to load the dynamic fields when they go back to the aspx page.
It looks like i figured it out.
ASKER CERTIFIED SOLUTION
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So what i did was:
i checked to see what columns in sharepoint contain data from the dynamic controls, i then rendered those controls again and populated it with the data.

Here is part of the code. This code only runs once when the page loads. It is only if the Add button was hit twice. There might be a nicer way to do it, but until i find a nicer way this will have to do.

                String strTB = listItem["Amount2"].ToString();
                String strTB2 = listItem["Amount3"].ToString();
                if (strTB != "" & strTB2 != "")
                {
                   //Creates the controls
                    btnAdd_Click(btnAdd, new System.EventArgs());
                   //Creates the controls
                    btnAdd_Click(btnAdd, new System.EventArgs());
                }

                for (int i = 0; i < PlaceHolder1.Controls.Count; i++)
                {
                  System.Web.UI.WebControls.TextBox TB = (TextBox)PlaceHolder1.Controls[i].FindControl("txt_Amount6");
                  System.Web.UI.WebControls.TextBox TB2 = (TextBox)PlaceHolder1.Controls[i].FindControl("txt_Amount16");
                    strTB = listItem["Amount2"].ToString();
                    strTB2 = listItem["Amount3"].ToString();
                    //Sets the values of the dynamic fields
                    TB.Text = strTB;
                    TB2.Text = strTB2;
                }
Hey, If it works!

It doesn't seem too bad.