Programmatically add textbox control with incrementing id

Discussion in 'ASP .Net Web Controls' started by nLL, Mar 12, 2009.

  1. nLL

    nLL Guest

    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
     
    nLL, Mar 12, 2009
    #1
    1. Advertising

  2. nLL

    nLL Guest

    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:
    > 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
    >
    >
     
    nLL, Mar 12, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Justin Gengo
    Replies:
    0
    Views:
    618
    S. Justin Gengo
    Jul 16, 2003
  2. jrpe
    Replies:
    0
    Views:
    1,089
  3. Chad Scharf
    Replies:
    3
    Views:
    954
    Chad Scharf
    Oct 18, 2007
  4. nLL
    Replies:
    2
    Views:
    1,530
  5. Peter Pan
    Replies:
    0
    Views:
    1,471
    Peter Pan
    Apr 13, 2009
Loading...

Share This Page