【问题标题】:jQuery and reloading server controljQuery 和重新加载服务器控件
【发布时间】:2013-04-23 17:16:07
【问题描述】:

我是 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 资源添加的文件中完成。

【问题讨论】:

  • 您可以在下拉菜单中使用.change() 事件,这将导致每次更改下拉菜单时触发一个事件。如果您有一些针对特定问题的代码,那肯定有助于获得准确的答案。
  • 太好了,我现在就发布我的代码。
  • 好的,刚刚添加了我的代码。如果您有任何问题,请告诉我。

标签: asp.net jquery web-controls


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2021-06-24
    • 2014-03-30
    相关资源
    最近更新 更多