Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all, i am adding two dropdown list in my registration form. the first dropdownlist is bind to database, and second dropdownlist also from same table. if i select a item from firstdropdownlist it remove the item from second dropdownlist. That is Ok for me, but the problem is that i click all item of firstdropdown list than the second dropdownlist become empty.. See my code

XML
<asp:DropDownList ID="DrpPrefLangauge1"
                  AppendDataBoundItems ="true"
                  OnSelectedIndexChanged="DrpPrefLangauge1_SelectedIndexChanged"  
                  runat="server"
                  AutoPostBack="True">
    <asp:ListItem Text="----Select----" 
                  Value="0">
    </asp:ListItem>
</asp:DropDownList> 

<asp:DropDownList ID="DrpPrefLangauge2"
                  AppendDataBoundItems="true"
                  runat="server">
    <asp:ListItem Text="----Select----"
                  Value="0">
    </asp:ListItem>
</asp:DropDownList>


At code behind:

C#
protected void setLangauge()
{
    nsBusinessLogicLayer.LanguageInfo objLanguageInfo = new nsBusinessLogicLayer.LanguageInfo();
    nsAbstractLayer.LanguageInfoAbstract objLanguageInfoAbstract = new nsAbstractLayer.LanguageInfoAbstract();
    //objLanguageInfoAbstract.Languageid = Convert.ToInt64(DrpPrefLangauge1.SelectedValue);

    DataTable dt = objLanguageInfo.Search(objLanguageInfoAbstract);

    DrpPrefLangauge1.DataSource     = dt;
    DrpPrefLangauge1.DataTextField  = "LanguageName";
    DrpPrefLangauge1.DataValueField = "Languageid";
    DrpPrefLangauge1.DataBind();

    DrpPrefLangauge2.DataSource     = dt;
    DrpPrefLangauge2.DataTextField  = "LanguageName";
    DrpPrefLangauge2.DataValueField = "Languageid";
    DrpPrefLangauge2.DataBind();
}



ON selected index:

C#
protected void DrpPrefLangauge1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListItem  value = DrpPrefLangauge1.SelectedItem;
    //int index=DrpPrefLangauge2.Items.IndexOf(value);
    DrpPrefLangauge2.Items.Remove(value);
}

I want if i select say hindi langauge from firstdownlist , it will remove in second dropdownlist.. this is working but if i select all item from my first downlist the second dropdownlist become empty..that is the problem how it solve it..
Posted
Updated 30-Dec-10 1:42am
v3
Comments
#realJSOP 30-Dec-10 7:41am    
Fixed your code snippet blocks. PAY ATTENTION TO WHAT YOU'RE DOING.
Estys 30-Dec-10 7:52am    
Well, do you or do you NOT want to remove it???? What's the purpose?
Manfred Rudolf Bihy 30-Dec-10 8:23am    
@Estys: And I thought he was only confusing me :) .
@Balongi: What then is the behavior you are expecting? From your description I would say it works as designed.
1. If you select an entry in the first ddl you want it to be removed from the second ddl.
2. But if all entries are selected in the first list what would be the desired outcome?

Pleas feel free to elaborate what you expect for case 2.

If your intention is to keep all items with the exception of the selected item in DrpPrefLanguage1, you must rebind DrpPrefLanguage2 before removing the item.

I hope this helps.

---------------------------------------------------------------
Signature: Imobiliárias em Guarulhos
 
Share this answer
 
Comments
Manfred Rudolf Bihy 30-Dec-10 8:49am    
I also came to this view (but you wrote it first ;) ). OP wants only the item missing from the second list that is currently selected in the first list, which BTW make very much sense indeed.

Good answer! 5+
You can't have it both ways. If you want an item removed when selected then when selecting all items they will all be removed.
 
Share this answer
 
Comments
balongi 30-Dec-10 8:32am    
if i select one by one item from first dropdowlist then the second dropdownlist become empty.. this is the problme
[no name] 30-Dec-10 8:42am    
Please re-state your objective, it is not very clear what exactly you want to accomplish.
Manfred Rudolf Bihy 30-Dec-10 8:50am    
I think silvioyf got it right what OP really meant. OP wasn't expressing it clearly though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900