?
Solved

Generate 4 textboxes in a row

Posted on 2012-09-13
7
Medium Priority
?
425 Views
Last Modified: 2012-10-03
I need a C# asp code that would add 4 textboxes next to each other in a row when the "Create Additional Space" button is clicked. Then if the user clicks it again, it would add another row of 4 textboxes below the previous one....
0
Comment
Question by:pauledwardian
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38397348
It would be super easy to generate the row by simply wrapping the 4 TBs with a <div>. You haven't really specified your architecture, so I'm going to assume you are using WebForms. Something like this should do the trick:

Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_27864391._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
</asp:Content>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:PlaceHolder ID="container" runat="server"></asp:PlaceHolder>
    <asp:Button ID="btnMoreSpace" runat="server" Text="Create Additional Space" 
        OnClick="btnMoreSpace_Click" />
</asp:Content>

Open in new window


Default.aspx.cs
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace _27864391
{
    public partial class _Default : Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (Session["clickCount"] == null)
            {
                Session["clickCount"] = 1;
            }

            if (IsPostBack)
            {
                for (int i = 0; i < (int)Session["clickCount"]; i++)
                {
                    HtmlGenericControl div = new HtmlGenericControl("div");

                    div.Controls.Add(new TextBox());
                    div.Controls.Add(new TextBox());
                    div.Controls.Add(new TextBox());
                    div.Controls.Add(new TextBox());

                    this.container.Controls.Add(div);
                }
            }

            base.OnInit(e);
        }

        protected void btnMoreSpace_Click(object sender, EventArgs e)
        {
            Session["clickCount"] = ((int)Session["clickCount"]) + 1;
        }
    }
}

Open in new window


Yields:

Screenshots
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38397351
P.S.

Note that the for loop is required in order to preserve the previously added dynamic TBs.

Dynamic controls must be added prior to the ViewState being loaded if you want them to preserve any values that were contained within them. This is why I put the creation and addition of the TBs in the overridden OnInit method. This is also why I kept track of the number of times the button was clicked within the Session object.
0
 

Author Comment

by:pauledwardian
ID: 38399202
Thank you! how can I change the 4th textbox property to multi line ? And how can I make the textboxes bigger? Also, I need to include this into a table format since users will put the information in and print it so there will be some borders, etc. FYI: nothing is comming from the database. Its more like something to type on the web and print it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:YZlat
ID: 38399646
tb.TextMode = TextBoxMode.MultiLine;
                tb.Height = 30;
                tb.Width = 150;
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38400033
<div>
<asp:GridView ID="gvTextBox" runat="server" AutoGenerateColumns="false" ShowHeader="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt1" runat="server" Width="100px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt2" runat="server" Width="100px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt3" runat="server" Width="100px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt4" runat="server" Width="100px" TextMode="MultiLine"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
</div>
<div>
<asp:Button ID="btnAddRows" runat="server" Text="Add Rows" 
            onclick="btnAddRows_Click" />
</div>

Open in new window

        protected void btnAddRows_Click(object sender, EventArgs e)
        {
DataTable dtText;
            if (Session["dt"] == null)
                dtText = new DataTable();
            else
                dtText = (DataTable)Session["dt"];

            DataRow dr;

            dr = dtText.NewRow();
            dtText.Rows.Add(dr);

            gvTextBox.DataSource = dtText;
            gvTextBox.DataBind();
        }

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38400211
Also, I need to include this into a table format since users will put the information in and print it so there will be some borders
If that's the case, then I would say to just use an actual Table control as opposed to the Placeholder (in the markup) that I used. Instead of working with a literal <div>, you can simply add a new row to the table.

e.g. (Adding in YZlat's suggestion):

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:Table ID="container" runat="server"></asp:Table>
    <asp:Button ID="btnMoreSpace" runat="server" Text="Create Additional Space"
        OnClick="btnMoreSpace_Click" />
</asp:Content>

Open in new window


using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication6
{
    public partial class _Default : Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (Session["clickCount"] == null)
            {
                Session["clickCount"] = 1;
            }

            if (IsPostBack)
            {
                for (int j = 0; j < (int)Session["clickCount"]; j++)
                {
                    TableRow row = new TableRow();
                    TextBox box = null;

                    for (int i = 0; i < 4; i++)
                    {
                        TableCell cell = new TableCell();

                        box = new TextBox();
                        cell.Controls.Add(box);
                        row.Cells.Add(cell);
                    }

                    box.Height = 50;

                    this.container.Rows.Add(row);
                }
            }

            base.OnInit(e);
        }

        protected void btnMoreSpace_Click(object sender, EventArgs e)
        {
            Session["clickCount"] = ((int)Session["clickCount"]) + 1;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:pauledwardian
ID: 38461219
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question