Styling and Div Container management with Bootstrap in ASP.NET Code Behind

I am currently in the process of developing an ASP.NET application that requires the dynamic creation of "TextBox" and "DropdownList" controls. Here is the method I have been using to create "TextBox" controls:

private void CreateTextBox(string id, string labelName)
{
     Label lbl = new Label();
     lbl.Text = labelName;
     pnlDynamicControls.Controls.Add(lbl);

     TextBox txt = new TextBox();
     txt.ID = id;
     pnlDynamicControls.Controls.Add(txt);
}

As part of my development, I am utilizing the bootstrap theme for my web pages. Below is an example of a styled "Textbox":

<div class="form-group">
<label class="col-sm-2 control-label">Document Base:</label>
<div class="col-sm-4">
    <asp:TextBox ID="txtName" runat="server" CssClass="form-control"></asp:TextBox >
</div>

In order to maintain consistency, I would like this style to be applied to the dynamically created "TextBox" controls along with the surrounding "div" tag. Can anyone assist me in achieving this?

The technologies I am working with include ASP.NET, C#, Bootstrap 3, and Visual Studio 2017.

Update: The following image illustrates how the control looks once the CssClass has been added: https://i.sstatic.net/dOT1U.png

I am aiming for it to resemble the following design: https://i.sstatic.net/VK6SM.png

Answer №1

Make sure you are utilizing the CssClass property.

private void GenerateInputField(string id, string title)
{
     Label lbl = new Label();
     lbl.CssClass = "col-sm-2 control-label";
     lbl.Text = title;
     pnlDynamicControls.Controls.Add(lbl);

     TextBox txt = new TextBox();
     txt.CssClass = "form-control";
     txt.ID = id;
     pnlDynamicControls.Controls.Add(txt); 
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conceal a division using CSS based on its data-role attribute

After extensive research, I have yet to find a solution. Let's say there is a div structured like this: div[data-role="form-footer"] What would be the most effective method for hiding it, and can it be done using CSS? I've tried the f ...

When it comes to carousel dynamic items, I encounter an issue when using the splice function to remove an item

When using the splice function to delete an item (image), all the images are being deleted. When I delete an item, I want the carousel to show the remaining images instead of disappearing. <div id="carouselExampleControls" class="carousel slide" data ...

I have noticed that the baseline of a Span element has shifted after updating my Chrome browser to a version that begins with

Once I updated to chrome Version 108.0.5359.94 (Official Build) (64-bit) from 107.0.5304.87 (Official Build) (64-bit), the behavior of the span element changed drastically. It shifted its baseline when multiple spans were stacked on top of each other. Exp ...

Can you answer this straightforward query about CSS positioning?

I am currently facing a challenge in creating a small div (header class) that overlays a main banner image div (banner class). When I maximize my browser window, the positioning is correct. However, upon resizing the browser, the header div maintains its m ...

Enhance Your Cards with Hover Zoom Text

I'm looking to place some text in front of a card hover zoom effect, but currently it appears behind the zoomed image upon hover. <div style="margin-top: 50px;" class="container imgzoom"> <div class="row ...

Tips for optimizing command execution speed while using selenium?

Is there a way to adjust the speed at which the mouse moves in selenium? I came across the DefaultSelenium class, but it's not static and I'm unsure how to use it to set delays in specific webdrivers. Could someone please provide a helpful exam ...

do not display bullet points

The bullets from the list-style type are not appearing. Despite the CSS code never declaring list-style-type: none, I have specifically declared list-style-type: disc for the section in question. I even checked with Firebug and other tools, and it is evide ...

Conceal a div element only if it is nested within a particular div class

I have a question about hiding a specific div within another div. Here is the scenario: <div class="xp-row xp-first-row"> <div class="social-buttons"> Some Content Here </div> </div> The goal is to hi ...

Effortless floating made possible by the magic of Twitter Bootstrap

I am currently working with a group of elements that are designated as row-fluid and span12, each containing span4 elements. My goal is to have these elements automatically align in columns of 3, regardless of how many are present. However, I am facing an ...

CSS - selecting elements that are greater than a specific criteria N

I have multiple <p> tags in the body of my HTML. I want to display only the first two paragraphs and hide all the paragraphs that come after. However, the code I'm using doesn't seem to work as expected. <html> <head> < ...

Is there a way to prevent the Material UI Chip component from appearing when there is no content present?

I have developed an application that pulls content from an API and displays it on the screen. Within this app, I have implemented a Material UI Card component to showcase the retrieved information, along with a MUI Chip component nested within the card. &l ...

My live website is plagued by an annoying horizontal scroll bar that doesn't appear in the local host version

I am struggling to remove the unwanted horizontal scroll bar on my live website, although it is not visible on my local host... <-- this is my website, built using Vite + ReactJs. I attempted to use overflow: hidden; and max-width: 100% on the body t ...

Tips for ensuring that a file name is properly quoted when it contains spaces

How can I ensure that the Process.startInfo.Arguments are correct when one of the arguments is a filename, such as "test test"? In this case, the StartInfo.Arguments would be set to filename. ...

Issue encountered with Tailwind Color Classes not functioning correctly when triggered by a button click within NextJS and React Vite framework

Recently, I've developed a component showcasing the "Text Gradient" effect in Tailwind + React. The component comprises of 4 select dropdowns and a "randomize" button. Upon clicking the button or selecting an option from the dropdowns, the changes are ...

JavaScript may not display height as anticipated

Currently, I am experimenting with JavaScript in order to achieve a website section that is 100% the height of the screen minus the "nav" bar. Imagine it as having two visible components - the first one being large and the second one small at the bottom. ...

In Angular 4 applications, all vendor CSS files are converted into style tags at the beginning of the HTML document

In my Angular 4 project, I have integrated Bootstrap, Fontawesome, and some custom CSS. However, all these styles are rendering as separate tags at the top of my index.html file. I would like them to be combined into a single bundled CSS file for better ...

Obtaining values from non-ASP controls within an ASP.NET project

Could someone please provide guidance on obtaining values from non-ASP controls in my C# code? Any help would be greatly appreciated. Thank you! ...

Invisible Thinglink image revealed upon resizing browser

I am currently repurposing a pre-existing theme that has a drop-down menu showcasing an image. I am attempting to integrate a Thinglink into the content section of this drop-down, but unfortunately, the image does not appear until I adjust the size of the ...

Reverting back to the specified CSS style

In my application, I have a common HTML component styled as follows: .box { min-width: 100px; padding: 20px 10px; } There are over 100 of these components and they each have a unique border style without a bottom border: .box:nth-child(1) { border ...

What is the method to display just the final 3 characters within a paragraph?

Is there a way to display only the last three characters of a string using either a method or a string method? I consistently have a 13-digit number, but I specifically require showing only the final three digits. Appreciate any assistance provided. Than ...