Home All Groups Group Topic Archive Search About

Programmatically add textbox control with incrementing id

Author
12 Mar 2009 11:16 AM
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

Author
12 Mar 2009 12:12 PM
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 />"));
         }


nLL wrote:
Show quoteHide quote
> 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
>
>