Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Comments
Sergey Alexandrovich Kryukov 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

SQL Script for Student Table

This is a sample table for storing the student information
SQL
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...
ASP.NET
<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...

HTML
<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

C#
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 :)
Thanks for your time...
-Mahesh
 
Share this answer
 
function GetDynamicTextBox(value){
return '<input name="DynamicTextBox" type="text" value="' + value + '" />' +
'<input type="button" value="Remove" onclick="RemoveTextBox(this)" />'
}
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('&lt;%=Values%&gt;');
if (values != null) {
var html = "";
for (var i = 0; i &lt; values.length; i++) {
html += "" + GetDynamicTextBox(values[i]) + "";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900