.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

adding textboxes dynamically

Posted By: Ravindra     Posted Date: March 06, 2010    Points:2   Category :ASP.Net
I need to add textboxes dynamically to the website based on the requirements,
i.e if i hav some 10 fields in database i need to load them dynamically in 10 textboxes at runtime

Responses
Author: Lalij Mer             
Posted Date: March 07, 2010     Points: 5   

Hi,
Use this code sample

    protected void Page_Load(object sender, EventArgs e)
{
//....Creating Dynamic TextBoxes
TextBox txtName = new TextBox();
txtName.ID = "txtName";
Page.Form.Controls.Add(txtName);

TextBox txtAddress = new TextBox();
txtAddress.ID = "txtAddress";
Page.Form.Controls.Add(txtAddress);

TextBox txtPhone = new TextBox();
txtPhone.ID = "txtPhone";
Page.Form.Controls.Add(txtPhone);

TextBox txtMobile = new TextBox();
txtMobile.ID = "txtMobile";
Page.Form.Controls.Add(txtMobile);

Button Button1 = new Button();
Button1.ID = "btnShow";
Button1.Text = "Show";
Button1.Click += new EventHandler(Button1_Click);
Page.Form.Controls.Add(Button1);
}

private void Button1_Click(object sender, EventArgs e)
{
//...Retriving Values from TextBoxes... here proper error handling should be done...
Response.Write(((TextBox)Page.Form.FindControl("txtName")).Text.Trim());
Response.Write("<br />");
Response.Write(((TextBox)Page.Form.FindControl("txtAddress")).Text.Trim());
Response.Write("<br />");
Response.Write(((TextBox)Page.Form.FindControl("txtPhone")).Text.Trim());
Response.Write("<br />");
Response.Write(((TextBox)Page.Form.FindControl("txtMobile")).Text.Trim());
}


Create text box dynamically Runtime
http://aspdotnetcodebook.blogspot.com/2008/03/how-to-create-textbox-control.html

More example Here will help

http://geekswithblogs.net/dotNETvinz/archive/2009/03/17/dynamically-adding-textbox-control-to-aspnet-table.aspx

Please Check my answer if you help....
Thank You...

Lalit.
Author: abhay             
Posted Date: March 08, 2010     Points: 5   

Create table for proper design then at run time

Suppose you have a datatable having 10 columns let's the dattble name is dt.

for(int count=0;count<dt.Columns.Count;++count)
{
Textbox txt = new Textbox();
txt.ID = count.Tostring();
td.Controls.Add(txt );td is the tablecell of the table and u r appending controls to the td
}

Author: Ashok Nalam             
Posted Date: March 10, 2010     Points: 5   

Using XML and XSLT concept You can generate the web pages on fly.
Just try out the concept xml with xslt.
Generate xml file from your database data(means no of fields).
Define one xslt file.
With parsing of xslt to xml you can generate aspx page

Author: TotalDotNet             
Posted Date: August 16, 2010     Points: 5   

The following code will definitely help u. Also see the demo
http://www.totaldotnet.com/Data%20%20Controls/Property_DataControl_Demo/Prop_GridViewRowProperties.aspx


Here, we dynamically added single textbox control




.cs file

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gridBind();
}

private void gridBind()
{
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DotNetLearnningConnectionString"].ConnectionString))
{
using (SqlCommand sqcmd = new SqlCommand())
{
sqcmd.Connection = conn;
sqcmd.CommandText = "Select * from [City]";
sqcmd.CommandType = CommandType.Text;
using (SqlDataAdapter ad = new SqlDataAdapter(sqcmd))
{
using (DataSet das = new DataSet())
{
ad.Fill(das);
GridView1.DataSource = das;
GridView1.DataBind();
}
}
}
}

}

catch
{
throw;
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DropDownList ddl1 = (DropDownList)e.Row.FindControl("DropDownList2");

using (DotNetLearnClassesDataContext dc = new DotNetLearnClassesDataContext())
{
var countries = from sa in dc.Countries
select new
{
sa.Country_Id,
sa.CountryName
};

var continents = from sa in dc.Continents
select new
{
sa.Continent_Id,
sa.Continent_Name

};
ddl.DataSource = countries;
ddl.DataTextField = "Country_Id";
ddl.DataValueField = "Country_Id";
ddl.DataBind();

ddl1.DataSource = continents;
ddl1.DataTextField = "Continent_Id";
ddl1.DataValueField = "Continent_Id";
ddl1.DataBind();
}
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr=GridView1.Rows[GridView1.EditIndex];
DropDownList dd1 = (DropDownList)gvr.Cells[2].FindControl("DropDownList1");
DropDownList dd2 = (DropDownList)gvr.Cells[3].FindControl("DropDownList2");
using (DotNetLearnClassesDataContext dc = new DotNetLearnClassesDataContext())
{
TextBox id = (TextBox)gvr.Cells[0].Controls[0];
var data = dc.Cities.Single(m => m.City_Id == Convert.ToInt32(id.Text));
data.Continent_Id = Convert.ToInt32(dd2.SelectedValue);
data.Country_Id = Convert.ToInt32(dd1.SelectedValue);
dc.SubmitChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dc.Cities;
GridView1.DataBind();
}
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}


aspx

<form id="form1" runat="server">
<div>
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating"

onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="City_Id" />
<asp:BoundField DataField="City_Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Country_Id") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Continent">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Continent_Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button2" runat="server" Text="Update" CommandName="Update" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
</div>
</form>




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend