如何从数据表中设置数据绑定下拉列表中的选定值

时间:2012-12-10 15:16:28

标签: c# asp.net drop-down-menu

好的,所以我有一个最初从数据库填充的下拉列表。现在基于数据表,我希望所选值等于驻留在数据库中的文本。无论我做什么,它只显示“---选择一个---”,这是我手动添加到下拉列表项列表中的唯一项目,如果我拉的值为空(或者这就是我想要的那样),则显示默认值做)

 protected void Page_Load(object sender, EventArgs e)
    {
        Master.TopLabel = "Survey Creation";
        if (!IsPostBack)
        {

            SqlConnection Connection = DatabaseConnection.GetSurveySystemConnection();

            string sqlquery = "SELECT S.[Survey_Desc], S.[Start_Date], C.[Category_Name] ,S.[End_Date], S.[Audience] FROM [Survey] S  Inner Join Category C On S.Category_ID = C.ID Where S.[ID] =" + Session["Survey_ID"];

            SqlCommand cmd = new SqlCommand(sqlquery, Connection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable DT = new DataTable();
            da.Fill(DT);

            if (DT != null)
            {
                DescriptionMemo.Text = DT.Rows[0]["Survey_Desc"].ToString();
                CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items.IndexOf(CategoryDropDownList.Items.FindByText(DT.Rows[0]["Category_Name"].ToString()));
                StartDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["Start_Date"].ToString());
                EndDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["End_Date"].ToString());
                string Audience = DT.Rows[0]["Audience"].ToString();
                if (Audience == "Students Only")
                {
                    AudienceRadioGroup.Items[0].Selected = true;
                }
                else if (Audience == "Staff Only")
                {
                    AudienceRadioGroup.Items[1].Selected = true;
                }
                else
                {
                    AudienceRadioGroup.Items[2].Selected = true;
                }


            }
          Connection.Close();
        }
    }

aspx页面中的DropDownList。

<asp:DropDownList ID="CategoryDropDownList" runat="server" 
                    DataSourceID="SqlDataSource1" DataTextField="Category_Name" 
                    DataValueField="Category_Name" AppendDataBoundItems="true" Height="16px" 
                    Width="200px">
                    <asp:ListItem>---Select One---</asp:ListItem>
                </asp:DropDownList>

sql datasource select命令。

SELECT [Category_Name] FROM [Category]

编辑:这是完整的代码,但我不知道它是否相关,抱歉。

3 个答案:

答案 0 :(得分:3)

更改:

CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items.IndexOf(CategoryDropDownList.Items.FindByText(DT.Rows[0]["Category_Name"].ToString()));

CategoryDropDownList.SelectedValue = DT.Rows[0]["Category_Name"].ToString()

答案 1 :(得分:2)

两件事:

  1. 不要在ASP.NET中使用全局连接(至少不要使它们成为静态连接),因为它是一个多线程环境。而是始终使用using-statement
  2. 尽快关闭连接
  3. if(!IsPostback)进行Page_Load检查,当EnableViewState设置为true时,请不要在回发上进行数据绑定控制(默认)。否则,将不会触发事件,并且会覆盖值(如DropDownList-SelectedIndex)。

  4. protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string sqlquery = "SELECT S.[Survey_Desc], C.[Category_Name], FROM [Survey] S  Inner Join Category C On S.Category_ID = C.ID Where S.[ID] =" + Session["Survey_ID"];
            using(var con=new SqlConnection(connectionString))
            using(var cmd = new SqlCommand(sqlquery, con))
            using(var da = new SqlDataAdapter(cmd))
            {
                DataTable DT = new DataTable();
                da.Fill(DT);
                DescriptionMemo.Text = DT.Rows[0].Field<string>("Survey_Desc");
                string categoryName = DT.Rows[0].Field<string>("Category_Name");
                CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items
                    .IndexOf(CategoryDropDownList.Items.FindByText(categoryName));
            }
        }
    }
    

答案 2 :(得分:0)

的onPageLoad     CategoryDropDownList.Items.FindByText(categoryName).selected = true;