[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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!!
0
JordanBlackler
Asked:
JordanBlackler
  • 3
  • 3
1 Solution
 
McExpCommented:
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?
0
 
JordanBlacklerAuthor Commented:
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.
0
 
JordanBlacklerAuthor Commented:
It looks like i figured it out.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
McExpCommented:
Does this mean you have a column in your list for every data field?

If so, see below for rough design: -

1) Load List into memory in asp page
2) Process items to find which columns contain data and therefore require a control to be rendered.
3) create controls using code similar to that you suggest above.
0
 
JordanBlacklerAuthor Commented:
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;
                }
0
 
McExpCommented:
Hey, If it works!

It doesn't seem too bad.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now