2012-07-31 13 views
5

To jest mój plik CS na liście rozwijanej:Jak dodać produkt w rozwijane listy w pliku cs

protected void BindDropDownList()  

    { 

     DataTable dt = new DataTable(); 
     string connString = System.Configuration.ConfigurationManager.AppSettings["EyeProject"]; 
     SqlConnection conn = new SqlConnection(connString); 


     try 
     { 
      conn.Open(); 
      string sqlStatement = "SELECT FirstName FROM tbl_UserDetails"; 
      SqlCommand sqlCmd = new SqlCommand(sqlStatement, conn); 
      SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); 

      sqlDa.Fill(dt); 
      if (dt.Rows.Count > 0) 
      { 
       DropDownList1.DataSource =dt; 


       DropDownList1.DataTextField = "FirstName"; // the items to be displayed in the list items 

       DropDownList1.DataBind(); 
      } 
     } 
     catch (System.Data.SqlClient.SqlException ex) 
     { 
      string msg = "No Data Found To display in the DropDown List"; 
      msg += ex.Message; 
      throw new Exception(msg); 
     } 
     finally 
     { 
      conn.Close(); 
     } 


    } 



By using this one iam getting values of table Firstname values now i want to add one more item Called ALLrecords. 

How can i add it. 

this is my Aspx file 

<div class="label"> 
            Select Name: 
            <asp:DropDownList ID="DropDownList1" runat="server"> 

            </asp:DropDownList> 
           </div> 

Odpowiedz

17

Spróbuj

DropDownList1.Items.Add(new ListItem("All Record")); 

i jeśli chcesz dodać przedmiot o wartości następnie

DropDownList1.Items.Add(new ListItem("All Record","0")); 

//or if you want to add at particular index then 

DropDownList1.Items.Insert(0,new ListItem("All Record"));// 0 is index of item 

mam nadzieję, że to pomoże.

+0

DropDownList1.Items.Add (new ListItem ("All Record")); iam za pomocą tego. po wyświetleniu allcords w liście rozwijanej pozostałe wartości, jakie kiedykolwiek uzyskałem z bazy danych, tj. imię. Musi to również pokazać w liście rozwijanym, kiedy go klikam. –

+0

Zwiń listę rozwijaną z wartościami bazy danych tak jak teraz, a następnie użyj metody Dodaj lub wstaw, która jest odpowiednia. –

2

wstawić element w określonym indeksie

DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value") 
3
DropDownList1.Items.Insert(0,new ListItem("AllRecords","itsValue_on_dropdownlist")); // use 0 to show "ALLRecords" text on top in dropdownlist 

i sugeruje, że należy powiązać wartości DropdownList również. tak -

DropDownList1.DataValueField = "FirstName"; 
0

Pisać onLoad z funkcją "addItems" w dropdownlist uznającej tag .aspx złożyć tak:

następnie utworzyć funkcję "addItems" w pliku cs.

<asp:DropDownList ID="DropDownList1" runat="server" OnLoad="addDeleteItems" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 

protected void addItems(object sender, System.EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)sender;   
    ListItem newItem = new ListItem("rose", "i"); 
    ddl.Items.Add(newItem); 
}  
Powiązane problemy