Programmatically add textbox control with incrementing id

N

nLL

Hi everyone, I am developing a simple poll/voting application and i need
to programmatically add choices/options as user clicks to add new choice
button.

I have
Poll question text box
First poll answer/choice with id choice1 in Choices Placeholder
Add new choice button.

When add new choice button clicked, i want to get id of last textbox
control in Choice Placeholder and add new textbox control with
incremented id.

here is the code side of it

-----------------
Poll question:
<asp:TextBox ID="PollQuestionText" runat="server"></asp:TextBox><br />
<asp:placeHolder ID="PollAnswers" runat="server">
<asp:Label ID="PollAnswerLabel1" runat="server" Text="Choice
1"></asp:Label>: <asp:TextBox ID="PollAnswer1" runat="server"></asp:TextBox>
<!-- INSERT NEW TEXTBOX CONTROL HERE AS ADD NEW BUTTON CLICKED-->
</asp:placeHolder>

<asp:Button ID="addChoiceButton" runat="server" Text="Add new choice"
onclick="addChoiceButton_Click" />
<asp:Button ID="SaveButton" runat="server" Text="Save" />
--------------------------------------------------

This will be a mobile web app so I can't use javascript.
Could any one point me to the right direction?

Thanks
 
E

Eliyahu Goldin

The right direction will be to lay out all your textboxes in the markup and
to hide/show them as needed. Asp.net is object-oriented, not
procedure-oriented.
 
N

nLL

got it with

protected void addChoiceButton_Click(object sender, EventArgs e)
{
addChoiceButton.Visible = false;
ChoicecountDropDownList.Visible = false;
HyperLinkReload.Visible = true;
HyperLinkReload.NavigateUrl = Request.FilePath +"?"+
Request.QueryString;

int numtextboxes =
System.Convert.ToInt32(ChoicecountDropDownList.SelectedItem.Value);
for (int i = 1; i <= numtextboxes; i++)
{
TextBox myTextbox = new TextBox();
myTextbox.ID = "Choice" + i.ToString();

Label myTextboxLabel = new Label();
myTextboxLabel.Text = "Choice " + i.ToString();
myTextboxLabel.ID = "Label" + i.ToString();
ChoicesPlaceHolder.Controls.Add(myTextboxLabel);
ChoicesPlaceHolder.Controls.Add(new LiteralControl(": "));
ChoicesPlaceHolder.Controls.Add(myTextbox);
ChoicesPlaceHolder.Controls.Add(new LiteralControl("<br />"));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top