Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
hi iam net to asp.net and i want to get selected data was saved to dropdownlist to edit it, i retrieve data to all controls ok but can't get in dropdownlist when i use
DropDownList.SelectedItem.Text= dr["FiledName"].ToString();
 
it show in dropdownlist duplicate and when i use
DropDownList.Items.Add(dr.GetString(0));
it doesn't show any thing but duplicate data in dropdownlist
Posted 14-Apr-13 22:53pm
M.Saied1.8K
Edited 14-Apr-13 23:04pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this. May help. good way to find the item in the list and if found set it selected.
 
if(DropDownList1.Items.FindByText(dr["FiledName"].ToString())!=null)
{
                DropDownList1.Items.FindByText(dr["FiledName"].ToString()).Selected = true;
 
}
 

~Prashant
  Permalink  
v2
Comments
m.saied at 15-Apr-13 5:31am
   
get error
 
Cannot have multiple items selected in a DropDownList
khalid92 at 15-Apr-13 5:38am
   
which error you are still getting ...?
if you have duplicate field names than try to set selected index by its value like this
 
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(dr["FiledName"].ToString()));
m.saied at 16-Apr-13 0:58am
   
thank you khalid it works
PrashantSonewane at 15-Apr-13 6:38am
   
if(DropDownList1.Items.FindByText(dr["FiledName"].ToString())!=null)
{
DropDownList1.ClearSelection();
DropDownList1.Items.FindByText(dr["FiledName"].ToString()).Selected = true;

}
m.saied at 16-Apr-13 1:01am
   
thanks this also works
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try this
 
 DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(dr["FiledName"].ToString()));
  Permalink  
Comments
m.saied at 15-Apr-13 5:31am
   
thank you khalid it works

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100