JavaScript:__doPostBack 中的多个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14482939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: Multiple parameters in __doPostBack
提问by Gunnar
First of all, the only post (calling-multiple-dopostback-from-javascript) I found about this didn't help my problem, so I don't belive this post is a duplicate.
首先,我发现的唯一一个帖子(调用-multiple-dopostback-from-javascript)对我的问题没有帮助,所以我不相信这篇文章是重复的。
I have this JavaScript function in my ASPX webpage that includes a __doPostBack function:
我的 ASPX 网页中有这个 JavaScript 函数,它包含一个 __doPostBack 函数:
function OpenSubTable(bolID, controlID) {
// code
__doPostBack('UpdatePanelSearch', bolID);
// more code
}
Works perfectly and I can get the value of bolID into my code behind like this:
完美运行,我可以像这样将 bolID 的值放入我的代码中:
protected void UpdatePanelSearch_Load(object sender, EventArgs e)
{
var bolID = Request["__EVENTARGUMENT"];
// code
}
The problem is, that I have to pass 2 different values through the postback. Are there any simple solutions to this? Obviously something like this doesn't work:
问题是,我必须通过回发传递 2 个不同的值。是否有任何简单的解决方案?显然,这样的事情是行不通的:
function OpenSubTable(bolID, controlID) {
// code
__doPostBack('UpdatePanelSearch', bolID, controlID); // not that simple, i'm afraid :(
// more code
}
Any help would be most welcome.
任何帮助将是最受欢迎的。
Regards, Gunnar
问候, 贡纳尔
采纳答案by jbabey
You could pass the two values as one JSON string:
您可以将这两个值作为一个 JSON 字符串传递:
function OpenSubTable(bolID, controlID) {
__doPostBack('UpdatePanelSearch', JSON.stringify({ bolID: bolID, controlID: controlID}));
}
And then parse it on the server:
然后在服务器上解析:
protected void UpdatePanelSearch_Load(object sender, EventArgs e)
{
SomeDTO deserializedArgs =
JsonConvert.DeserializeObject<SomeDTO>(Request["__EVENTARGUMENT"]);
var bolID = deserializedArgs.bolID;
var controlID = deserializedArgs.controlID;
}
public class SomeDTO
{
public string bolID { get; set; }
public string controlID { get; set; }
}
If you're using .Net >=4.0, I believe you can deserialize to a generic touple and avoid having to create SomeDTO. Edit: More information about deserializing to dynamic types.
如果您使用的是 .Net >=4.0,我相信您可以反序列化为通用元组,而不必创建SomeDTO. 编辑:有关反序列化为动态类型的更多信息。
回答by andleer
Consider placing your data in server side hidden fields and then reading that data after your postback.
考虑将您的数据放在服务器端隐藏字段中,然后在回发后读取该数据。
<asp:HiddenField id="Data1HiddenField" runat="server" />
<asp:HiddenField id="Data2HiddenField" runat="server" />
Your client script should include the ClientID values to handle server side naming container modifications. Using the <%= expression %>syntax (Expression Builder) requires that your script (or at least this part of the script) be maintain within your .aspxfile. If you maintain your JavaScript in external files, you can "register" a simple function that gets called by your main JavaScript to move the data and compose that function server side along with the required ClientIDs. See ClientScriptManager.RegisterClientScriptBlock().
您的客户端脚本应包含 ClientID 值以处理服务器端命名容器修改。使用<%= expression %>语法 (Expression Builder) 要求您的脚本(或至少脚本的这一部分)在您的.aspx文件中维护。如果你在外部文件中维护你的 JavaScript,你可以“注册”一个简单的函数,它被你的主 JavaScript 调用以移动数据并将该函数与所需的ClientIDs. 见ClientScriptManager.RegisterClientScriptBlock()。
var data1 = "data value 1";
var data2 = "data value 2";
$("#<%= Data1HiddenField.ClientID %>").val(data1);
$("#<%= Data2HiddenField.ClientID %>").val(data2);
Your server side code then looks like this:
您的服务器端代码如下所示:
string data1 = Data1HiddenField.Value;
string data2 = Data2HiddenField.Value;
There are certainly other techniques to passing multiple data values but I have found this to be both simple and easy to maintain. You can pass all kinds of data and encode it using JSON if needed.
当然还有其他技术可以传递多个数据值,但我发现这既简单又易于维护。如果需要,您可以传递各种数据并使用 JSON 对其进行编码。
回答by WraithNath
I have used multiple parameters before by building and splitting a string.
我之前通过构建和拆分字符串使用了多个参数。
eg
例如
string args = String.Format("{0};{1}", bolID, ControlID);
You can then pass this in to the arguments for the postback, and when checking for the postback arguments just split the string based on your speration character (in this case ';')
然后,您可以将其传递给回发的参数,并且在检查回发参数时,只需根据您的 speration 字符拆分字符串(在本例中为 ';')

