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();   
4

3 回答 3

2

您必须在所选列表中选择 CustomerId,为此您必须编写

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

代替

 ddCustomerName.DataValueField = reader["CustomerId"].ToString();
于 2013-05-06T06:32:04.880 回答
2

嘿,Saranya 之前的帖子很完美,但如果 Dropdown 值与 DB 值不匹配,或者 Dropdown 没有 Db 返回的值,则会出错,因此更安全的一方也总是检查空值。

请使用此代码

if (ddCustomerName.Items.FindByValue(reader["CustomerId"].ToString()) != null)
        dddCustomerName.Items.FindByValue(reader["CustomerId"].ToString()).selected = true;
于 2013-05-06T06:57:15.027 回答
1
use selectedvalue property

while (reader.Read())
{
    ddCustomerName.SelectedValue= reader["CustomerId"].ToString();
    txtPickupLocation.Text = reader["Location"].ToString();
}
于 2013-05-06T06:36:49.870 回答