remain TextBox value after the page refreshed

左心房为你撑大大i 提交于 2020-05-09 05:58:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!