asp.net,c#.net repeated fields
I have a asp.net webpage abc.aspx
<td class="style1">
<asp:TextBox ID="chqdt1" runat="server" Width="71px"></asp:TextBox>
<a href="javascript:OpenCalFuture('ctl00_ContentPlaceHolder1_chqdt1');">
<img border="0" height="16" src="cal.gif" width="16" /></a>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="add" style="margin-left: 0px" />
</td>
<td>
</td>
I want on each button click event create new row with new textbok with calender field created
handle button's OnClick event and put in handler code like that:
TextBox2 = New TextBox()
TextBox2.ID = "TextBox2"
TextBox2.Style("Width") = "71px"
Form1.Controls.Add(TextBox2)
http://support.microsoft.com/kb/317515
Here's quick answer:
- First create a PlaceHolder for your textboxes.
- Handle buttons OnClick event
- Generate TextBox and Literal controls for your textbox and calendar field.
- Add the generated controls to PlaceHolder.
Code sample:
<td class="style1">
<asp:PlaceHolder ID="placeholder1" runat="server" />
</td>
<td>
<asp:Button OnClick="Button1_Click" ID="Button1" runat="server" Text="add" style="margin-left: 0px" />
</td>
Code Behind:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox textbox = new TextBox();
textbox.ID = "myTextBoxID";
Literal literalCal = new LiteralCalendar();
literalCal.Text =
"<a href=\"javascript:OpenCalFuture('" + textbox.ClientID + "');\"><img border=\"0\" height=\"16\" src=\"cal.gif\" width=\"16\" /></a>";
placeholder1.Controls.Add(textbox);
placeholder1.Controls.Add(literalCal);
}
The sample above might not be a complete (or valid) code because I didn't test it, but I hope you get the idea.
You'd have to do what @Dima says but instead of the Forms1.Controls you should use a Placeholder control and write:
placeholder.Controls.Add(TextBox2)
Then you'd also do:
TextBox2.Focus();
to have the focus on that textbox.
精彩评论