0
votes

Hi I have a webform which contains both static and dynamic textboxes. I have written a code to create unlimited dynamic textboxes (maximum used would be 31 but i am giving user flexibility) but now I have to insert the values of dynamically created textboxes in database

Suppose I fill some text in 1 textbox it automatically creates a next textbox ie., as soon as i enter 1 character in the 1st textbox it generate new text box, finally when i click submit I want all this data from textboxes to store in database.

It should create new fields/columns for every new textbox generated, but if it is within the maximum limit then it should insert the data within the columns previously created. Any Solution?

Here is my code: WebForm1.aspx

<script type="text/javascript">

    getId = function ()
     {
        var id = 1;
        return function () 
        {
            id++;
        }
    }

    function CreateTextbox() 
    {
        var box = document.getElementById("divCreateTextbox");
        var curr = 'txt' + getId();
        var inp = document.createElement('input');
        inp.type = 'text';
        inp.name = 'textfield';
        inp.setAttribute("id", 'curr');
        inp.setAttribute("minlength", '1');
        box.appendChild(inp);
        inp.setAttribute('onkeyup', 'moveOnMin(this)');
        inp.setAttribute("textBoxAdded", "0");
        inp.focus();

    }

    function moveOnMin(s)
     {
         if (s.value.length == parseInt(s.getAttribute("minlength")) && s.getAttribute("textBoxAdded") == "0")
         {
            CreateTextbox();
            s.setAttribute("textBoxAdded", "1");
            s.focus();
        }
    }

        <div id="divCreateTextbox"></div>
        <script type="text/javascript">
        window.onload = function ()
       {
            CreateTextbox()
       }

WebForm1.aspx.cs(Code to insert static textboxes text into sql)

public partial class WebForm1 : System.Web.UI.Page
{

     SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {



        if (!IsPostBack)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(CS))
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Tbl Return's Tracker]", connection);
                DataSet ds = new DataSet();
                da.Fill(ds);

                GridView1.DataBind();
            }
            }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {

        SqlCommand command = new SqlCommand("INSERT INTO [Tbl Return's Tracker] ([Company],[Date],[Month],[OrderReference],[Product],[Status],[ReturnLabel],[NumberOfLenses],[Shelf], [ReshipmentNumber],[Reason1],[Reason2],[ActionTaken],[ReturnHandledby])VALUES (@company,@date,@month,@orderreferencenumber,@product,@status,@returnlabel,@numberoflenses,@shelf,@reshipmentordernumber,@Reason1,@Reason2,@action,@name)", connection);
        command.Parameters.AddWithValue("@company", DdlCompany.SelectedItem.Text);
        command.Parameters.AddWithValue("@month", LblMonth.Text);
        command.Parameters.AddWithValue("@date", LblDate.Text);
        command.Parameters.AddWithValue("@orderreferencenumber", TxtOrderReferenceNo.Text);
        command.Parameters.AddWithValue("@product", DdlProduct.SelectedItem.Text);
        command.Parameters.AddWithValue("@status", DdlStatus.SelectedItem.Text);
        command.Parameters.AddWithValue("@returnlabel", DdlReturnLabel.SelectedValue);
        command.Parameters.AddWithValue("@numberoflenses", TxtNumberOfLenses.Text);
        command.Parameters.AddWithValue("@shelf", DdlShelf.SelectedItem.Text);
        command.Parameters.AddWithValue("@reshipmentordernumber", TxtReshipmentOrderNo.Text);
        command.Parameters.AddWithValue("@Reason1", DdlReason1.SelectedItem.Text);
        command.Parameters.AddWithValue("@Reason2", DdlReason2.SelectedItem.Text);
        command.Parameters.AddWithValue("@action", DdlAction.SelectedItem.Text);
        command.Parameters.AddWithValue("@Name", DdlName.SelectedItem.Text);

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
        GridView1.DataBind();         

    }
1

1 Answers

0
votes

If I understand u correctly...

You need to clean up your code. Make sure you have unique ID set for the controls. Your getid does not return ID - debug and check it out. Make the var id global and correct this code in your CreateTextBox Func

var box = document.getElementById("divCreateTextbox");
var curr = 'txt' + id++; // getId();
var inp = document.createElement('input');
inp.type = 'text';
inp.name = curr;  //'textfield';
inp.setAttribute("id", curr);

In your postback just loop thru your controls

 For Each txtBx As String In Request.Form
        If txtBx.StartsWith("txt") Then 
 Response.Write(txtBx & "=" & Request.Form(txtBx) & "<BR>")
 end if
    Next

is this what you need?