好吧,看起來我必須回答我自己的問題。以下是人們需要的片段,如果你想按照我做的那樣做。
希望它可以幫助別人,
乾杯, .PD。
在您將用戶控件置於其中的頁面上,您需要捕獲提交updatepanel的點擊。
<script type="text/javascript">
$(subscribeClicks);
// for use on pages with updatepanels. once the panel has reloaded, the jquery
// events get slagged so we need to rebind them here.
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(subscribeClicks);
function subscribeClicks() {
// catch the search button before it (partially) posts back and send
// data
$('input[id*="btnSearch"]').click(function (e) {
// this function is in a script in your usercontrol
senddataFromUserControl('ThisPage.aspx/NameOfYourWebMethod');
});
}
</script>
在你的用戶控件,你需要senddataFromUserControl到表單中的數據AJAX來代碼隱藏。請注意隱藏元素接收會話密鑰的成功部分。還有async:false(感謝Kevin B)。
function senddataFromUserControl(url) {
var arr = new Array();
var ele = $('.jq_idt_selected');
for (var i = 0; i < ele.length; i++) {
arr.push({ Name: $(ele[i]).find('.jq_idt_path').text(), Value: $(ele[i]).find(':hidden').val() });
}
$.ajax({
type: "POST",
async: false,
url: url,
data: "{args:" + JSON.stringify(arr) + "}",
dataType: "text",
contentType: "application/json; charset=utf-8",
success: function (data) {
$('input[id*="hdnSessionKey"]').val($.parseJSON(data)["d"]);
},
error: function (data) {
alert(data.responseText);
}
});
}
在後面的代碼,建立了類接收名稱/值對(這一個在VB)
Public Class SearchArgs
Public Name As String
Public Value As String
End Class
而在C#:
public class SearchArgs {
public string Name;
public string Value;
}
然後編寫你的WebMethod (VB先)
<System.Web.Services.WebMethod()> _
Public Shared Function NameOfYourWebMethod(args As List(Of SearchArgs)) As String
' generate a session key for the client to pass back when the page postback occurs
Dim key As String = String.Format("IDT_{0:yyMMddhhmmss}", Now)
HttpContext.Current.Session(key) = args
Return key
End Function
他重新的C#版本:
[System.Web.Services.WebMethod()]
public static string NameOfYourWebMethod(List<SearchArgs> args)
{
// generate a session key for the client to pass back when the page postback occurs
string key = string.Format("IDT_{0:yyMMddhhmmss}", DateAndTime.Now);
HttpContext.Current.Session[key] = args;
return key;
}
最後在提交按鈕單擊,從會話中抓取額外的數據。
Dim o As Object = yourUserControl.FindControl("hdnSessionKey")
Dim hdn As HtmlInputHidden = CType(o, HtmlInputHidden)
If hdn IsNot Nothing Then
Dim key As String = hdn.Value
Dim filterValues As List(Of SearchArgs) = CType(Session(key), List(Of SearchArgs))
For Each filterValue As SearchArgs In filterValues
' do what you need to prep this for your data layer
Next
Session(key) = Nothing
End If
而在C#:
object o = yourUserControl.FindControl("hdnSessionKey");
HtmlInputHidden hdn = (HtmlInputHidden)o;
if (hdn != null) {
string key = hdn.Value;
List<SearchArgs> filterValues = List<SearchArgs>)Session[key];
foreach (SearchArgs filterValue in filterValues) {
// do what you need to prep this for your data layer
}
Session[key] = null;
}
選擇您喜歡的類型,從中提取的值,然後把他們......正是你嘗試過什麼?給我們一個你如何試圖自己解決這個問題的例子,可以幫助我們更好地理解你想要完成的事情。 –
你的ajax請求是否設置爲'async:false'?否則正常的按鈕處理程序將在ajax完成之前以及隱藏輸入更新之前返回true。 –
這解釋了爲什麼它只有當我通過它但沒有斷點時才工作:)謝謝。 – twelveFunKeys