Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

controls and tablerows on the fly 2

Status
Not open for further replies.

henslecd

MIS
Apr 21, 2003
259
US
Is there any way to create tablerows based on the value of a dropdownlist?

I.e dropdown contains values 1 through 10.

User selects 5. 5 Tablerows are created.

I was thinking something along the lines of
i=dropdown.selectedvalue
so i=5...

For x = 1 to i
<TR>
<TD>
<asp:dropdownlist id=dd<%=x%></asp:dropdownlist>.
<asp:textbox id=Textbox1<%=x%> runat=server></asp:textbox>.
<asp:textbox id=Textbox2<%=x%> runat=server></asp:textbox>.
<asp:textbox id=Textbox3<%=x%> runat=server></asp:textbox>.
<asp:textbox id=Textbox4<%=x%> runat=server></asp:textbox>
</TD>
</TR>

That is the way I did it in asp but it craps out on the <%%>.
Is there a way to create the controls dynamically, giving them ids?

Another issue is the placement on the page. I tried writing it out to a label to make it

appear where I want instead of the top of the page. That worked fine but I couldn't get

dropdowns created.

Thanks for the help
 
Hen: You're probably going to have to create the textboxes in code behind and not loope through the HTML -- Until someone adds a little more light, check out LV's (C#) solution to creating a Textbox dynamically, and see if this approach is not scalable.

thread855-754338
 
This should help.

Code:
<table id="myTable" runat="server">
   <tr>
     <td>My Table</td>
   </tr>
</table>

...

In code-behind:

Code:
private void myDropDown_SelectedIndexChanged(object sender, System.EventArgs e)
{
	HtmlTableRow tr;
	HtmlTableCell tc;
	TextBox txt;

	int rows = 
		Convert.ToInt32( myDropDown.SelectedValue );

	for( int i = 0; i < rows; i++ )
	{
		tr = new HtmlTableRow();
		tc = new HtmlTableCell();
		txt = new TextBox();

		tc.Controls.Add( txt );
		tr.Cells.Add( tc );
		myTable.Rows.Add( tr );
	}
}

If you want to get the values from the controls, you just need to re-create them on PostBack, then treat them in code as any typical control.

Let me know if you'd like more info.
 
Thanks, Isadore.

I just had to do something like this for work so it's fresh in my brain.
 
Oh here is the vb version...


If (True) Then
Dim tr As HtmlTableRow
Dim tc As HtmlTableCell
Dim txt As TextBox

Dim rows As Integer = Convert.ToInt32(myDropDown.SelectedValue)

Dim i As Integer
For i = 0 To rows - 1
tr = New HtmlTableRow()
tc = New HtmlTableCell()
txt = New TextBox()

tc.Controls.Add(txt)
tr.Cells.Add(tc)
myTable.Rows.Add(tr)
Next i
End If

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top