[英]Call a code-behind function with ajax?
Searched a bit, couldn't find a clear answer. 搜索了一下,找不到明确的答案。
I have a big HTML table with numbers in it. 我有一个很大的HTML表格,里面有数字。 I have a selector (radiobuttonlist) that the user can click if he wants to see the table in $ or days. 我有一个选择器(radiobuttonlist),如果用户希望以$或天为单位查看该表,可以单击该选择器。
Right now it works perfectly but the page refreshes because i'm calling a code-behind function (RefreshTable) everytime the user clicks on one of the two radiobutton since it changes the format and needs new calculation done by the RefreshTable function 现在它可以正常工作,但是页面会刷新,因为每次用户单击两个单选按钮之一时,我都会调用代码隐藏函数(RefreshTable),因为它会更改格式并需要由RefreshTable函数完成新的计算
. 。 Is there any ways I can call that function without refreshing the page using ajax or something ? 有什么方法可以调用该函数而无需使用Ajax或其他刷新页面? The function has only one parameter : ProjectID, it's coded in VB.NET and we're using ASP.NET 该函数只有一个参数:ProjectID,它是在VB.NET中编码的,我们使用的是ASP.NET
Here's the table code from the .ASPX page, it's only the shell, everything is added thru a VB.NET method called when the RadioButton is changed (autopostback=true), so we check wich one is selected and execute the VB.NET method to populate the table. 这是.ASPX页面上的表代码,它只是外壳程序,所有内容都是通过更改RadioButton(autopostback = true)时调用的VB.NET方法添加的,因此我们检查选择了哪一个并执行VB.NET方法填充表格。 (code of the function is below) (功能代码如下)
note: changed some column\\var name since it's sensitive info, but you get the big picture. 注意:因为它是敏感信息,所以更改了某些column \\ 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>
The code behind method, thats what I wanna call without a postback, we want to remove every page refresh. 方法后面的代码,这就是我想要不回发的调用,我们希望删除每次刷新的页面。 I'll post just a sample of the function since it's pretty repetitive since it does it for every column. 我将仅发布该函数的一个示例,因为该示例非常重复,因为它针对每一列进行了重复。 I replaced some variables names with random name since it's pretty sensitive data. 我用随机名称替换了一些变量名称,因为它是非常敏感的数据。
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)
Thanks everyone. 感谢大家。
Sorry I'm there is a little delay to answer, as I haven't touched VB since so loooong. 抱歉,我的回答有点延迟,因为自从如此放心以来我没有接触过VB。
Now suppose you have a div (can be a button or any html element) on clicking which you want to bring data from server without making a full postback. 现在,假设您要单击一个div(可以是按钮或任何html元素),但要从服务器中引入数据而不进行完整的回发。 Following is the HTML setup. 以下是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>
Following is the jquery code that we will use: 以下是我们将使用的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>
This is what you need to write in your codebehind file (aspx.vb file). 这就是您需要在代码隐藏文件(aspx.vb文件)中编写的内容。 This public shared
method decorated with WebMethod
attribute is called PageMethod
. 这个用WebMethod
属性修饰的public shared
方法称为PageMethod
。 Now as HtmlTable
object cannot be automatically serialized we need to use an HtmlWriter
to render it to as StringBuilder
and return complete HTML to client side: 现在由于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
EDIT:- ASP.Net returns JSON objects by serializing .Net objects. 编辑: -ASP.Net通过序列化.Net对象返回JSON对象。 But this approach does not works with HtmlTable
objects as they do not implement InnerHTML
and it throws exception there. 但是,此方法不适用于HtmlTable
对象,因为它们未实现InnerHTML
并且在那里引发了异常。
Using AJAX you could create an Generic Handler (assuming you are using VS 2008 or up). 使用AJAX可以创建通用处理程序(假设您使用的是VS 2008或更高版本)。 A generic handler will have .ashx
extension instead of a .aspx
extension. 通用处理程序将具有.ashx
扩展名而不是.aspx
扩展名。 Essentially it allows you to control the output whereas a web form .aspx
has the UI component and the code-behind. 本质上,它允许您控制输出,而Web窗体.aspx
具有UI组件和背后的代码。 .ashx
files are essentially blank with aa reference to your code-behind. .ashx
文件本质上是空白的,并且引用了您的代码。 In your code-behind you can write the code you need an output what you need for your AJAX response. 在您的后台代码中,您可以编写所需的代码,此输出需要AJAX响应所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.