[英]Call a code-behind function with ajax?
搜索了一下,找不到明確的答案。
我有一個很大的HTML表格,里面有數字。 我有一個選擇器(radiobuttonlist),如果用戶希望以$或天為單位查看該表,可以單擊該選擇器。
現在它可以正常工作,但是頁面會刷新,因為每次用戶單擊兩個單選按鈕之一時,我都會調用代碼隱藏函數(RefreshTable),因為它會更改格式並需要由RefreshTable函數完成新的計算
。 有什么方法可以調用該函數而無需使用Ajax或其他刷新頁面? 該函數只有一個參數:ProjectID,它是在VB.NET中編碼的,我們使用的是ASP.NET
這是.ASPX頁面上的表代碼,它只是外殼程序,所有內容都是通過更改RadioButton(autopostback = true)時調用的VB.NET方法添加的,因此我們檢查選擇了哪一個並執行VB.NET方法填充表格。 (功能代碼如下)
注意:因為它是敏感信息,所以更改了某些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>
方法后面的代碼,這就是我想要不回發的調用,我們希望刪除每次刷新的頁面。 我將僅發布該函數的一個示例,因為該示例非常重復,因為它針對每一列進行了重復。 我用隨機名稱替換了一些變量名稱,因為它是非常敏感的數據。
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)
感謝大家。
抱歉,我的回答有點延遲,因為自從如此放心以來我沒有接觸過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
並且在那里引發了異常。
使用AJAX可以創建通用處理程序(假設您使用的是VS 2008或更高版本)。 通用處理程序將具有.ashx
擴展名而不是.aspx
擴展名。 本質上,它允許您控制輸出,而Web窗體.aspx
具有UI組件和背后的代碼。 .ashx
文件本質上是空白的,並且引用了您的代碼。 在您的后台代碼中,您可以編寫所需的代碼,此輸出需要AJAX響應所需的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.