ALOK
ALOK

Reputation: 553

Populated Dropdownlist not working properly

Here ext is storing "System.Web.UI.WebControls.Label" but i want to retrieve the selected text in dropdownlist. What's the problem here? I am populating dropdownlist2 based on dropdownlist1. please refer to below code.

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    a = DropDownList1.SelectedItem.Text;
    DropDownList2.Items.Clear();
    if (a == "Electronic")
    {
        DropDownList2.Items.Add(new ListItem("---Select SubCategory---","0"));
        DropDownList2.Items.Add(new ListItem("Mobile", "1"));
        DropDownList2.Items.Add(new ListItem("Tablet", "2"));
        DropDownList2.Items.Add(new ListItem("Laptop", "3"));
        DropDownList2.Items.Add(new ListItem("Accessories", "4"));
    }
    else if (a == "Kitchen")
    {
        DropDownList2.Items.Add(new ListItem("---Select SubCategory---", "0"));
        DropDownList2.Items.Add(new ListItem("Cookware", "1"));
        DropDownList2.Items.Add(new ListItem("Dinner set", "2"));
        DropDownList2.Items.Add(new ListItem("Food processor", "3"));
        DropDownList2.Items.Add(new ListItem("Microwave", "4"));

    }
    else if (a == "Books")
    {
        DropDownList2.Items.Add(new ListItem("---Select SubCategory---", "0"));
        DropDownList2.Items.Add(new ListItem("Entrance exam", "1"));
        DropDownList2.Items.Add(new ListItem("Fiction", "2"));
        DropDownList2.Items.Add(new ListItem("Romance", "3"));
        DropDownList2.Items.Add(new ListItem("Thriller", "4"));

    }
    else if (a == "Beauty")
    {
        DropDownList2.Items.Add(new ListItem("---Select SubCategory---", "0"));
        DropDownList2.Items.Add(new ListItem("Mens perfume", "1"));
        DropDownList2.Items.Add(new ListItem("Womens perfume", "2"));
    }
      string ext = DropDownList2.SelectedItem.Text;
}

Edit:

Problem here is that I am using validation & it must not allow submission if I press submit button. But I have more 4 buttons for navigation purpose and that validation is not allowing me to use these buttons i.e it should allow me to execute code of button2_click and not allow for submit_click.

Upvotes: 0

Views: 108

Answers (3)

Emad Mokhtar
Emad Mokhtar

Reputation: 3297

You have 2 solutions:

  1. Use ValidationGroup property to group of controls to activate certain validations.
  2. Set CausesValidation = false for the 4 navigation buttons.

Upvotes: 1

Zo Has
Zo Has

Reputation: 13018

First thing,

That is really a bad way to hard code your dropdown lists like that based on a parent value. This is very hard to maintain in future when you add/remove items from your code. What you can do is to create a separate table in your database & store your items inside that table with a foreign key relation to the parent table like

   Tbl_Categories                         Tbl_Items
   ----------------------------           -----------------------------------
   CategoryID      Description            ItemID     CategID    Description
   ----------------------------           -----------------------------------
      1            Electronics              1          1        Mobiles
      2            Kitchen                  2          1        Tablets
      3            Books                    3          1        Laptops
      4            Beauty                   4          1        Accessories

Then you would just define the datasource of your dropdownlist to filter on the selectedValue of the parent dropdown. No need to code it manually. You can check the answer in this post as a starting point How to select dropdown lists with sql datasource

Coming to your main problem,

You can define a validation group on your submit button & the validation controls specifically. This enables you to invoke the validation only when that button is pressed. Specifying Validation Groups on MSDN

<!--When Button1 is clicked, only validation
controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1" 
  text="Validate" 
  causesvalidation="true"
  validationgroup="PersonalInfoGroup"
  runat="Server" />

  <asp:requiredfieldvalidator id="RequiredFieldValidator3"
  controltovalidate="CityTextBox"
  validationgroup="PersonalInfoGroup"
  errormessage="Enter a city name."
  runat="Server">
</asp:requiredfieldvalidator>

Upvotes: 1

Laila
Laila

Reputation: 437

So you mean that you want the validation to work only with the submit button and not with all the buttons. You can refer to this page : http://msdn.microsoft.com/en-us/library/ms227424.aspx So, in this way you can assign the validations to work only with the Submit Button you have.

As an example:

    <asp:textbox id="AgeTextBox" 
      runat="Server">
    </asp:textbox>
<br/>
    <asp:requiredfieldvalidator id="RequiredFieldValidator2"
      controltovalidate="AgeTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your age."
      runat="Server">
    </asp:requiredfieldvalidator>
<br/>
  <asp:button id="Button1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server" />

Upvotes: 2

Related Questions