2011-11-04 57 views
1

我一直在試驗這個,試圖瞭解頁面生命週期幾個小時。我完全被困在這一點上:'(任何幫助將非常感激在動態填充的CheckBoxList中預先選擇項目

以下是我的方法我的_PrefillWizard方法實際上適用於我的頁面上的所有控件,除了那些動態填充的控件。CheckBoxList s是這樣的,因爲它們是根據我的數據庫中的表中的可選值自動生成的。我已經在我的標記中擁有此控件,因此...我如何在運行其他代碼之前強制執行此操作並準備就緒?

<asp:CheckBoxList ID="MyLanguagesCBL" runat="server" RepeatDirection="Vertical" 
    RepeatColumns="4" Width="100%" DataSourceID="LanguagesSDS" 
    DataTextField="Language" DataValueField="ID"> 
</asp:CheckBoxList> 
<asp:SqlDataSource ID="LanguagesSDS" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
    SelectCommand="select ID, [Language] from LanguagesListTable" 
    DataSourceMode="DataReader"> 
</asp:SqlDataSource> 

顯然,這些特定的控件尚未生成,或者在調用我的_PrefillWizard時導致沒有任何CheckBox被預先生成-填充。 如何在調用預填方法之前讓它們生成?謝謝;)

事情發生的點擊事件。

'On "Load" Button click, clears a Wizard control and Prefills it with the 
' data from the clicked record based on the primary key stored in SessionState. 
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand 
    If e.CommandName = "Select" Then 
     Session.Add("ClickedPrimaryKey", GridView2.DataKeys(e.CommandArgument).Value.ToString) 
     'This is the main function which calls "_SetCheckBoxes" among others. 
     _PrefillWizard(Session.Item("ClickedPrimaryKey")) 
    End If 
End Sub 

這是我來填充我的CheckBoxList控制功能,但它顯然取決於是否有已加載頁面上的控制。

'Parse a CSV String from the Database back into CheckBoxList Selections 
Protected Sub _SetCheckBoxes(ByRef cbl As CheckBoxList, ByRef dt As DataTable, ByVal row As Integer, ByVal csv As String) 
    'Do something if there is a value in the cell 
    If Not IsDBNull(dt.Rows.Find(row)(csv)) Then 
     For Each item As ListItem In cbl.Items 
      item.Selected = False 'Reset the checkbox first 
      'Then, if the ID value of the checkbox corresponds to a value 
      ' in the CSV string, then tick the checkbox. 
      If csv.Contains(item.Value) Then 
       item.Selected = True 
      End If 
     Next 
    End If 
End Sub 

回答

0

你們可能會得到一個解決辦法,但答案非常明顯,我甚至沒有抓住這一切。當然,我也專注於其他約10項功能。 <

下面是答案。您會注意到,在我的_SetCheckBoxes方法中,我首先檢查確保特定csv字符串的值存在,但是請注意,我未能實際將其設置爲相應的值字符串。這不是直接顯而易見的,但我實際上通過的csv是我的DataTable中實際存在字符串的列的名稱。

csv = dt.Rows.Find(row)(csv).ToString 

該行在我的IsDBNull檢查後立即添加,解決了我的問題。 :)

0

這是我們以前在類似的情況下使用一個潛在的想法,和一個:因爲頁面初始化事件單擊處理之前觸發

,實際上你可以檢查Request.Form("__EventTarget"),如果它是你的行單擊,將新的CheckBoxList添加到適當的容器(頁面,選項卡等)。

如果您要添加多個控件,您必須確保您追蹤頁面中某處的添加項目,以便每次啓動Page Init時都可以自動重新創建它們。

+0

我迷路了。 :(目前我有一個預填充的按鈕單擊表單,在該表單上生成了我動態填充的CheckBoxList,我相當確定問題是事件以錯誤的順序觸發,而我不能爲我的生活弄清楚如何讓事情按照正確的順序發生。:( – Chiramisu

相關問題