问题
I have a textbox and this is how I assigned value into it
var start = moment().subtract(6, 'days');
var end = moment();
$('#datePicker').daterangepicker({
timepicker: false,
startDate: start,
endDate: end,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start,end);
function cb(start, end) {
$('#datePicker').html(start + ' - ' + end);
}
My interface
As u can see I choose the date value (04/04/2020 - 04/04/2020) and click the button it will retrieve data from database and refresh the page, after refresh, the textbox value will change back to 03/29/2020 - 04/04/2020 (it will use a week ago date as default).Btw this textbox is Asp.net textbox
Is there any way that I can remain the textbox value after the refreshed?
Do let me know if you need more information
回答1:
if this line cb(start,end);
overwrite your text box on post back, then do that.
Add on aspx page one literal to render the variable isPostBack
as javascript.
<asp:Literal runat="server" ID="txtExtraScriptVariables" EnableViewState="false" />
Then on code behind render it.
txtExtraScriptVariables.Text
= string.Format("var isPostBack = {0};", IsPostBack.ToString().ToLower());
and now on your script use it as
if(!isPostBack)
cb(start,end);
来源:https://stackoverflow.com/questions/61030630/remain-textbox-value-after-the-page-refreshed