Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

javascript - is there a way to hold the values? - lost in postback

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" />

thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

In your code behind:

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

In your aspx file:

$(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.

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" />

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;
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...