2013-05-06 16 views
0

当我点击编辑按钮表格填充文本框中的数据库值以及下拉列表..如何在编辑中将下拉列表值和项目设置为数据库表格值?

设置文本框的值。但我无法设置下拉列表值。

代码是;

SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
con.Open(); 
string str = "Select * from Master where Id='" + id + "'"; 
SqlCommand command = new SqlCommand(str, con); 
SqlDataReader reader = command.ExecuteReader(); 
while (reader.Read()) 
{ 
     ddCustomerName.DataValueField = reader["CustomerId"].ToString(); 
     txtPickupLocation.Text = reader["Location"].ToString(); 
} 
con.Close(); 

回答

1
use selectedvalue property 

while (reader.Read()) 
{ 
    ddCustomerName.SelectedValue= reader["CustomerId"].ToString(); 
    txtPickupLocation.Text = reader["Location"].ToString(); 
} 
+0

严其工作... – Saranya 2013-05-06 06:40:18

+0

好...投票,如果它的工作:) – 2013-05-06 06:43:25

2

你必须在客户在选择的列表中,你必须写

ddCustomerName.Items.FindByValue(reader["CustomerId"].ToString()).selected=true; 

,而不是

ddCustomerName.DataValueField = reader["CustomerId"].ToString(); 
2

嘿Saranya先前的职位是完美的,但如果下拉值与DB值不匹配,或者下拉值不是由Db返回的值,则会发生错误,因此安全方总是检查n也值。

请使用此代码

if (ddCustomerName.Items.FindByValue(reader["CustomerId"].ToString()) != null) 
     dddCustomerName.Items.FindByValue(reader["CustomerId"].ToString()).selected = true; 
相关问题