0

我是 jQuery 新手,有一些问题。我创建了一个服务器 Web 控件(它实现了一个webControl),并且想知道在事件触发时如何刷新控件。换句话说:

  1. 我有一个具有下拉列表的服务器控件。
  2. 当用户进行选择时,我想引起部分回发并重新加载控件(设置一些可以由使用控件的页面读取的公共属性值)。

这可以通过使用 jQuery 来完成,还是我仍然需要更新面板?

这是我的服务器控件:

<Assembly: WebResource("com.myControls.WebControls.miniActiveDirectorySearchStyle.css", "text/css", PerformSubstitution:=True)> 
Namespace com.myControls.WebControls
    < _
    System.Drawing.ToolboxBitmap(GetType(Button), "myControls.WebControls.ActiveDirectorySearch.bmp"), ToolboxData("<{0}:miniActiveDirectorySearch ID='miniActiveDirectorySearch{0}'  runat=""server""> </{0}:miniActiveDirectorySearch>"), _        ClientCssResource("com.myControls.WebControls.miniActiveDirectorySearchStyle.css")> _
Public Class miniActiveDirectorySearch
    Inherits WebControl
    Public ddlValue As New TextBox
    Public ddlText As New TextBox

    Private tblMainLayout As HtmlTable
    Private divControlContainer As New HtmlGenericControl()
    Private modalExtender As New ModalPopupExtender()
    Private tbSearchUser As New TextBox
    ''' <summary>
    '''X Offset for modal dialog
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Description("Mouser Over Highlight Color")> Public Property mouseOverColor As String = "#9db3d9"

    ''' <summary>
    '''X Offset for modal dialog
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Description("Drop Down List Width")> Public Property ddlWidth As String = "100"

    Private Sub attachWebResources()

        Dim styleLink As String = "<link rel='stylesheet' text='text/css' href='{0}' />"
        Dim location As String = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "myControls.WebControls.miniActiveDirectorySearchStyle.css")
        Dim styleInclude As New LiteralControl([String].Format(styleLink, location))
        DirectCast(Page.Header, HtmlControls.HtmlHead).Controls.Add(styleInclude)

        ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "com.myControls.ActiveDirectory.WebControls.jquery-1.4.1.min.js")
        ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "com.myControls.ActiveDirectory.WebControls.miniActiveDirectorySearch.js")


    End Sub
    Protected Overrides Sub CreateChildControls()
        createDynamicControls()
        MyBase.CreateChildControls()
    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        attachWebResources()
        MyBase.OnInit(e)
    End Sub


    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
    End Sub

    Private Sub createDynamicControls()
        Controls.Clear()

        Try
            tblMainLayout = buildMaintable()
            Controls.Add(tblMainLayout)
        Catch ex As Exception
            Throw New ApplicationException("Exception Occurred Creating Control.", ex.InnerException)
        End Try
    End Sub
    Private Function buildMaintable() As HtmlTable
        tblMainLayout = New HtmlTable
        Dim divImage As New HtmlGenericControl("div")
        ddlText = New TextBox
        ddlValue = New TextBox

        With ddlText
            .ID = "ddlText"
        End With

        With ddlValue
            .ID = "ddlValue"
        End With
        With divControlContainer
            .ID = Me.UniqueID & "_divSearchResults"
            .Attributes.Add("onClick", "hideMe(this)")
        End With
        With divImage
            .ID = Me.UniqueID & "_ImageDiv"
        End With
        With tblMainLayout
            .CellPadding = 0
            .CellSpacing = 0
            .Style.Add("border-collapse", "collapse")

            Dim tr As New HtmlTableRow
            With tr
                Dim td As New HtmlTableCell
                With td
                    With tbSearchUser
                        .ID = Me.UniqueID & "_tbSearchName"
                        .Attributes.Add("onKeyPress", "searchKeyPress(event, this.id, '" & divControlContainer.UniqueID & "', '" & mouseOverColor & "','" & ddlText.ID & "','" & ddlValue.ID & "')")
                        .Attributes.Add("onmouseover", "document.getElementById('" & divImage.ID & "').className='DivSearchMouseOverMini', tbMouseOver(this)")
                        .Attributes.Add("OnmouseOut", "document.getElementById('" & divImage.ID & "').className='divSearchDefaultMini',tbMouseOut(this)")
                        .Width = ddlWidth
                        .Attributes.Add("onClick", "showSearchResults('" & divControlContainer.UniqueID & "')")
                        .CssClass = "tbSearchDefault"
                    End With
                    .Controls.Add(tbSearchUser)


                    .Controls.Add(ddlText)
                    .Controls.Add(ddlValue)

                End With

                .Controls.Add(td)

                td = New HtmlTableCell
                With td

                    With divImage
                        .Attributes.Add("class", "divSearchDefaultMini")
                        .Attributes.Add("onmouseover", "this.className='DivSearchMouseOverMini',document.getElementById('" & tbSearchUser.ID & "').className='tbSearchMouseOver'")
                        .Attributes.Add("onmouseout", "this.className='divSearchDefaultMini', document.getElementById('" & tbSearchUser.ID & "').className='tbSearchDefault'")
                        .Attributes.Add("onClick", "showSearchResults('" & divControlContainer.UniqueID & "')")
                    End With
                    .Controls.Add(divImage)
                End With
                .Controls.Add(td)

            End With
            .Controls.Add(tr)
            tr = New HtmlTableRow
            With tr
                Dim td As New HtmlTableCell
                With td
                    .ColSpan = 2
                    .Controls.Add(divControlContainer)
                End With
                .Controls.Add(td)
            End With
            .Controls.Add(tr)
        End With

        Return tblMainLayout
    End Function

End Class

此代码创建一个带有文本框和样式的服务器控件以模仿下拉列表。这样,我可以在下拉列表中添加一些功能。当用户对“下拉列表”或文本框进行更改时,我想引起部分回帖。我所有的 jQuery 都在另一个作为 Web 资源添加的文件中完成。

4

1 回答 1

0

为什么不对服务器页面使用 ajax 调用,这不会导致实际的回发。获取您需要的内容并在响应中的客户端上进行更新。或者,您也可以触发服务器代码来执行此操作。

jQuery ajax 真的很容易使用。只需在服务器上声明一个静态 Web 方法来处理您的请求应该完成的任何事情。

于 2013-04-23T17:45:55.620 回答