有没有办法来保存价值? - 迷失在回发 [英] is there a way to hold the values? - lost in postback

查看:181
本文介绍了有没有办法来保存价值? - 迷失在回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择使用作为一个DropDownList的国家/国家

i have two select using as a dropdownlist for country/state

一切正常,如我所料,但是当我做一回发,然后我失去了从值以上<选择...> 拿什么来留住最好的方法的价值观和你能告诉我一个例子好吗?

everything works as i expected but when i do a postback then i lost the values from the above <select...> what is the best way to retain the values and can you show me with an example please?

<asp: Button ID="Button1" runat="server" 
onclick="Button1_Click" Text="PostBack" />

感谢。

推荐答案

如果您使用的是ASP.NET与一些jQuery,你可以在后置的隐藏字段的值回。然后在$(文件)。就绪(),你刚才读的隐藏字段的值。

If you're using ASP.NET with some jQuery, you could set the value of a hidden field in the post back. Then in the $(document).ready() you just read that value from the hidden field.

在你的$ C $落后C:

In your code behind:

protected void Button1_Click(object sender, EventArgs e)
{
  this.countryField.Value = "WhateverValueYouWantToPersist";
}

在你的aspx文件:

$(document).ready(function(){
  var persistedValue = <% this.countryField.ClientID %>;
  // do something...
});

<asp:HiddenField runat="server" ID="countryField" />

更新:

我想出了一个好一点的解决方案。我希望你只是想捕捉选定值构成的选择列表,并不需要坚持举国/状态集合。

Update:

I came up with a little better solution. I hope you only want to capture the selected values form the select lists and don't need to persist the whole country/state collection.

我想设置我的aspx页面像这样:

I would setup my aspx page as so:

<asp:DropDownList runat="server" ID="countryField" />
<asp:DropDownList runat="server" ID="stateField" />

<asp:Button runat="server" ID="button1" OnClick="button1_click" OnClientClick="return clientSideClick()" />

<asp:HiddenField runat="server" ID="hiddenCountry" />
<asp:HiddenField runat="server" ID="hiddenState" />

在我的jQuery我想有一个函数来处理一下,捕捉选定值(S),并设置为隐藏字段的值:

In my jQuery i would have a function to handle that click, which captures the selected value(s) and sets the value for the hidden fields:

function clientSideClick() {
    var state = $("#<%= this.stateField.ClientID %> :selected").val();
    $("#<%= this.hiddenState.ClientID %>").val(state);

    var country = $("#<%= this.countryField.ClientID %> :selected").val();
    $("#<%= this.hiddenCountry.ClientID %>").val(country);
}

然后在你的服务器端的按钮后回到事件,你可以捕捉到隐状态的值,做任何你需要从那里做的:

Then in your server side button post back event, you could capture the value of the hidden states and do whatever you need to do from there:

protected void button1_click(object sender, EventArgs e)
{
    string stateValue = this.hiddenState.Value;
    string countryValue = this.hiddenCountry.Value;
}

如果你确实想重新选择pviously选择国家/州对,然后在您的jQuery code中的$ P $载入国家和使用国的AJAX程序中,previously选定的值仍然会在该隐藏字段,并从那里你可以使用的值。

If you did want to re-select the previously selected Country/State pair, then after your jQuery code loads the Countries and States using your ajax routines, the previously selected values will still be in that hidden field and you can use the values from there.

这篇关于有没有办法来保存价值? - 迷失在回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆