簡體   English   中英

如何在數據表的數據綁定下拉列表中設置所選值

[英]How to set selected value on a databound dropdownlist from a datatable

好的,所以我有一個最初從數據庫填充的下拉列表。 現在基於數據表,我希望所選值等於數據庫中的文本。 無論我做什么,它只會顯示“ --- Select One ---”,這是我手動添加到下拉列表項列表中的唯一項,如果我要提取的值為null(或者這就是我想要的),則會顯示默認值去做)

 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數據源選擇命令。

SELECT [Category_Name] FROM [Category]

編輯:這是完整的代碼,但我不知道它是否相關,對不起。

變更:

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

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

兩件事情:

  1. 不要在ASP.NET中使用全局連接(至少不要將它們設為靜態),因為它是多線程環境。 而是總是始終使用using-statement盡快關閉連接。
  2. if(!IsPostback)檢查放入Page_Load ,當EnableViewState設置為true (默認值)時,不要在回發時對控件進行數據綁定。 否則事件將不會被觸發,並且值(如DropDownList-SelectedIndex )將被覆蓋。

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));
        }
    }
}

OnPageLoad CategoryDropDownList.Items.FindByText(categoryName).selected = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM