Two possible approaches. I prefer the first
Option 1. in your layout, add the needed rows, make them actual table rows. you will add runat=server to the rows and give them an id. When you need them to be visible simply do a postback with the add rows button and make them visible when you need them.
If you need to do it without a postback, simply hide / show them using javascript instead of running the <tr> on the server
Option 2
add a placeholder where you need the textboxes to be and then add the textboxes in the code. Here is a sample.
private void AddTextBox(int TextBoxNumber)
{
txtItem = new TextBox();
txtItem.ID = "txtItem" + TextBoxNumber.ToString();
thePlaceHolder.Add(txtItem);
}
I thought about the option 1. But what if the customer service wants to add more records. I don;t want to restrict the textbox instead have them request as they need. I tried using the second option and on the button click I do get 3 rows of text boxes but if I try to click the button again it doesn;t add more textbox after the 3 was already added. Does that make sense? let me know I will try and re phrase the question.
This post shows you how to dynamically add rows to a table server-side:
http://forums.asp.net/thread/1264968.aspx
NC...
You must re-add the controls on each subsequent post-back. To do this, you will need a counter, and you will need a method that will add the number of textboxes in the counter.
example: create a label on the page and set the value = 0 and visible = false. This will be your counter.
create a method that runs in page_load that creates the same number of textboxes in your counter. When you click to add a textbox, increment your counter and add your new text box. Viewstate will still remember the values in the textboxes.
I am really new to this. Could you possibly provide me a sample code. Thanks.
Thanks for the pseudocode. It worked. Now I have another question. ON similar lines to the previous one, How do I create a table or table row with two text boxes in each row (in 2 different columns) based on button click. Any suggestions.
The link that I posted earlier shows exactly how to do that.
NC...
Here is the code sample and it gives me an error on the highlighted code in dark gray , saying "Value of type 'System.Web.UI.WebControls.TableRow' cannot be converted to 'System.Data.DataRow'. Help Please
Id. is the a number that passsed in this function. It is the counter which occurs on the click of the button
------------------
Dim
iAsInteger = 0DoWhile (i < dynamicTable.Rows.Count)Dim dataRowAs DataRowDataRow = dynamicTable.Rows(i)
Dim newRowAs TableRow =New TableRowDim newCell00As TableCell =New TableCellDim textboxesAsNew TextBoxtextboxes.ID =
"ProductId" + Id.ToString()textboxes.Width = Unit.Pixel(32)
Dim literalControlAsNew LiteralControlliteralControl.Text =
"<p>"form1.Controls.Add(literalControl)
form1.Controls.Add(textboxes)
Dim newCell01As TableCell =New TableCellDim textboxes1AsNew TextBoxtextboxes.ID =
"ITemId" + Id.ToString()textboxes.Width = Unit.Pixel(32)
Dim literalControl1AsNew LiteralControlliteralControl.Text =
"<p>"form1.Controls.Add(literalControl)
form1.Controls.Add(textboxes)
newRow.Cells.Add(newCell00)
newRow.Cells.Add(newCell01)
Me.dynamicTable.Rows.Add(newRow)i = (i + 1)
LoopDuplicate post -- Please don't do that.
http://forums.asp.net/thread/1264968.aspx
NC...
0 comments:
Post a Comment