回发后加载值的问题

时间:2014-03-21 10:54:46

标签: asp.net vb.net postback viewstate dynamic-controls

我有一个工具栏,其中包含用户可以启动的不同操作。在用户界面中,它看起来像:

User interface

如果我按下" Ok"按钮该值在后端不可知。我的代码结构如下:

操作的配置

<Action Id="MyAction" Name="Action with Form">
    <Form>
        <asp:TextBox xmlns:asp="System.Web.UI.WebControls" ID="txtValue" runat="server" />
    </Form>
</Action>

ASPX-文件

<asp:Content ContentPlaceHolderID="cphToolbar" Runat="Server">
    <asp:PlaceHolder ID="plhToolbar" runat="server" />
</asp:Content>

VB-File到ASPX文件

Partial Class Form
    Inherits UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.plhToolbar.Controls.Add(Me.CreateActionToolbar(<XML configuration element for the toolbar>))
    End Sub
End Class

页面基类

Namespace UI
    Public MustInherit Class Page
        Inherits System.Web.UI.Page

        Protected Overridable Function CreateActionToolbar(source As XmlElement) As Interfaces.IActions
            Dim oAction As Interfaces.IActions = Me.LoadControl("~/Controls/Toolbar/Actions.ascx")

            For Each element As XmlElement In source.SelectNodes("node()").OfType(Of XmlElement)()
                Dim NewItem As New Controls.ActionItem

                'set settings for the toolbar element

                'add fields to form
                For Each Item As XmlNode In element.SelectNodes("Form/node()", oNamespaceManager)
                    If (TypeOf Item Is XmlElement) Then
                        If (NewItem.Fields Is Nothing) Then NewItem.Fields = New List(Of XmlElement)

                        NewItem.Fields.Add(Item)
                    End If
                Next

                oAction.Items.Add(NewItem)
            Next

            Return oAction
        End Function
    End Class
End Namespace

操作用户控件

Partial Class Actions

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each Item As ActionItem In Me.Items
            'set settings for action

            If (Not (Item.Fields Is Nothing)) Then
                Dim oPanel As New Panel
                oPanel.ID = "dlgActionPanel" & Me.dlgAction.Controls.Count
                Me.dlgAction.Controls.Add(oPanel)

                Dim oFields As New Panel
                oFields.ID = oPanel.ID & "_Controls"

                For Each Field As XmlElement In Item.Fields
                    Dim oControl As System.Web.UI.Control = Nothing

                    Try
                        oControl = Me.ParseControl(Field.OuterXml)
                    Catch ex As Exception
                        oControl = New LiteralControl("<font style=""color:red;"">" & ex.Message & "</font>")
                    End Try

                    oFields.Controls.Add(oControl)
                Next

                Dim pnlResult As New Panel
                Dim btnOk As New Button
                btnOk.ID = "btnOk_" & oPanel.ID
                AddHandler btnOk.Click, AddressOf Ok_Click
                btnOk.Attributes.Add("onclick", "ShowWaitDialog();")
                btnOk.Attributes.Add("ItemId", NewAnchor.Attributes("ItemId"))
                btnOk.UseSubmitBehavior = False
                btnOk.Text = Me.AcceptDialogText
                pnlResult.Controls.Add(btnOk)
                Dim btnCancel As New Button
                btnCancel.Attributes.Add("onclick", "ShowWaitDialog();$('#" & oPanel.ClientID & "').dialog('close');CloseWaitDialog();return false;")
                btnCancel.Text = Me.CancelDialogText
                pnlResult.Controls.Add(btnCancel)
                oPanel.Controls.Add(oFields)
                oPanel.Controls.Add(pnlResult)

                Dim strMessageControlId As String = oPanel.ClientID
                strClientScript &= "$(""#" & strMessageControlId & """).dialog({" & NewLine
                strClientScript &= "    bgiframe:true, " & NewLine
                strClientScript &= "    autoOpen:false, " & NewLine
                strClientScript &= "    modal:true, " & NewLine
                strClientScript &= "    closeOnEscape:false, " & NewLine
                strClientScript &= "    width:600, " & NewLine
                strClientScript &= "    height:450, " & NewLine
                strClientScript &= "    minWidth:450, " & NewLine
                strClientScript &= "    minHeight:300, " & NewLine
                If (Not (Item.Description Is Nothing)) Then strClientScript &= "    title:'" & Item.Description & "', " & NewLine
                strClientScript &= "    open: function(event, ui) { $("".ui-dialog-titlebar-close"").hide(); } " & NewLine
                strClientScript &= "});" & NewLine

                If (String.IsNullOrEmpty(NewAnchor.Attributes("onclick"))) Then NewAnchor.Attributes.Add("onclick", String.Empty)
                NewAnchor.Attributes("onclick") &= "$('#" & oPanel.ClientID & "').dialog('open');"
            End If
        Next
    End Sub

    Private Sub Ok_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim oDialog As Panel = Nothing
        If (Not (String.IsNullOrEmpty(sender.ID))) Then oDialog = Me.dlgAction.FindControl(sender.ID.Substring(sender.ID.IndexOf("_") + 1) & "_Controls")

        Dim oAction As ActionItem = Items.Find(Function(item) item.ItemId = sender.Attributes("ItemId"))

        If (Not (oDialog Is Nothing)) Then
            For Each Field As XmlElement In oAction.Fields
                Dim oControl As WebControl = Nothing

                If (Not (Field.SelectSingleNode("@Id|@ID") Is Nothing)) Then oControl = oDialog.FindControl(Field.SelectSingleNode("@Id|@ID").Value)

                If (Not (oControl Is Nothing)) Then
                    Dim oParameter As SqlClient.SqlParameter = oAction.Parameters.Find(Function(item) item.ParameterName = "@" & oControl.ID.Substring(3))

                    If (Not (oParameter Is Nothing)) Then
                        Select Case oControl.GetType.ToString
                            Case GetType(TextBox).ToString
                                'After postback the value is empty!!!
                                If (Not (String.IsNullOrEmpty(CType(oControl, TextBox).Text))) Then oParameter.Value = CType(oControl, TextBox).Text
                            'more controls
                            Case Else
                        End Select
                    End If
                End If
            Next
        End If
    End Sub
End Class

TextBox的值在PostBack之后为空而未设置因View State而未设置的错误在哪里?

感谢您的回复。

1 个答案:

答案 0 :(得分:0)

我可以解决它。问题是jQuery对话框没有PostBack保存。解决方案是

$("#dialog").dialog({
    appendTo:'form:first'
});