ASP.NET - Generating Dynamic Textbox - Asked By Rajiv Sharma on 23-Nov-11 04:05 AM

hi,

I need to create text boxes within grid view dynamically which are suppose to be get generated once the user have enter some value within the text box suppose the user enters 4 then 4 rows is to be get generates with the combination of check boxes and text boxes.

Any idea most appreciated.

Thanks & Regards
Rajiv
s j replied to Rajiv Sharma on 23-Nov-11 04:08 AM


hi,

Hope this is useful site for u.
http://geekswithblogs.net/dotNETvinz/archive/2009/03/17/dynamically-adding-textbox-control-to-aspnet-table.aspx


please tell me if u have any doubts regarding this.


Cheers,
Samantha.
Suchit shah replied to Rajiv Sharma on 23-Nov-11 04:09 AM
You can do it like below exampl

<asp:button id="Button1" runat="server" text="Press Me" 
      onclick="Button1_Click" />
<asp:panel id="Panel1" runat="server" style="margin-top: 20px;">
</asp:panel>

Code behind

private int Counter
{
     
get
     
{
         
if (this.ViewState["Counter"] == null)
         
{
             
this.ViewState["Counter"] = 1;
         
}

         
return Convert.ToInt32(this.ViewState["Counter"]);
     
}
     
set { this.ViewState["Counter"] = value; }
}

protected void Button1_Click(object sender, EventArgs e)
{
     
for (int i = 1; i <= this.Counter; i++)
     
{
         
Panel1.Controls.Add(new TextBox());
         
Panel1.Controls.Add(new LiteralControl("&nbsp;"));
         
Panel1.Controls.Add(new DropDownList());
         
Panel1.Controls.Add(new LiteralControl("&nbsp;"));
         
Panel1.Controls.Add(new Button { Text = "Submit" });
         
Panel1.Controls.Add(new LiteralControl("&lt;br />"));
     
}

     
this.Counter++;
}
Reena Jain replied to Rajiv Sharma on 23-Nov-11 04:14 AM
Hi,

check this code

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
 {
   int count = Convert.ToInt32(Textbox1.Text);
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
   for (int i = 0; i< e.Row.Cells.Count; i++)
   {
     TextBox txt= new TextBox();  
      
     e.Row.Cells[i].Controls.Add(txt);
     count++;
   }
   }   
 }

Like same you can add check box

for more help check this
www.eggheadcafe.com/community/asp-net/17/10338247/how-to-add-new-row-with-textbox-conatrol-by-clicking-on-the-button.aspx
Jitendra Faye replied to Rajiv Sharma on 23-Nov-11 04:18 AM
Try this sample code-


 protected void Page_Load(object sender, EventArgs e)
       
{
           
TemplateColumn tc = new TemplateColumn();
           
            dg
.AutoGenerateColumns = false;
            tc
.ItemTemplate = new CreateItemTemplateTextBox1("testb");
           
this.dg.Columns.Add(tc);
           
            bind
();
           

       
}


       
private void bind()
       
{
           
DataTable table = new DataTable();
            table
.Columns.Add("test");
           
DataRow dr = table.NewRow();
            dr
["test"] = "aa";
            table
.Rows.Add(dr);
           
this.dg.DataSource = table;
            dg
.DataBind();
       
}

       
protected void Button1_Click(object sender, EventArgs e)
       
{
           
TextBox box = dg.Items[0].FindControl("testb") as TextBox;
           
string val = box.Text;
       
}

 

 

    public class CreateItemTemplateTextBox1 : ITemplate
   
{
       
string strColumnText;
       
string strTextBoxName;
       
bool TextMode;
       
int MaxLength = 0;
       
bool ReadOnly = false;
       
public CreateItemTemplateTextBox1( string TextBoxName)
       
{
           
           
this.strTextBoxName = TextBoxName;
           
       
}
       
       
public void InstantiateIn(Control objContainer)
       
{
           
TextBox txt = new TextBox();
            txt
.ID = strTextBoxName;
           
            objContainer
.Controls.Add(txt);
       
}
       
   
}
 
Try this and let me know.