By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,727 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,727 IT Pros & Developers. It's quick & easy.

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

P: 55
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:

Expand|Select|Wrap|Line Numbers
  1. protected void btAdd_Click(object sender, EventArgs e)
  2.     {
  3.         Label lblSelected = new Label();
  4.         lblSelected.Text = ddlAdd.SelectedItem.Text;
  5.         ddlAddingSales.Items.Remove(ddlAdd.SelectedItem);
  6.         UpdatePanel.ContentTemplateContainer.Controls.Add(lblSelected);
  7.     } 
  8.  
Of course with this way though it just keeps writing over the previously created label :(

Many thanks in advance.
Sep 23 '08 #1
Share this Question
Share on Google+
5 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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
Sep 23 '08 #2

P: 55
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
Sep 23 '08 #3

Frinavale
Expert Mod 5K+
P: 9,731
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
Sep 24 '08 #4

P: 55
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.
Sep 24 '08 #5

Frinavale
Expert Mod 5K+
P: 9,731
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......alert 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
Sep 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.