add controls at runtime and save data to database

Last post 10-29-2009 10:24 PM by vinz. 5 replies.

Sort Posts:

  • add controls at runtime and save data to database

    10-29-2009, 2:36 AM
    • Member
      17 point Member
    • suri_vinod
    • Member since 02-01-2008, 2:09 PM
    • Posts 50

    Hello,

    What i am trying to do is to add controls, say a textbox, at runtime.

    Which i can do with javascript. But i my main problem is i am not able to get how to store data entered in that texbox by user to

    database.


    Vinod.


  • Re: add controls at runtime and save data to database

    10-29-2009, 5:24 AM
    Answer
    • All-Star
      91,758 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    You may try to consider the server side way of generating controls instead..

    Dynamically Adding TextBox Control to ASPNET Table

    FAQ: Dynamically Adding Rows in ASP Table on Button Click event

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: add controls at runtime and save data to database

    10-29-2009, 10:00 AM
    • Member
      17 point Member
    • suri_vinod
    • Member since 02-01-2008, 2:09 PM
    • Posts 50

    This is a good way of doing this.

    But my problem is i dont want to ask user about number of rows or columns.

    User should be able to click some link, say more, and add as many textboxes they want.


    Vinod

  • Re: add controls at runtime and save data to database

    10-29-2009, 11:13 AM
    Answer
    • All-Star
      26,406 point All-Star
    • PeteNet
    • Member since 01-21-2009, 6:15 PM
    • Posts 3,766

    suri_vinod:

    But my problem is i dont want to ask user about number of rows or columns.

    User should be able to click some link, say more, and add as many textboxes they want.

    which should be very simple too. use a LinkButton with Text, say, "More" or "AddRows", something like:

    <asp:LinkButton ID="LinkButton1" runat="server" Text="AddRows" OnClick="LinkButton1_OnClick"/>

    and in the OnClick event handler, write code similar to what's in the Button1_Click, the example Vinz has given you.

    except that you won't be parsing the values of the number of rows and column from the textboxes but will use some predefined or hard-coded values.

    you can use the same variables, numOfColumns and numOfRows and set up their values in the page load event...so you won't need these lines that I've crossed out:

    protected void LinkButton1_OnClick(object sender, EventArgs e)
            {

    //Check if the inputs are numbers

            if (int.TryParse(TextBox1.Text.Trim(), out numOfColumns) && int.TryParse(TextBox2.Text.Trim(), out numOfRows))

            {

                //Generate the Table based from the inputs

                GenerateTable(numOfColumns, numOfRows);

    //Store the current Rows and Columns In ViewState as a reference value when it post backs

                ViewState["cols"] = numOfColumns;

                ViewState["rows"] = numOfRows;

            }

            else

            {

                Response.Write("Values are not numeric!");

            }

    }


    also, it would depend on you to set up the numOfRows value...maybe = 1 as you want the users to add rows every time they click.....that is up to you.

    Regards,
    Peter
  • Re: add controls at runtime and save data to database

    10-29-2009, 11:45 AM

    when you add the textboxes with javascript, specify a name and id attributes:

      $('form').attach('<input type="text" name="textbox1" id="textbox1" />');

    then server side:

      var textbox1Value = Request.Form["textbox1"];


    bruce (sqlwork.com)
  • Re: add controls at runtime and save data to database

    10-29-2009, 10:24 PM
    Answer
    • All-Star
      91,758 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    suri_vinod:
    User should be able to click some link, say more, and add as many textboxes they want.

    The second link that I have provided should do the things you described:

    FAQ: Dynamically Adding Rows in ASP Table on Button Click event

    "Code,Beer and Music ~ my way of being a programmer"

Page 1 of 1 (6 items)