【问题标题】:Call a code-behind function with ajax?用ajax调用代码隐藏函数?
【发布时间】:2012-05-19 20:51:50
【问题描述】:

搜索了一下,没有找到明确的答案。

我有一个很大的 HTML 表格,里面有数字。 我有一个选择器(单选按钮列表),如果用户想在 $ 或几天内查看表格,可以单击它。

现在它工作得很好,但是页面会刷新,因为每次用户单击两个单选按钮之一时我都会调用一个代码隐藏函数 (RefreshTable),因为它会更改格式并需要由 RefreshTable 函数完成新的计算

。有什么方法可以在不使用 ajax 或其他东西刷新页面的情况下调用该函数? 该函数只有一个参数:ProjectID,它是用VB.NET编码的,我们使用的是ASP.NET

这是来自 .ASPX 页面的表格代码,它只是外壳程序,所有内容都是通过在 RadioButton 更改时调用的 VB.NET 方法添加的(autopostback=true),因此我们检查选择了哪个并执行 VB .NET 方法来填充表。 (函数代码如下)

注意:更改了某些列\var 名称,因为它是敏感信息,但您可以了解全局。

  <td>
    <asp:RadioButtonList  RepeatDirection="Horizontal" id="rdiolist" onclick="alert('hello');" runat="server" RepeatLayout="flow" AutoPostBack="true">
   <asp:ListItem selected="true"> $ </asp:ListItem>
   <asp:ListItem> Days </asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>

<br />

    <table id="tblBudgetRessourceVP" runat="server" class="ProjetTable ProjetTableHover">
    <thead>
    <tr>
    <th style="width:80px">COLUMN 1</th>
    <th style="width:120px">COLUMN 2/th>
    <th style="width:120px">COLUMN 3</th>
    <th style="width:120px">COLUMN 4</th>
    <th style="width:120px">COLUMN 5</th>
    <th style="width:120px">COLUMN 6</th>
    <th style="width:120px">COLUMN 7</th>
    <th style="width:120px">COLUMN 8</th>
    </tr>
    </thead>

    </table>

方法背后的代码,这就是我想在没有回发的情况下调用的方法,我们想删除每次页面刷新。我将仅发布该函数的一个示例,因为它非常重复,因为它对每一列都执行此操作。我用随机名称替换了一些变量名称,因为它是非常敏感的数据。

Private Sub FillTable(ByVal vProjetID As String)
    Dim sqlquery As String = "SELECT SUM(EFFORT_RESRC.NB_JP_PLANF) as Planifie, SUM(EFFORT_RESRC.NB_JP_DDC) as DDC, SUM(EFFORT_RESRC.NB_JP_REEL) as Reel, SUM(EFFORT_RESRC.NB_JP_RESTN) as RAF, " & _
                "SUM(EFFORT_RESRC.NB_JP_REVS) as Revise, SUM(EFFORT_RESRC.NB_JP_PROJT) as Projete, SUM(EFFORT_RESRC.ECART_REVS_PROJT) as Ecart,RESRC.ID_VP , VICE_PRESD.DE_VP, TA_COMPS.TAUX " & _
                "FROM EFFORT_RESRC INNER JOIN " & _
                "TA_COMPS ON EFFORT_RESRC.COMPOSANTEID = TA_COMPS.COMPOSANTEID INNER JOIN " & _
                "RESRC ON EFFORT_RESRC.NO_EMPLY = RESRC.NO_EMPLY INNER JOIN " & _
                "VICE_PRESD ON RESRC.ID_VP = VICE_PRESD.ID_VP " & _
                "WHERE EFFORT_RESRC.PROJETID = '" & vProjetID & "' AND EFFORT_RESRC.ANNEE = '" & dd_ressourceprojet_annee.SelectedValue & "' AND TA_COMPS.ANNEE = '" & dd_ressourceprojet_annee.SelectedValue & "' " & _
                "GROUP BY RESRC.ID_VP, VICE_PRESD.DE_VP, TA_COMPS.TAUX " & _
                "ORDER BY VICE_PRESD.DE_VP"

    Dim dtRessource As New DataTable
    Master.GetDataTable(dtRessource, sqlquery)

    While (tblBudgetRessourceVP.Rows.Count > 1)
        tblBudgetRessourceVP.Rows.RemoveAt(1)
    End While

    Dim tr As HtmlTableRow
    Dim td As HtmlTableCell


    For Each ressource As DataRow In dtRessource.Rows

        If ressource("DE_VP") <> curStrVP And curStrVP <> String.Empty Then
            tr = New HtmlTableRow

                td = New HtmlTableCell
                td.InnerHtml = curStrVP
                tr.Cells.Add(td)

                td = New HtmlTableCell
            td.Attributes.Add("class", "budget")
            If rdiolist.SelectedIndex = 0 Then // Check the selector, if $ or Days display
                td.InnerHtml = Format(curPlan, "### ### ### ### ### ##0.00$")
            Else
                td.InnerHtml = Format(curPlan, "####")
            End If
            totPlan += curPlan
            tr.Cells.Add(td)  // Add the cell to the table.

            td = New HtmlTableCell
            td.Attributes.Add("class", "budget")

            If rdiolist.SelectedIndex = 0 Then // Check if JP or $ is selected for display format.
                td.InnerHtml = Format(curDDC, "### ### ### ### ### ##0.00$")
            Else
                td.InnerHtml = Format(curDDC, "####")
            End if
            totDDC += curDDC
            tr.Cells.Add(td)

            td = New HtmlTableCell
            td.Attributes.Add("class", "budget")
            If rdiolist.SelectedIndex = 0 Then  // Check if JP or $ is selected for display format.
                td.InnerHtml = Format(curRevise, "### ### ### ### ### ##0.00$")
            Else
                td.InnerHtml = Format(curRevise, "####")
            End If
            totRevise += curRevise
            tr.Cells.Add(td)

谢谢大家。

【问题讨论】:

  • 你可以发布你已经拥有的代码吗?
  • 什么部分?函数背后的代码?还是 HTML ?
  • 我不知道“函数背后的代码”应该是什么,但您应该发布相关的 HTML 和 JS。
  • 你考虑过使用UpdatePanel吗?
  • 这是 ASP.NET 项目中的 VB.NET 代码,我会在主帖中发布一些代码。

标签: javascript asp.net html ajax vb.net


【解决方案1】:

抱歉,我的回答有点延迟,因为我很久没接触过 VB。

现在假设您有一个 div(可以是一个按钮或任何 html 元素)单击,您希望从服务器获取数据而不进行完整的回发。以下是 HTML 设置。

<div id="click">click</div>
<div id="dvTest"></div>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/json2.js" type="text/javascript"></script>

以下是我们将使用的 jquery 代码:

<script type="text/javascript">
    $(function () {
        $("#click").click(function () {
            var o = new Object();
            o.ProjectId = 1;
            var x = JSON.stringify(o);
            $.ajax({
                url: 'Default.aspx/GetData',
                type: 'POST',
                dataType: 'JSON',
                contentType: 'application/json;charset=utf-8;',
                data: x,
                success: function (data) {
                    data = JSON.parse(data);
                    var d=data.hasOwnProperty("d") ? data.d : data;
                    //as we are returning fully rendered table
                    //we can directly set html of container div
                    $("#dvTest").html(d);
                },
                error: function (a, b, c) {
                    alert(b);
                }
            });
        });
    });
</script>

这是您需要在代码隐藏文件(aspx.vb 文件)中编写的内容。这个用WebMethod 属性修饰的public shared 方法称为PageMethod。现在由于HtmlTable 对象无法自动序列化,我们需要使用HtmlWriter 将其呈现为StringBuilder 并将完整的HTML 返回到客户端:

<WebMethod()>
Public Shared Function GetData(ByVal ProjectId As String) As String

    Dim tbl As New HtmlTable
    Dim tr As HtmlTableRow
    Dim td As HtmlTableCell

    'instead of these loops you will polulate table rows/cells
    'based on the data returned in your data table
    For I As Integer = 1 To 5
        tr = New HtmlTableRow
        For j As Integer = 1 To 5
            td = New HtmlTableCell
            td.InnerHtml = "Cell " & I & j
            tr.Cells.Add(td)
        Next
        tbl.Rows.Add(tr)
    Next

    Dim sb As New StringBuilder
    Dim sr As New StringWriter(sb)
    Dim hr As New HtmlTextWriter(sr)
    tbl.RenderControl(hr)
    Return sb.ToString()
End Function

编辑:- ASP.Net 通过序列化 .Net 对象返回 JSON 对象。但是这种方法不适用于 HtmlTable 对象,因为它们没有实现 InnerHTML 并且会在那里引发异常。

【讨论】:

  • 很棒的信息,但我不确定我是否得到了关于呈现 html 并将其以字符串形式返回的最后一部分,你能帮我解释一下吗?谢谢你。
  • 另外,这一行有一个错误 var d=data.hasOwnProperty("d")data.d:data
  • 很抱歉我错过了? 那里的错误。我还试图解释为什么我们必须渲染 HtmlTable 对象。
  • 谢谢,我刚刚看到你的编辑,那么 HtmlTable 的选项是什么?
【解决方案2】:

使用 AJAX,您可以创建一个通用处理程序(假设您使用的是 VS 2008 或更高版本)。通用处理程序将具有 .ashx 扩展名而不是 .aspx 扩展名。本质上,它允许您控制输出,而 Web 表单 .aspx 具有 UI 组件和代码隐藏。 .ashx 文件基本上是空白的,并引用了您的代码隐藏。在您的代码隐藏中,您可以编写所需的代码以输出 AJAX 响应所需的内容。

【讨论】:

  • 您能详细说明一下吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多