0

I have two dropdownlist's.DropDownList2(not bound to a datasource) and DropDownList3(bound to a datasource)

On change on input in one dropdownlist some content in the other Dropdownlist should change. For that i had used the logic as.

Autopostback is enabled for both this controls.

   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList2.SelectedItem.Text == "Stamp")
        {
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));
        }


<asp:DropDownList ID="DropDownList3" runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="skey" DataValueField="casecode" 
            AppendDataBoundItems="True" AutoPostBack="True">
            <asp:ListItem Selected="True" Value="S">Select</asp:ListItem>
        </asp:DropDownList>

Now the problem is when i select DropDownList2.SelectedItem.Text == "Reg" STA and STM are not present. I want STA and STM values back in the dropdownlist on selection of 'Reg'.

When i first load my page and select 'Reg' all the values in DropDownList3(including 'STA' and 'STM') are present and than when i select 'Stamp' the values 'STA' and 'STM' are lost(as shown in the code). Now again when i select 'Reg' this values are not there, i want this values to be present again.

What do i have to do?? Do i have to bind it again to database?

Is there any other logic for it to be used in a different way ?If anyone can help me

2 Answers 2

1

You can to bind DropDownList3 everytime DropDownList2 selected index change then only if the value is "Stamp" you remove the values "STA" and "STM" from DropDownList3

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)    
{   
   // Fill DropDownList3 data source and bind it again to restore all the items
   FillDataSource(); // This method gets all the data from DropDownList3
   DropDownList3.DataBind();

   if (DropDownList2.SelectedItem.Text == "Stamp")
   {
        DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
        DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));        
   }
   ...
Sign up to request clarification or add additional context in comments.

2 Comments

How do i use FillDataSource(); i had done databinding using wizard in VisualWebDeveloper 2008 so there is not much coding
No need to call FillDataSource(); equivalent. Just call DropDownList3.DataBind(); I have tried it and it is working.
0

If you know the values of the dropdown items, you can add them in the else clause, if you don't know the value/text combination you'll have to rebind.

5 Comments

else { DropDownList2.Items.Add("STA"); DropDownList2.Items.Add("STM"); } i had done this but it dint work
you mean DropDownList3.Items.Add("") right? What didn't work about it? they just didn't show up? Might be a problem of when you are binding - can you post more code?
also on SelectedIndexChanged the items are not deleted,only when i click on submit button i am able to see the changes. How is it so?
with autopostback=true, that should cause a page submit and should cause the items to be removed. How are these dropdown's databound? where in the code - can you post that?
I have done databinding using wizard in VisualWebDeveloper 2008 so there is not much coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.