2010-07-21 85 views
0

我有我的代碼隱藏Page_Load函數中的下面的代碼,根據從sql數據查詢返回的記錄的名稱在detailsview中設置dropdownlist的默認選定值。ASP.Net dropdownlist內detailsview不在代碼隱藏設置

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Me.Page.Title = "Editing record" 

     'Perform dropdown list population operations 
     Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown") 
     If Page.IsPostBack = False Then 

      Dim ticket_ID As String = getDataKey(DetailsView1) 
      'Fetch Category ID 
      Dim sqlText As String = "SELECT TS_REQCATEGORY FROM USR_ITFAC WHERE (TS_ID = " + ticket_ID + ") " 
      Dim reqDataReader As SqlDataReader = GetDataReader(sqlText) 
      reqDataReader.Read() 

      Dim category_ID As String = reqDataReader(0) 

      'Fetch Category name and set as selected value in dropdown list 
      sqlText = "SELECT TS_NAME FROM TS_SELECTIONS WHERE (TS_ID = " + category_ID + ") " 
      reqDataReader = GetDataReader(sqlText) 
      reqDataReader.Read() 
      category_Name = reqDataReader(0) 
      'myDDL.DataBind() 
      myDDL.SelectedValue = category_Name 
     End If 

    End Sub 

我的問題是,當頁面加載的第一次,即使我的下拉列表設置選擇的值不會顯示出來,而是隻顯示在下拉列表我的默認名字。我嘗試在設置selectedvalue之前和之後綁定我的下拉列表,它在上面的示例代碼中註釋掉了,但似乎沒有做任何事情。

更新: 我設置數據源中的Web窗體如下:

DROPDOWNLIST:

<asp:DropDownList DataSourceID="ReqCategoryData" DataTextField="ReqCategory" DataValueField="ReqCategory" 
           ID="reqCategoryDropDown" runat="server" AppendDataBoundItems="true" AutoPostBack="true">                
          </asp:DropDownList> 

連接到數據源的幾行字下來:

<asp:SqlDataSource ID="ReqCategoryData" runat="server" ConnectionString="<%$ ConnectionStrings:TTPRODReportsQuery %>" 
     SelectCommand="SELECT TS_NAME AS ReqCategory FROM dbo.TS_SELECTIONS WHERE (TS_FLDID = 5299 AND TS_STATUS = 0) ORDER BY TS_NAME"> 
     </asp:SqlDataSource> 

UPDATE_2 : 好的,所以我在Page_Init函數的代碼隱藏中以編程方式實現了他的SQLDataSource,如下所示:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init 
     DetailsView1.DefaultMode = DetailsViewMode.Edit 

     ''Setup DropDownList SqlDataSource 
     ddlDataSource.ID = "ReqCategoryData" 
     Page.Controls.Add(ddlDataSource) 
     ddlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("TTPRODReportsQuery").ConnectionString 
     ddlDataSource.SelectCommand = "SELECT TS_NAME AS ReqCategory FROM dbo.TS_SELECTIONS WHERE (TS_FLDID = 5299 AND TS_STATUS = 0) ORDER BY TS_NAME" 
     Dim args As New DataSourceSelectArguments 
     ddlDataSource.Select(args) 
     ddlDataSource.DataBind() 

    End Sub 

之後我設置嘗試設置下拉列表的選定值如上。該頁面仍未設置所選值。

回答

1

或許檢查以下項目:

  • 確保你打電話myDDL.DataSource = reqDataReader或設置您的下拉列表的數據源的一些其他形式。
  • 確保下拉列表的DataTextFieldDataTextField設置正確。這可能是代碼隱藏或標記。
  • reqDataReader的第0個索引 - 你確定這是DataValueField的預期序號嗎?

也許是這樣的:

With myDDL 
    .DataTextField = "CategoryName" 
    .DataValueField = "CategoryID" ' or perhaps CategoryName as your value. 
    .DataSource = reqDataReader 
    .DataBind() 
    .SelectedValue = category_Name ' Name or ID; depends on what was set as DataValueField 
End With 

數據綁定 - 代碼隱藏與標記數據源

考慮選擇結合您的下拉列表一種風格 - 在後臺代碼或標記SqlDataSource的。兩者都會產生混淆,哪些綁定會贏 - 代碼隱藏或數據源。我認爲這是在這種情況下的SqlDataSource。

考慮從標記中刪除SqlDataSource,並創建一個代碼隱藏方法,其唯一目的是創建數據綁定。這是綁定應該發生的唯一地方。如果您需要追蹤缺陷或增強您的邏輯,那麼這是唯一一個參觀的地方。

Private Sub BindMyDropDown(Optional ByVal selectedValue as String) 
    With myDDL 
     .DataTextField = "CategoryName" 
     .DataValueField = "CategoryID" ' or perhaps CategoryName as your value. 
     .DataSource = LoadTicketReqCategory(TicketID) 
     .DataBind() 
     .SelectedValue = selectedValue ' Name or ID; depends on what was set as DataValueField 
    End With 

End Sub 

從你Page_Load(),檢查IsPostBack()和調用這個方法時,你不回發。

+0

我沒有看到這裏列出的數據源和數據綁定評論目前註釋掉(與單引號有)所以這看起來是發生了什麼事。 – 2010-07-21 15:45:10

+0

我在上面添加了一些細節,我實際上是在Web表單本身設置數據源。並且是p.cambell,我確定第0個索引包含我想要設置DataValueField的值。驗證我包含調試時的值。 – kingrichard2005 2010-07-21 15:52:31

0

myDDL.ClearSelection item = myDDL.Items。FindByValue(category_Name) item.selected = true

- 您也可以嘗試將SelectedIndex設置爲2,例如,以這種方式進行檢查。

0

問題可能是您在回發檢查之外分配了myDDL變量。

Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown") 
    If Page.IsPostBack = False Then 

應該

If Page.IsPostBack = False Then 
     Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown") 
相關問題