好吧,看起来我必须回答我自己的问题。以下是人们需要的片段,如果你想按照我做的那样做。
希望它可以帮助别人,
干杯, .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