Is there a way to keep values? - lost in reverse gear

I have two options that are used as a drop-down list for a country / state

everything works as I expected, but when I do the postback, then I lost the values ​​from the above <select...>, which is the best way to save the values, and can you show me an example?

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

thanks.

0
source share
2 answers

If you use ASP.NET with some jQuery, you can set the value of the hidden field in the message. Then in $ (document) .ready () you just read that value from a 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:

. , /.

aspx :

<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 , :

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

, :

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

/, , jQuery ajax, - , .

+2

:

  • - " ajax", .
  • .
  • cookie.
+1

Source: https://habr.com/ru/post/1712272/


All Articles