在动态填充的 CheckBoxList 中预选项目

发布于 2024-12-13 21:14:28 字数 2239 浏览 1 评论 0原文

我一直在尝试这个并试图理解页面生命周期几个小时。我在这一点上完全被难住了:'( 任何帮助将不胜感激。

以下是我的方法。我的 _PrefillWizard 方法实际上适用于我页面上的所有控件,除了那些动态填充的控件。 CheckBoxList 之所以如此,是因为它们是根据我的数据库中的表中的可选值自动生成的,我的标记中已经有了这个控件,就像这样......我如何强制它加载并被加载。在运行我的另一个之前准备好 显然,

<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 被调用时,这些特定的控件尚未生成或出现在页面上,导致没有任何复选框被预先填充如何让它们生成。在调用我的预填充方法之前? 谢谢;)

启动事件的点击事件。

'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

I've been experimenting with this and trying to understand the Page Life Cycle for hours upon hours. I'm completely stumped at this point :'( Any help would be GREATLY appreciated.

Below are my methods. My _PrefillWizard method actually works for all the controls on my page except for those which are dynamically populated. The CheckBoxLists are such because they are automatically generated based on selectable values in a table in my DB. I already have this control in my markup like so... how do I force this to load and be ready before running my other code?

<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>

Obviously these particular controls are not yet generated or on the page by the time my _PrefillWizard has been called, resulting in none of the CheckBoxes being pre-filled. How do I make them generate before my prefill method is called? Thanks ;)

The click event that kicks things off.

'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

Here's my function for populating the my CheckBoxList control, but it obviously depends on the control having already been loaded on the page.

'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-12-20 21:14:28

这是一个潜在的想法,我们之前在类似情况下使用过:

因为 Page Init 事件在点击处理程序之前触发,所以您实际上可以检查 Request.Form("__EventTarget") 并,如果是您的行单击,请将新的 CheckBoxList 添加到适当的容器(页面、选项卡等)。

如果要添加多个控件,则必须确保在页面中的某个位置跟踪添加的项目,以便每次触发 Page Init 时都可以自动重新创建它们。

Here is a potential idea, and one that we have used before in a similar situation:

Because the Page Init event is fired before the click handler, you can actually examine Request.Form("__EventTarget") and, if it is your row click, add the new CheckBoxList to the appropriate container (page, tab, etc).

If you are adding multiple controls, you will have to be sure that you track the added items somewhere in the page so that they can be automatically re-created each time Page Init is fired.

如痴如狂 2024-12-20 21:14:28

你们可能会从中得到乐趣,但答案是如此明显,以至于我一直没有明白。当然,我也专注于大约 10 个其他功能>。<

答案如下。您会注意到,在我的 _SetCheckBoxes 方法中,我首先检查以确保特定 csv 字符串存在值,但还要注意,我未能实际设置它之后到相应的值字符串。这并不是立即显而易见的,但我实际上作为 csv 传递的是我的 DataTable 中字符串实际所在的列的名称。

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

在我的 IsDBNull 检查之后立即添加了该行,解决了我的问题。 :)

You guys will probably get a kick out of this, but the answer was so obvious that I didn't even catch it all this time. Of course I was preoccupied with about 10 other functions too >.<

Here's the answer. You'll notice that in my _SetCheckBoxes method, I first checked to ensure that a value existed for the particular csv string, but also notice, that I failed to actually SET it to the corresponding value string afterward. This isn't immediately apparent, but what I was actually passing in as csv was the name of the Column in my DataTable where the string actually lived.

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

That line added immediately after my IsDBNull check, fixed my problem. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文