0

I want to generate TextBoxes in my ASP.NET webpage. It works fine

    foreach (var Field in db.A_Settings)
    {
        TextBox t = new TextBox();
        t.ID = Field.ID.ToString();
        t.CssClass = "smallinput";
        t.Text = Field.Text;
        LabelPlaceHolder.Controls.Add(t);
    }

And it nicely generates something like this:

<input name="ctl00$ContentPlaceHolder1$1" type="text" value="ValueA" id="ContentPlaceHolder1_1" class="smallinput">
<input name="ctl00$ContentPlaceHolder1$2" type="text" value="ValueB" id="ContentPlaceHolder1_4" class="smallinput">
<input name="ctl00$ContentPlaceHolder1$3" type="text" value="ValueC" id="ContentPlaceHolder1_5" class="smallinput">

It is correct, but in fact I want to wrap it with some HTML, like

<p>
    <label>Label for the first TextBox obtained from database</label>
    <span class="field">
        <input name="ctl00$ContentPlaceHolder1$1" type="text" value="ValueA" id="ContentPlaceHolder1_1" class="smallinput">
    </span>
</p>

I couldn't found how to do it this way, so I was thinking about putting it into List<TextBox>, but I'm stuck here either (the same problem - no idea how to wrap the object with HTML).

Is there any way to do this?

For any posts like "Why don't you add those TextBoxes manually?" I'll send a photo of me hitting my head at keyboard, while there will be a dump of SQL with dozens of fields that needs to be handled displayed on the screen :)
Or a photo of a lemur. Lemurs are okay, too

4 Answers 4

1

Not the cleanest solution, but should work...

foreach (var Field in db.A_Settings)
{
    TextBox t = new TextBox();
    t.ID = Field.ID.ToString();
    t.CssClass = "smallinput";
    t.Text = Field.Text;
    //add literal control containing html that should appear before textbox
    LabelPlaceHolder.Controls.Add(new LiteralControl("html before"));
    LabelPlaceHolder.Controls.Add(t);
    //add literal control containing html that should appear after textbox
    LabelPlaceHolder.Controls.Add(new LiteralControl("html after"));
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would probably use a Repeater control:

    <asp:Repeater ID="SettingsRepeater" runat="server">
        <ItemTemplate>
            <p>
                <asp:Label ID="ItemLabel" runat="server"></asp:Label>
                <span class="field">
                    <asp:TextBox ID="ItemTextbox" runat="server"></asp:TextBox>
                </span>
            </p>
        </ItemTemplate>
    </asp:Repeater>

And bind the list to the repeater:

SettingsRepeater.DataSource = db.A_Settings;
SettingsRepeater.DataBind();

Then write your ItemDataBound code to set the existing values.

2 Comments

It would be problematic with gathering the fields' values and then saving them back into database - that is why my IDs are the same as from database. And values may be set by simply doing <asp:TextBox ID="ItemTextbox" runat="server" Text='<%#Eval("Text")'> Besides, Repeater is sometimes buggy and problematic to use. Thanks for the answer anyway!
You could create a hidden field in the repeater item that contains the field id, or give the textbox an attribute: ItemTextbox.Attributes["id"] = Field.ID; and read that back as you iterate the repeater items. But to each his own.
0

You want HtmlGenericControl controls.

foreach (var Field in db.A_Settings)
{
    TextBox t = new TextBox();
    t.ID = Field.ID.ToString();
    t.CssClass = "smallinput";
    t.Text = Field.Text;

    var label = new HtmlGenericControl("label");
    label.Controls.Add(new LiteralControl("LABEL TEXT"));

    var p = new HtmlGenericControl("p");
    p.Controls.Add(label);

    var span = new HtmlGenericControl("span");
    span.Attributes.Add("class", "field");
    span.Controls.Add(t);
    p.Controls.Add(span);

    LabelPlaceHolder.Controls.Add(p);
}

Comments

0

You can create custom ASP.NET Controls that can render any HTML you need. Have a look at this: Developing a Simple ASP.NET Server Control. This way you can create your a control called CustomTextBox which will render a Textbox inside a paragraph.

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.