好吧,看来我得回答我自己的问题了。如果你想像我一样做,这里是人们需要的片段。
希望它可以帮助某人,
干杯,.pd。
在您放置用户控件的页面中,您需要捕捉提交更新面板的点击。
<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;
}
然后编写您的网络方法(首先是 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;
}
最后在提交按钮中单击,从 Session 中获取额外的数据。
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;
}