页面刷新后仍保留TextBox值 [英] remain TextBox value after the page refreshed
问题描述
我有一个文本框,这就是我向其中分配值的方式
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);
}
我的界面
如您所见,我选择日期值( 04/04/2020-04/04/2020 ),然后单击按钮,它将从数据库中检索数据并刷新页面,刷新后,文本框的值将更改回 2020年3月29日-2020年4月4日(默认使用一周前的日期).此文本框为 Asp.net >文本框
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
推荐答案
如果此行 cb(start,end);
覆盖发回文本框,则执行此操作.
if this line cb(start,end);
overwrite your text box on post back, then do that.
在aspx页面上添加一个文字,以将变量 isPostBack
呈现为javascript.
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);
这篇关于页面刷新后仍保留TextBox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!