• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

load control dynamically in the table

Hi,

I have table that looks:
Label   textbox
Label   textbox

I want to load label and textbox dynamically.
I put 2 PlaceHolders :
<TR>
      <TD>
            <asp:PlaceHolder id="PlaceHolder_Labels" runat="server"></asp:PlaceHolder></TD>
      
      <TD>
            <asp:PlaceHolder id="PlaceHolder_Values" runat="server"></asp:PlaceHolder></TD>
</TR>


  but when I add to PlaceHolder.Controls it looks:
Label1Label2 textbox1textbox2

and I need:
Label1 textbox1
Label2 textbox2

How to achive that?
0
netformx
Asked:
netformx
  • 2
  • 2
1 Solution
 
orbulatCommented:
u may try this, add Literal control to fix the alignment

Literal lit = new Literal();
lit.Text = "<br>";

PlaceHolder_Labels.Controls.Add(Label1);
PlaceHolder_Labels.Controls.Add(lit);
PlaceHolder_Labels.Controls.Add(Label2);

PlaceHolder_Values.Controls.Add(textbox1);
PlaceHolder_Values.Controls.Add(lit);
PlaceHolder_Values.Controls.Add(textbox2);
0
 
GavinMannionCommented:
Try adding a label between the two textboxes where the label has a value of <BR>.

Else a better way would be to create a small table and add the textboxes to the table rows and add the table to the placeholder.
0
 
netformxAuthor Commented:
with <BR> it does not look well (since text box is of 4 rows and label should be in the middle of it - when it's in table it looks OK.

can you write the simple example how to create a small table + then iterate on labels and values and fill values,
I can think of the follwoing:
add :
<TR>
<TD>
LABEL
</TD>
to PlaceHolder_Labels.Controls

and add
<TD>
textbox
</TD>
</TR>
to PlaceHolder_TextBoxes.Controls

iteration:
for(int i=0; i<PlaceHolder_Labels.Controls.Count; i++)
{
if (PlaceHolder_Labels.Controls[i] is Label)
   myTextBox = PlaceHolder_Vals.Controls[i-1] //i-1 becasue of table
 
}


but it's seems very clumsy to me, maybe you can suggest more elegant way?
0
 
GavinMannionCommented:
Just relooking at your original code, I see no reason to have two Placeholders, create one place holder and add the newly created table to that placeholder.

Basically

HtmlTable table = new HtmlTable();
        for( int i = 0; i < 5; IAppDomainSetup++)
        {
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell tc = new HtmlTableCell();
            Label l = new Label();
            l.Text = "BLAH";
            tc.Controls.Add(l);
            tr.Cells.Add(tc);
            tc = new HtmlTableCell();
            TextBox t = new TextBox();
            tc.Controls.Add(t);
            tr.Cells.Add(tc);
            table.Rows.Add(tr);
        }

This would create 5 rows with a label in the left and a textbox in the right....

That is what you require I think.
0
 
netformxAuthor Commented:
thanks, it worked great!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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