本文介绍了asp.net中的dropdownlist c#将select语句作为选定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#c​​ode --Dropdown code

c# code --Dropdown code

private void FillBatch()  // Batch
   {
       try
       {
           clsDataAccess da = new clsDataAccess();
           da.AddTabletoDataset("SELECT  Batch_Year,BatchId FROM Batch", "Batch");
           if (da.DataSet.Tables["Batch"].Rows.Count > 0)
           {
               DDL_Batch.DataSource = da.DataSet.Tables["Batch"];
               DDL_Batch.DataTextField = "Batch_Year";
               DDL_Batch.DataValueField ="BatchId";
               DDL_Batch.DataBind();
               DDL_Batch.Items.Insert(0, "Select");
           }
       }
       catch (Exception ex)
       {
           HandleException.SendMail(ex);
       }
   }





创建表Batch(BatchId int,Batch_Year varchar(50))







数据库插入脚本



create table Batch (BatchId int,Batch_Year varchar(50))



database insert script

insert Batch values
(1,'2013'),(2,'2012'),
(3,'2011'),
(4,'2010'),
(5,'2009'),
(6,'2008'),
(7,'2007'),
(8,'2006'),
(9,'2005'),
(10,'2004')

推荐答案

private void FillBatch()  // Batch
   {
       try
       {
           SqlDataAdapter da=new SqlDataAdapter("SELECT  Batch_Year,BatchId FROM Batch", con);
         Dataset ds=new Dataset();
da.Fill(ds);
           if (ds.Tables[0].Rows.Count > 0)
           {
               DDL_Batch.DataSource = ds;
               DDL_Batch.DataTextField = "Batch_Year";
               DDL_Batch.DataValueField ="BatchId";
               DDL_Batch.DataBind();
               DDL_Batch.Items.Insert(0, "Select");
           }
       }
       catch (Exception ex)
       {
           HandleException.SendMail(ex);
       }
   }


protected void BindCountryDropDown()
   {
       DataTable dt = null;
       using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tempdbConn"].ConnectionString))
       {
           using (SqlCommand cmd = conn.CreateCommand())
           {
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = "SELECT  Batch_Year,BatchId FROM Batch";
               using (SqlDataAdapter da = new SqlDataAdapter(cmd))
               {
                   dt = new DataTable();
                   da.Fill(dt);
               }
           }
       }
       ddlCountry.DataSource = dt;
       ddlCountry.DataTextField = "Batch_Year";
       ddlCountry.DataValueField = "BatchId";
       ddlCountry.DataBind();
       ddlCountry.Items.Insert(0, new ListItem("Select Batch"));
   }



这篇关于asp.net中的dropdownlist c#将select语句作为选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:32