11

Depend of number of results I got like 3 results and I want to assign each result to label text and put to new Div each new line. How I do that?

Collection<PSObject> output = pipeline.Invoke(); is where I stored results.

Here a sample C# codes,

foreach(PSObject psObject in output)
        {
            Label ipLabel = new Label();
            ipLabel.Text = psObject + "\r\n";
            div1.Controls.Add(ipLabel);
        }

Here a asp.net codes,

<div id="div1" runat="server"></div>

I got the right Label results but it all displayed in 1 div line instead of every div new line .

1
  • 1
    The Repeater control sounds like what you're looking for... Commented Nov 20, 2013 at 19:51

2 Answers 2

20

Ideally, markup manipulation should be done using data-binding within your .aspx file rather than in your codebehind, but anyway:

foreach(PSObject psObject in output) {
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.Controls.Add( new Label() { Text = psObject + "\r\n"; } );
    div1.Controls.Add( div );
}

This is how I would do it in the aspx file:

<div id="div1">
<% foreach(PSObject psObject in output) { %>
    <div>
    <%: psObject %>
    </div>
<% } //foreach %>
</div>

Note the use of <%: foo %> which is equivalent to <%= Server.HtmlEncode( foo ) %>.

Sign up to request clarification or add additional context in comments.

7 Comments

@StudentIT "labels" don't exist in rendered HTML, the id="" attribute is applied to some other markup. Please explain what you ultimately want to accomplish.
I wanted to add textBox ID in div.Controls.Add(new TextBox() { Text = "" }); empty textboxes during foreach loop because after user fill out name on each textbox, I want to be able to grab those specific textboxes to get values and pass back to server to update.
@StudentIT Using dynamically-created controls in ASP.NET WebForms is more complicated, you'll need to override the CreateChildControls method. The approach I put into my post is not suitable for adding input controls to a page, sorry.
It would appear that if you're trying to add an attribute like <div id="someid" runat="server" some-attribute="<%: foo %>"></div> you get some-attribute="<%: foo %>" in the HTML--only if you're placing it on another control. Why is that? Is there a way around that?
@codeMonkey You shouldn't ever get that: you'll get a compiler error (from the runtime compiler). Are you confusing .aspx with .cshtml?
|
2

The "Panel" control outputs a div, so maybe you'd like to wrap the Label there:

foreach(PSObject psObject in output)
{
    Panel ipPanel = new Panel();
    Label ipLabel = new Label();
    ipLabel.Text = psObject;
    ipPanel.Controls.Add(ipLabel);
    div1.Controls.Add(ipPanel);
}

You could also put a "
" (newline for HTML, roughly) inside the label:

foreach(PSObject psObject in output)
{
    Label ipLabel = new Label();
    ipLabel.Text = psObject + "<br/>";
    div1.Controls.Add(ipLabel);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.