0

警告!这个问题不适合装腔作势。我花了几天时间试图解决它。

我有一个Wizard大约有 5 个步骤的控件,每个都有几个控件,从非常简单到非常复杂,例如基于DropDownLists 和TextBoxes 的自定义 jQuery 组合框。每个步骤都有几个包装在一个控件中的PlaceHolder控件。

Wizard连同所有s 和他们的PlaceHolder孩子Controls都嵌套在 aView的 a 中MultiView。在同一页面上,我有另一个View样式像表单,但不是一个。此视图在 的每个步骤中PlaceHolder为每个 s 都有对应的 s 。PlaceHolderWizard

根据 ReferralUrl 我调用以下函数Wizard通过移动所有控件将视图从表单样式视图“切换”,然后按如下方式设置活动视图:

Protected Sub ToggleView() Handles ViewToggle.Click
    If Wizard_mv.ActiveViewIndex = 0 Then
        ViewToggle.Text = "Toggle Wizard View"
        fPH1.Controls.Add(wPH1)
        fPH2.Controls.Add(wPH2)
        fPH3.Controls.Add(wPH3)
        fPH4.Controls.Add(wPH4)
        fPH5.Controls.Add(wPH5)
        Wizard_mv.ActiveViewIndex = 1
    ElseIf Wizard_mv.ActiveViewIndex = 1 Then
        ViewToggle.Text = "Toggle Form View"
        wPH1.Controls.Add(fPH1)
        wPH2.Controls.Add(fPH2)
        wPH3.Controls.Add(fPH3)
        wPH4.Controls.Add(fPH4)
        wPH5.Controls.Add(fPH5)
        Wizard_mv.ActiveViewIndex = 0
    End If
End Sub

紧接着,我使用另一个函数来使用数据库中记录中的值预填充控件。在允许用户进行一些更改后,他们可以将更新的记录重新提交到数据库。问题是,如果我Wizard在切换之后而不是在切换之后这样做,它就可以正常工作。跟踪显示Control Tree在提交更新的记录时 是空的,因此我无法从此视图中获取用户输入/预填充的值。预填充效果很好,“选定”值都是正确的。单击提交后在 PostBack 上出现问题,它丢失了所有值和控件。

除非您完全理解我的问题并愿意提供帮助,否则请不要回答。我认为问题很好地存在于页面生命周期中。奇怪的是,当我从我的向导提交时,在 Page_Init 的回发中我加载了我的控件值,但是当我从我的表单视图提交时,既没有加载控件也没有加载它们的值。根据我的阅读,这是一个持久性问题。真的希望有一个相对简单的解决方案。

这是我的标记示例Wizard(为简洁起见,删除了所有样式):

<asp:WizardStep ID="WizardStep1" runat="server" Title="Product Info">
    <asp:PlaceHolder ID="wPH1" runat="server" ViewStateMode="Enabled">
        <asp:Table ID="ProductInfoTable" runat="server" Width="100%">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell><h3>Product Line</h3></asp:TableHeaderCell>
                <asp:TableHeaderCell><h3>Publication Type</h3></asp:TableHeaderCell>
                <asp:TableHeaderCell><h3>Request Type</h3></asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell>
                    <div class="ui-widget">
                        <!-- Autocomplete Combobox -->
                        <asp:DropDownList ID="productLine_ddl" runat="server" DataSourceID="productLineSDS" ViewStateMode="Enabled" DataTextField="Product" DataValueField="ID"></asp:DropDownList>
                        <asp:TextBox ID="productLine_cb" runat="server" EnableViewState="True"></asp:TextBox>
                        <button id="productLine_btn" type="button" title="Show All Items"></button>
                    </div>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="docType_ddl" runat="server" DataSourceID="docTypeSDS" DataTextField="DocType" DataValueField="ID"></asp:DropDownList>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="requestType_ddl" runat="server" DataSourceID="requestTypeSDS" DataTextField="RequestType" DataValueField="ID"></asp:DropDownList>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2">
                    <asp:MultiView ID="Attachment_mv" runat="server" ActiveViewIndex="0">
                        <!-- File Upload/Browsing Display -->
                        <asp:View ID="AttachmentUploadView" runat="server">
                            <h3 class="inlineH">Attach File: </h3>
                            <asp:FileUpload ID="AttachmentFile_btn" runat="server" />
                            <asp:Button ID="UploadFile_btn" runat="server" Text="Upload File" />
                        </asp:View>
                        <!-- File Attached Display -->
                        <asp:View ID="FileAttachedView" runat="server">
                            <h3 class="inlineH">Uploaded File: </h3>
                            <asp:Label ID="FileAttachedLabel" runat="server" Text="Label"></asp:Label>
                            <asp:Literal ID="FilesOnServer" runat="server" />
                        </asp:View>
                    </asp:MultiView>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </asp:PlaceHolder>
</asp:WizardStep>

我的页面生命周期事件,根据要求(为了您的方便,按时间顺序排列):)

Dim referrerPage As String
'Initialize Dynamic controls here. These are controls that need to be prefilled, etc.
Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
    'DO NOT PREFILL at this stage, as the Controls are not yet rendered and will cause errors.
    '  Use this section for detecting the "referrerPage" and missing "id" parameters when expected, etc.
    If Not IsPostBack Then
        ViewState.Clear()
    End If

    Try
        referrerPage = Right(Request.UrlReferrer.AbsolutePath, Len(Request.UrlReferrer.AbsolutePath) - Request.UrlReferrer.AbsolutePath.LastIndexOf("/"c) - 1)
    Catch ex As Exception
        If Not String.IsNullOrEmpty(Session.Item("referrerPage")) Then
            referrerPage = Session.Item("referrerPage")
        End If
    End Try

    If StrComp(referrerPage, "wizard.aspx") <> 0 And String.IsNullOrEmpty(Session.Item("referrerPage")) Then 'Initialize Session state to remember "referrerPage"
        Session.Add("referrerPage", referrerPage)
    End If
    If StrComp(referrerPage, "formdetails.aspx") = 0 Then
        If String.IsNullOrEmpty(Request.Params("id")) Then
            'This line checks for an expected "id" param value and if none exists, forwards the page back to "tcom.aspx"
            Response.Redirect(Request.UrlReferrer.AbsolutePath)
        Else
            ToggleView()
        End If
    End If
End Sub

'Prefill Dynamic controls here.
Private Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad
    If Not IsPostBack Then
        productLine_ddl.DataBind()
        docType_ddl.DataBind()
        requestType_ddl.DataBind()
        '...and several more DataBinds for each individual
        '   control in the wizard. Nothing more.
    End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'Benign code for querying the database to get User info for Page.User.Identity.Name
    End If
End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
    If Not IsPostBack Then
        'Here is completely benign code for hiding a couple controls.
    End If
End Sub

Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRenderComplete
    'PREFILL HERE when appropriate. This occurs after checking the "referrerPage" and ensuring a value for the "id" parameter.
    If Not IsPostBack Then
        Try
            If Not String.IsNullOrEmpty(Request.Params("id")) Then
                PrefillWizard(Request.Params("id"))
            Else : output.Text += "Source: " + Request.UrlReferrer.AbsolutePath
            End If
        Catch ex As Exception
        End Try
    End If
End Sub

对于任何可以提供帮助的人,我永远感激你。;)

4

1 回答 1

1

问题是页面的控制树在每次回发期间必须完全相同。这意味着您每次都必须添加所有控件,无论设置哪个ActiveViewIndex。最好是 inCreateChildControls或 page init。在该ToggleView功能中,您可以设置控件的可见性。

于 2011-11-22T22:29:19.860 回答