简体   繁体   中英

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.

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" .

divId.Controls.Add(txtRun);

The declaration of div in html would be like

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

There is no property Location in WebForms TextBox .

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.

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

<style>
   .txtContainer {
       width: 15%;
       float:left;
       padding:10px;
   }
</style>

And here is main container div

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

This is WinForm style for add any text box. You can create a jQuery code for add text boxes

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. 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:

 for (c = 0; c <= num; c++)
        {
            TextBox txtRun = new TextBox();
            txtRun.Attributes.Add("class", "btnClass");
            dvId.Controls.Add(txtRun);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM