C# Web App: Creating labels from drop down list selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmj07
    New Member
    • Aug 2008
    • 55

    C# Web App: Creating labels from drop down list selection

    Hi all,

    I would like to know a way to add a label to an update panel by using a drop down list selected item.

    For example:

    There is a list of items in a drop down list, the user selects one then presses the add button next to it and it creates a label in a table next to it.

    Here is the code I have at the moment:

    Code:
    protected void btAdd_Click(object sender, EventArgs e)
        {
            Label lblSelected = new Label();
            lblSelected.Text = ddlAdd.SelectedItem.Text;
            ddlAddingSales.Items.Remove(ddlAdd.SelectedItem);
            UpdatePanel.ContentTemplateContainer.Controls.Add(lblSelected);
        }
    Of course with this way though it just keeps writing over the previously created label :(

    Many thanks in advance.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Why don't you just append to the existing label instead of creating a whole bunch of controls that are not needed? This would reduce page bloat.

    -Frinny

    Comment

    • dmj07
      New Member
      • Aug 2008
      • 55

      #3
      I don't understand what you mean, if you mean append to a label already on the webform then I dont have one because I want to dynamically creat them without full page postback. If you could provide me any sample code to show me what you mean that would be helpful.

      Many thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Check out the article on how to use dynamic controls in ASP.NET. It should help you understand how to do want you want.

        -Frinny

        Comment

        • dmj07
          New Member
          • Aug 2008
          • 55

          #5
          Thankyou very much for your help. Although the page was very helpful I need to load the created labels into a update panel inside a modal popup box. I will keep trying though.

          Many thanks again.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by dmj07
            Thankyou very much for your help. Although the page was very helpful I need to load the created labels into a update panel inside a modal popup box. I will keep trying though.

            Many thanks again.
            A Modal pop up box? As in a JavaScript Alert Box?
            Labels are .NET controls......a lert boxes are JavaScript and only display text.

            You could keep track of the items selected and add them to a String ... you can then add to a JavaScript call that makes an alert box with the string containing the selected items.

            -Frinny

            Comment

            Working...