set padding to dynamic textbox C# asp.net

人走茶凉 提交于 2019-12-31 05:29:13

问题


here is the code I create textbox from C# code..

for (int x = 0; x < 30; x++)
            {
                            TextBox txt = new TextBox();
                            txt.ID = "txt - " + x.ToString(); 
                            data.Controls.Add(txt);
                            data.Controls.Add(new LiteralControl("<br/>"));
             }

all textbox will be stick to each other.. I wonder can I add padding-top in the loop? how may I do it?

thanks for your help and your suggestion and comment is appreaciated.

This is create by using C#

I wish want got space like this.

please ignore the drop down box.. its just example.


回答1:


I hope you know something about CSS and stylesheets? You can render anything you want in the backend like in your example: textboxes. Using CSS you can add some style to it. This doesn't needs to be done during the creation of your controls.

In your example just create a stylesheet like default.css and add it into your page using:

<link rel="stylesheet" type="text/css" href="./css/default.css" />

Then using following code you can add some padding or even better some margin to your inputs:

input[type="text"] {
    margin-bottom: 10px;
}

Another solution is using classes:

ASP.NET

for (int x = 0; x < 30; x++)
{
     TextBox txt = new TextBox();
     txt.ID = "txt - " + x.ToString(); 
     txt.CssClass = "form-control"; //Assign a css class to your textbox
     data.Controls.Add(txt);
     data.Controls.Add(new LiteralControl("<br/>"));
}

CSS

.form-control {
    margin-bottom: 10px;
}


来源:https://stackoverflow.com/questions/30885074/set-padding-to-dynamic-textbox-c-sharp-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!