[英]I would like to know how to add location to dynamic textboxes in asp.net C# !
I want to specify location for dynamically occurring textbox however i am unable to do so as the "Location" property doesn't work saying I am missing some namespace. 我想为动态发生的文本框指定位置,但是我无法这样做,因为“ Location”属性不起作用,表示我缺少一些名称空间。
Here is my code: 这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
protected void freq_txtbox(object sender, EventArgs e)
{
int num = Convert.ToInt32(e_freq.Text);
int c = 0;
for (c = 0; c <= num; c++)
{
TextBox txtRun = new TextBox();
txtRun.Location= new Point(100,20*c);
this.Controls.Add(txtRun);
}
}
Instead of adding dynamically created TextBox control to webform using this you can make a container like asp.net Panel
or div
a server accessible control by assigning it id
and setting runat
property to "server" . 不用使用它向Webform添加动态创建的TextBox控件,您可以通过为其分配
id
并将runat
属性设置为“ server”来使诸如asp.net Panel
或div
这样的容器成为服务器可访问控件。
divId.Controls.Add(txtRun);
The declaration of div in html would be like html中div的声明就像
<div id="divId" runat="server"></div>
There is no property Location
in WebForms TextBox
. WebForms TextBox
没有属性Location
。
So what you can do is create a container div
for every textbox and set styling for each div
to adjust the position of divs. 因此,您可以做的是为每个文本框创建一个
container div
,并为每个div
设置样式以调整div
的位置。
for (c = 0; c <= num; c++)
{
TextBox txtRun = new TextBox();
HtmlGenericControl div = new HtmlGenericControl("div");
div.Controls.Add(txtRun);
div.Attributes.Add("class", "txtContainer");
divContainer.Controls.Add(div);
}
And here is styling for container div surrounding textboxes 这是容器div周围文本框的样式
<style>
.txtContainer {
width: 15%;
float:left;
padding:10px;
}
</style>
And here is main container div 这是主容器div
<div runat="server" id="divContainer">
</div>
This is WinForm style for add any text box. 这是WinForm样式,用于添加任何文本框。 You can create a jQuery code for add text boxes
您可以创建用于添加文本框的jQuery代码
enter code here 在这里输入代码
function AddNewTextBoxes(noOfTextBoxNeeded){
var varHtmlTextbox="";
for(var i=0;i<noOfTextBoxNeeded;i++){
varHtmlTextbox = varHtmlTextbox + "<input type='text' /> <br/>";
}
$("#DivToAppenTextboxes").append(varHtmlTextbox);
}
Hope this will help you. 希望这会帮助你。
You have to Take a div where you can add your controls in it. 您必须在div中添加控件。 suppose
假设
Client side 客户端
<style>
.btnClass{
//your css is going to implement here whatever you want.
}
</style>
<div id="dvId" runat="server"></div>
What will be the server side code, here we need to apply css to button which is going to add in our Div controller: 服务器端代码是什么,在这里我们需要将CSS应用于按钮,该按钮将添加到Div控制器中:
for (c = 0; c <= num; c++)
{
TextBox txtRun = new TextBox();
txtRun.Attributes.Add("class", "btnClass");
dvId.Controls.Add(txtRun);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.