2011-11-04 100 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

相关问题