[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with PlaceHolders and Aligning Controls

Posted on 2007-10-16
8
Medium Priority
?
291 Views
Last Modified: 2010-04-15
Hello all -
This is for the Web using C#

Yesterday i posted about creating controls dynamically and building tables. I decided to go a different direction.

I'm now using PlaceHolders.  I'm creating 7 controls on a click of a button and everytime the user clicks the Add button another 7 controls are created. it consists of 3 labels. 3 textboxes and 1 button.

How it works right now, all the controls are created in one PlaceHolder and it looks fine when they hit the Add button once. But when the button is hit for the second time it shows 12 controls on one row and 2 controls on another row.

I would like it to show 7 controls per row, this way it is all aligned.
If that isn't possible, is there a way to add the controls to a new PlaceHolder everytime the Add button is clicked.  
Also, when i click the Add button and fill out the three fields, then click the Add button again, the first field is blank but the other two fields still have the text i entered.

Thanks a lot in advance. My code is below.

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

protected void btnAdd_Click(object sender, EventArgs e)
{
        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 * 7)
        {
            // -----INVOICE-----
           //Control One
            Label lblInvoice = new Label();
            PlaceHolder1.Controls.Add(lblInvoice);
            lblInvoice.ID = "lblInvoice" + PlaceHolder1.Controls.Count.ToString();
            lblInvoice.Text = "Invoice #:";

            //Control Two
            TextBox txtInvoice = new TextBox();
            PlaceHolder1.Controls.Add(txtInvoice);
            txtInvoice.ID = "txtInvoice:" + PlaceHolder1.Controls.Count.ToString();
            txtInvoice.Text = "";
            txtInvoice.Height = 18;

            // -----AMOUNT-----
            //Control Three
            Label lblAmount = new Label();
            PlaceHolder1.Controls.Add(lblAmount);
            lblAmount.ID = "lblAmount" + PlaceHolder1.Controls.Count.ToString();
            lblAmount.Text = "Amount:";

            //Control Four
            TextBox txtAmount = new TextBox();
            PlaceHolder1.Controls.Add(txtAmount);
            txtAmount.ID = "txtAmount" + PlaceHolder1.Controls.Count.ToString();
            txtAmount.Text = "";
            txtAmount.Height = 18;

            // -----DATE-----
            //Control Five
            Label lblDateRecieved = new Label();
            PlaceHolder1.Controls.Add(lblDateRecieved);
            lblDateRecieved.ID = "lblDateRecieved" + PlaceHolder1.Controls.Count.ToString();
            lblDateRecieved.Text = "Date:";

            //Control SIx
            TextBox txtDateRecieved = new TextBox();
            PlaceHolder1.Controls.Add(txtDateRecieved);
            txtDateRecieved.ID = "txtDateRecieved" + PlaceHolder1.Controls.Count.ToString();
            txtDateRecieved.Text = "";
            txtDateRecieved.Height = 18;

            //Control Seven
            Button btnCalendar = new Button();
            PlaceHolder1.Controls.Add(btnCalendar);
            btnCalendar.ID = "btnCalendar" + PlaceHolder1.Controls.Count.ToString();
            btnCalendar.BackColor = System.Drawing.Color.White;
            btnCalendar.CssClass = "calendarImage";
            btnCalendar.Width = 27;
        }
       
    }

0
Comment
Question by:JordanBlackler
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 20092334
if you add literals to the placeholder containing table rows, you can align is just fine:

Literal tableInfo = new Literal();
tableInfo.Text = "<table>";

PlaceHolderControl.Controls.Add(tableInfo);

//Loop starts here to print controls
tableInfo = new Literal();
tableInfo.Text = "<tr><td>";

PlaceHolderControl.Controls.Add(tableInfo);
PlaceHolderControl.Controls.Add(MyFirstTextBox);

tableInfo = new Literal();
tableInfo.Text = "</td><td>";

//repeat as above and end loop.
0
 

Author Comment

by:JordanBlackler
ID: 20093057
Thanks for the response.
Do you think you can plug it into my code above.
When i tried i got this error:

"Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request"
0
 
LVL 28

Expert Comment

by:strickdd
ID: 20093127
why don't you post the code that you have with my code in it and i'll tell you what to change. That way I can see all your variables and field declarations in the proper context.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JordanBlackler
ID: 20093280
Am i supposed to add this section for every control i'm creating? I'm a little bit lost i think.

//Loop starts here to print controls
tableInfo = new Literal();
tableInfo.Text = "<tr><td>";

PlaceHolderControl.Controls.Add(tableInfo);
PlaceHolderControl.Controls.Add(MyFirstTextBox);

tableInfo = new Literal();
tableInfo.Text = "</td><td>";
0
 
LVL 28

Accepted Solution

by:
strickdd earned 2000 total points
ID: 20093376
i guess i wasn't clear enough in my comment "//Loop starts here to print controls"

What i meant is you put your code for the For Loop, While Loop, or whatever you are doing to loop and create controls:

for(int i=0; i<numControlsToCreate; i++)
{
      tableInfo = new Literal();
tableInfo.Text = "<tr><td>";

PlaceHolderControl.Controls.Add(tableInfo);
PlaceHolderControl.Controls.Add(MyFirstTextBox);

tableInfo = new Literal();
tableInfo.Text = "</td><td>";
}

NOTE: you will have to "new" all the controls you are adding and give distinct IDs to them each loop. If you do not then you will have problems referencing controls. You also want to come up with a naming convention for the controls to reference them easily. For exmple,

textBoxControl.ID = "firstTextBox" + i;

To reference it:
PlaceHolderControl.FindControl("firstTextBox" + i);
0
 

Author Comment

by:JordanBlackler
ID: 20093720
I get an error when i click the Add button the second time.
"Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request"

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

protected void btnAdd_Click(object sender, EventArgs e)
{
        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"];
        }
       
        Literal tableInfo = new Literal(); //////// your code
        tableInfo.Text = "<table>";   ////////// your code

        PlaceHolder1.Controls.Add(tableInfo);

        while (PlaceHolder1.Controls.Count < count * 7)
           
        {
            tableInfo = new Literal();////////// your code
            tableInfo.Text = "<tr><td>";////////// your code

            PlaceHolder1.Controls.Add(tableInfo);
             // -----INVOICE-----
            Label lblInvoice = new Label();
            PlaceHolder1.Controls.Add(lblInvoice);
            lblInvoice.ID = "lblInvoice" + PlaceHolder1.Controls.Count.ToString();
            lblInvoice.Text = "Invoice #:";

            TextBox txtInvoice = new TextBox();
            PlaceHolder1.Controls.Add(txtInvoice);
            txtInvoice.ID = "txtInvoice:" + PlaceHolder1.Controls.Count.ToString();
            txtInvoice.Text = "";
            txtInvoice.Height = 18;
            txtInvoice.Width = 100;
            // -----AMOUNT-----
            Label lblAmount = new Label();
            PlaceHolder1.Controls.Add(lblAmount);
            lblAmount.ID = "lblAmount" + PlaceHolder1.Controls.Count.ToString();
            lblAmount.Text = "Amount:";

            TextBox txtAmount = new TextBox();
            PlaceHolder1.Controls.Add(txtAmount);
            txtAmount.ID = "txtAmount" + PlaceHolder1.Controls.Count.ToString();
            txtAmount.Text = "";
            txtAmount.Height = 18;
            txtAmount.Width = 100;
            // -----DATE-----
            Label lblDateRecieved = new Label();
            PlaceHolder1.Controls.Add(lblDateRecieved);
            lblDateRecieved.ID = "lblDateRecieved" + PlaceHolder1.Controls.Count.ToString();
            lblDateRecieved.Text = "Date:";

            TextBox txtDateRecieved = new TextBox();
            PlaceHolder1.Controls.Add(txtDateRecieved);
            txtDateRecieved.ID = "txtDateRecieved" + PlaceHolder1.Controls.Count.ToString();
            txtDateRecieved.Text = "";
            txtDateRecieved.Height = 18;
            txtDateRecieved.Width = 100;

            Button btnCalendar = new Button();
            PlaceHolder1.Controls.Add(btnCalendar);
            btnCalendar.ID = "btnCalendar" + PlaceHolder1.Controls.Count.ToString();
            btnCalendar.BackColor = System.Drawing.Color.White;
            btnCalendar.CssClass = "calendarImage";
            btnCalendar.Width = 27;
           
            tableInfo = new Literal();////////// your code
            tableInfo.Text = "</td></tr>";////////// your code
     }
}
0
 

Author Comment

by:JordanBlackler
ID: 20094059
I added the below section of the code inside the loop rather then outside the loop and it works now.
           Literal tableInfo = new Literal();
            tableInfo.Text = "<table>";
            PlaceHolder1.Controls.Add(tableInfo);

The only problem now is that the first textbox of each row doesn't hold the text when i click the Add button. The two other textboxes keep the data.
Any ideas?

Thanks for your help.

0
 

Author Comment

by:JordanBlackler
ID: 20095205
Nevermind, i got it to work. I had to add the last section of the code to the placeholder
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question