Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Dir Sir...
I am trying to create multiple textbox at run time using web method.
And I want to save all data into mssql database.And textbox create after click button. So plz help me out.
Posted 26-Nov-12 20:23pm
Comments
Sergey Alexandrovich Kryukov at 27-Nov-12 1:26am
   
ASP.NET? Tag it. Anything else? Tag or specify properly.
This is not a question. What have you done so far, what's the problem?
 
(Do you mean "TextBox"? Which one? System.Web.UI.WebControls.TextBox? Somethine else? :-)
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

SQL Script for Student Table
 
This is a sample table for storing the student information
USE [DB Name]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Stud_Name] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Age] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Addr] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Gender] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Qualification] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
 
GO
SET ANSI_PADDING OFF
 

 
.Aspx code Here just a DropDownList for firing a SelectedIndexChanged event as you said you want to create textboxes on button click am using this event...
<div>
       Select Id: <asp:dropdownlist id="DropDownList1" runat="server" autopostback="True" appenddatabounditems="true" xmlns:asp="#unknown">
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:dropdownlist>
</div>
 

web.confog Code this is for connecting sql fill your information...
 
 <connectionstrings>
    <add name="ConString" connectionstring="Data Source=[SQL serverName];Initial Catalog=[DB name];Persist Security Info=True;User ID=sa;Password=[*****]" providername="System.Data.SqlClient" />
  </connectionstrings>
 

--aspx.cs code
here on selected changed event name is displaying in a single char text box which is dynamically creating so please check it out
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
 
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
 
        if (!IsPostBack)
        {
            ddl();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Select Stud_Name from student where Id='" + DropDownList1.SelectedItem + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            string s = ds.Tables[0].Rows[0][0].ToString();
            for (int i = 0; i < s.Length; i++)
            {
                string id = "txt" + i.ToString();
                TextBox txt = new TextBox();
                txt.ID = id;
                txt.Text = s[i].ToString();
                txt.Width = 20;
                txt.MaxLength = 1;
                txt.ReadOnly = true;
                form1.Controls.Add(txt);
            }
        }
        catch
        {
        }
    }
    private void ddl()
    {
        try
        {
            cmd = new SqlCommand("Select Id from student ", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ds.Clear();
            DropDownList1.Items.Add("Select...");
            da.Fill(ds);
 
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "Id";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataBind();
        }
        catch
        {
        }
    }
}
 

 
I hope it will help you Smile | :)
Thanks for your time...
-Mahesh
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<script type="text/javascript">
function GetDynamicTextBox(value){
return '<input name="DynamicTextBox" type="text" value="' + value + '" />' +
'<input type="button" value="Remove" />'
}
function AddTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}

function RemoveTextBox(div) {
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}

function RecreateDynamicTextboxes() {
var values = eval('<%=Values%>');
if (values != null) {
var html = "";
for (var i = 0; i < values.length; i++) {
html += "" + GetDynamicTextBox(values[i]) + "";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
</script>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100