C# dropdown list selection

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
So I started learning C# today (literally...maybe 10 hours ago?) and I am loving it so far. Visual Studio 2010 Pro is THE BOMB. It seems like it does everything for you. Anyways, I'm making a webpage that includes a dropdown menu. For each selection in the dropdown menu, I will then display a list of checkboxes. Each dropdown menu value will have a different list of checkboxes that are associated with it. I have the dropdown menu set up, but how do I tell it to do an action (like display the list of checkboxes) once a value is selected? Likewise (this is probably the same process), how can I tell my web application to complete an action once a certain checkbox is selected?
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
You create a function that's triggered by the on select event.
Code:
<asp:DropDownList ID="ddlBlah"  AutoPostBack="true" OnSelectedIndexChanged="SelectBlah" runat="server">
    <asp:ListItem />
</asp:DropDownList>
 
protected void SelectBlah(Object sender, EventArgs e)
{
    //not needed if you don't have the blank selection
    if (ddlBlah.SelectedValue != String.Empty)
    {
         //what you want to do
    }
 
}
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Ok I guess where I messed up then is what Value does it return when you select an item from the list? I tried both with the name of it as a string and I also gave it a numerical value (0,1,2,3) and it seemed like both did not work, but I guess I tried using them wrong. So, just for clarification, if the second item in my dropdown list is Cat and I give it numerical value 1, is (in your example) ddlBlah.SelectedValue a string "Cat" or an int 1 or a string "1" or something else entirely?

Thanks
 

Hmongkeysauce

Senior member
Jun 8, 2005
360
0
76
How did you bind the drop down list? It's been awhile since I did asp.net but if I remember correctly, you should be able to set the value and text of each item in the list separately. I'd imagine you may want the value to be numeric while the text to be "Cat".

Just something I was pondering about but are you sure you want a drop down list of checkboxes? The default drop down list control only allows single selection (if I'm not mistaken). That means you can only select one checkbox at a time which would defeat the purpose of a list of checkboxes, no? I apologize if you already went through this thought process :p
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
DropDownLists uses a collection of ListItems. ListItem has a text and value object. Value is generally hidden text is what you see. Value is a string so I think it'll return "1" as a string.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
How did you bind the drop down list? It's been awhile since I did asp.net but if I remember correctly, you should be able to set the value and text of each item in the list separately. I'd imagine you may want the value to be numeric while the text to be "Cat".

Just something I was pondering about but are you sure you want a drop down list of checkboxes? The default drop down list control only allows single selection (if I'm not mistaken). That means you can only select one checkbox at a time which would defeat the purpose of a list of checkboxes, no? I apologize if you already went through this thought process :p

It isn't a dropdown list of checkboxes. It is a dropdown list, of which when a value is selected then a certain list of checkboxes will appear on the page. So if I have Cat and Dog as selections in my dropdown list, I may have 5 checkboxes appear on the page if the user selects Cat and only 3 appear if they select Dog.

Thanks for the help though guys. Hopefully I can figure this out, since this is basically the main function to display info on my website.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
DropDownLists can't have multiple selections you'll get a runtime error if you try to force multiple selections programatically. Do you want a ListBox instead?
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Sorry I worded that poorly. I have a dropdownlist of multiple things, of which the user will choose one. It is a normal dropdownlist. Just when the user chooses an option, I need to then display several checkboxes containing traits similar to what they chose in the dropdown menu and also update other information on my page.

Here is what I am trying to do. Components is my DropDownList.
Code:
protected void Components_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (Components.SelectedIndex == 1)
            {
                  //display checkboxes specific to selection 1     
               protected void CheckBoxList1.SelectedIndexChanged(object sender, EventArgs e)
        {

        }
            }

It would then go on to say if (Components.SelectedIndex == 2) display the next list of checkboxes, etc.

I get an error if I try to do it like this?
 
Last edited:

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
By default, dropdownlists do not automatically cause a postback on change. Any C# code you have only runs on the server side, and without a postback, it can't do anything.

In the designer, select your dropdownlist and change autopostback to true. Then you can run server side code to display/hide checkboxes and it will kick in when the select is changed. Kind of stiff though - probably more appropriate to do this client side with javascript.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,635
4,562
75
Don't you mean to say something like:
Code:
protected void Components_SelectedIndexChanged(object sender, EventArgs e)
{         
  if (Components.SelectedIndex == 1)
  {
    //display checkboxes specific to selection 1     
    CheckBoxList1.SelectedIndexChanged(sender, e);
  }
}

In general, I'd expect you to have several hidden collections of checkboxes, and display one while hiding the others. But I assume that's within CheckBoxList1.SelectedIndexChanged. Otherwise I'd have a list of collections of checkboxes set to visible and/or invisible at each if.

Edit: Or maybe its just your braces and indentation that have me confused.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Don't you mean to say something like:
Code:
protected void Components_SelectedIndexChanged(object sender, EventArgs e)
{         
  if (Components.SelectedIndex == 1)
  {
    //display checkboxes specific to selection 1     
    CheckBoxList1.SelectedIndexChanged(sender, e);
  }
}

In general, I'd expect you to have several hidden collections of checkboxes, and display one while hiding the others. But I assume that's within CheckBoxList1.SelectedIndexChanged. Otherwise I'd have a list of collections of checkboxes set to visible and/or invisible at each if.

Edit: Or maybe its just your braces and indentation that have me confused.

I try that, but it doesn't work. You are definitely understanding correctly what I want, but the code you gave me will give me compilation errors.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Ok here is a similar example of what I want. http://msdn.microsoft.com/en-us/lib...trols.listcontrol.onselectedindexchanged.aspx This example contains a Radiobuttonlist and a Label. To compare it to what I need, the radiobuttonlist would be like my dropdownlist and the label would be like my checkboxes. If a person selects Item 1 from the radiobuttonlist (or my dropdownlist) it should display the label (or checkboxes) associated with Item 1 only, and no others should appear on the page.

Using this example, I actually got a Label that will say "This is: " and then the number that corresponds with whichever Item in my Dropdownlist I have selected.
Code:
void Index_Changed(Object sender, EventArgs e)
    {
        Label1.Text = "This is: " + Components.SelectedIndex;
    }
However, no implementation I have found of CheckBoxList will work in place of this Label.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Create all your checkboxlists and make them visible = false then in the event function change them to visible as needed. Off the top of my head you'd probably use a case/switch dependent on the index of the dropdownlist.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Create all your checkboxlists and make them visible = false then in the event function change them to visible as needed. Off the top of my head you'd probably use a case/switch dependent on the index of the dropdownlist.

What you just told me is (I think) the solution to my problem, and the way I want to do things, but it still doesn't work in my WebApp. If I do a regular Windows Form App for my computer, the .Visible = true method works exactly as I would want it, but it does not work in my webpage for some reason. I tried placing the code in both my Page.aspx and Page.aspx.cs areas, and then just one and not the other, but no combination seemed to work correctly. Is there something else I have to do sense it is going to be on a webpage? I get no errors when I run it, it just doesn't show up.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
First, for ASPNET, get used to using CONTROL.Style.Add("display", "none") to hide and CONTROL.Style.Add("display", "") to show.

Second, consider grouping your different sets of checkboxes inside a panel and do a show/hide on the panel rather than each individual checkbox. This will make your life easy.

Third, considering these hide/show commands are server side, you will need to cause a postback or a partial postback, at least for your events to have an effect. Sometimes AutoPostBack="true" does not have the intended effects especially if you process server side commands before the event to trigger the postback happens.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
First, for ASPNET, get used to using CONTROL.Style.Add("display", "none") to hide and CONTROL.Style.Add("display", "") to show.

Second, consider grouping your different sets of checkboxes inside a panel and do a show/hide on the panel rather than each individual checkbox. This will make your life easy.

Third, considering these hide/show commands are server side, you will need to cause a postback or a partial postback, at least for your events to have an effect. Sometimes AutoPostBack="true" does not have the intended effects especially if you process server side commands before the event to trigger the postback happens.

Thanks for your quick response. I will play around with the CONTROL.Style.Add(). Also, I am using CheckBoxLists rather than just single checkboxes, but I will see how I can use panels to make grouping/hiding/showing them more efficient and effective. I will also have to read up more on PostBacks if you are saying that AutoPostBack isn't enough, I wasn't aware that that wasn't always going to be what I needed to use.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Thanks for your quick response. I will play around with the CONTROL.Style.Add(). Also, I am using CheckBoxLists rather than just single checkboxes, but I will see how I can use panels to make grouping/hiding/showing them more efficient and effective. I will also have to read up more on PostBacks if you are saying that AutoPostBack isn't enough, I wasn't aware that that wasn't always going to be what I needed to use.

While you're at it, study up on AJAX and updatepanels. It's just a teeny more programming but with no full screen flashing postback effects.

Basically, it will write client side code that processes server side commands.

Also, I gave a little more thought about your postback issue. Do you initialize any stuff in page load? If so, did you make sure you did the standard "if (!isPostBack) { DO STUFF}" If not, you will reload the same initial stuff every postback.
 
Last edited:

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
While you're at it, study up on AJAX and updatepanels. It's just a teeny more programming but with no full screen flashing postback effects.

Basically, it will write client side code that processes server side commands.

Also, I gave a little more thought about your postback issue. Do you initialize any stuff in page load? If so, did you make sure you did the standard "if (!isPostBack) { DO STUFF}" If not, you will reload the same initial stuff every postback.

You know, I did do the "if (!isPostBack) { DO STUFF}" for some things, but I think I deleted it when I tried the newest stuff, I'll make sure to add it back in. I'm still very new at this, been at it 4 days now, but I think this thread and other tutorials online have really helped me progress.
 

Hmongkeysauce

Senior member
Jun 8, 2005
360
0
76
If you are using a checkboxlist and posting back to the server every time a selection changes, why not just build the list at runtime and bind the list to the checkboxlist instead of creating each checkboxlist beforehand and worry about hide/show?