pauledwardian
asked on
Generate 4 textboxes in a row
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....
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.
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.
ASKER
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.
tb.TextMode = TextBoxMode.MultiLine;
tb.Height = 30;
tb.Width = 150;
tb.Height = 30;
tb.Width = 150;
<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>
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();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Default.aspx
Open in new window
Default.aspx.cs
Open in new window
Yields: