编辑页面上的下拉列表中的重复项目

时间:2015-08-02 06:23:46

标签: asp.net sql-server drop-down-menu

我有一个要编辑的页面user profile。在页面加载时,数据会加载到给定userid的页面。问题是下拉列表中有一个重复的项目被选中。如下所示:

enter image description here

这是我的代码:

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //btnReset.Visible = false;
        BindDisabilityDropDown();
        BindGenderDropDown();
        BindStateDropDown();
        ExtractUserData();
    }
 }
 protected void BindGenderDropDown()
{
    string CS = ConfigurationManager.ConnectionStrings["ss"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tblGenders", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlGender.DataSource = ds;
        ddlGender.DataTextField = "GenderName";
        ddlGender.DataValueField = "GenderId";
        ddlGender.DataBind();
    }
    ddlGender.Items.Insert(0, new ListItem("---Select---", "0"));
    ddlGender.SelectedIndex = 0;
}
 private void ExtractUserData()
{
    string CS = ConfigurationManager.ConnectionStrings["ss"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from tblAllUsers where UserId='PL00012'", con);
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            string path = rdr["ProfilePicPath"].ToString();
            hfImagePath.Value = Path.GetFileName(path);
            lblUserId.Text = rdr["UserId"].ToString();
            txtFName.Text = rdr["FirstName"].ToString();
            txtLName.Text = rdr["LastName"].ToString();
            txtEmailAddress.Text = rdr["EmailAdd"].ToString();
            txtContactNumber.Text = rdr["MobileNo"].ToString();
            txtdob.Value = rdr["DOB"].ToString();
            txtStreetAddress.Text = rdr["StreetAddress"].ToString();
            txtZipCode.Text = rdr["ZipCode"].ToString();
            ddlGender.SelectedItem.Text = rdr["Gender"].ToString();
            ddlDisabled.SelectedItem.Text = rdr["Disabled"].ToString();
            ddlState.SelectedItem.Text = rdr["State"].ToString();
            ddlCity.SelectedItem.Text = rdr["City"].ToString();
        }
    }

我猜测的是,下拉列表由BindGenderDropDown()加载,然后SelectedItem再次添加ExtractData()。那么我怎样才能避免不必要的添加?

2 个答案:

答案 0 :(得分:1)

它可能对你有所帮助。

if (ddlGender.Items.Contains(ddlGender.Items.FindByText(rdr["Gender"].ToString())))
            {
                ddlGender.Items.FindByText(rdr["Gender"].ToString()).Selected = true;
            }

在您的情况下,ddlGender.SelectedItem.Text = rdr["Gender"].ToString();正在更改所选索引的文字,因此它会将--Select--更改为Your Selected Value

并且为了选择Dropdown中的Item,我建议使用下面的代码。

ddlGender.SelectedValue = rdr["GenderId"].ToString();

答案 1 :(得分:1)

您可以在DropdownList方法中选择SelectedItem ExtractUserData()时执行以下操作:

ddlGender.SelectedIndex = ddlGender.Items.IndexOf(ddlGender.Items.FindByText(rdr["Gender"].ToString()));

您的代码ddlGender.SelectedItem.Text = rdr["Gender"].ToString();正在显示---Select--- rdr["Gender"].ToString()。此代码不会更改项目,但只选择项目。