2012-12-10 27 views
0

確定在數據綁定下拉列表中選擇設定值,所以我有一個是最初從數據庫中填充的下拉列表。現在基於數據表,我希望選定的值等於數據庫中的文本。無論我做什麼,它只顯示「---選擇一個---」這是我手動添加到下拉列表項目列表中的唯一項目,以顯示默認值,如果我拉的值爲空(或這就是我想要的要做)如何從數據表中

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

DropDownList in aspx頁面。

<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命令。

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

+1

你能爲我們提供完整的凱德?你確定你的dropdownlist的數據綁定是在你的page_load之前完成的嗎? – GeorgesD

+0

總是選擇第一行故意? –

+0

請張貼填充列表 –

回答

2

兩件事情:

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

The survey_desc填充備忘錄沒有問題,但該類別下拉列表還沒有做任何事情 – BasharKH

+0

@BasharKH:?你真的需要使用'SqlDataSource'控制(公關我不會用它但是用於演示頁面)而是在'CategoryDropDownList.SelectedIndex'部分之前的'if(!IsPostBack)'代碼中手動綁定它。 –

+0

我只是這樣做,它的工作CategoryDropDownList.SelectedValue = DT.Rows [0] [「Category_Name」]。ToString(); – BasharKH

3

變化:

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

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

的onPageLoad CategoryDropDownList.Items.FindByText(類別名).selected = TRUE;