remain TextBox value after the page refreshed

2020-04-14 09:27发布

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

enter image description here

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条回答
啃猪蹄的小仙女
2楼-- · 2020-04-14 09:56

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);
查看更多
登录 后发表回答