简体   繁体   中英

Textbox value is not getting changed on assigning it to a value in the c# code

This is the code for my Textbox (DepartureDateTB):

<asp:TextBox ID="DepartureDateTB"
             runat="server"
             CssClass="DepartureDateTB"
             ClientIDMode="Static"
             ontextchanged="DepartureDateTB_TextChanged"
             EnabledViewState="False"
             AutoPostBack="True">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1"
                      runat="server"
                      ControlToCompare="ArrivalDateTextBox"
                      ControlToValidate="DepartureDateTB"
                      ErrorMessage="*Departure to be greater"
                      ForeColor="Red"
                      Operator="GreaterThan">
</asp:CompareValidator>

JQuery for Date Picker:

$(function () {
            $("[id$='DepartureDateTB']").datepicker({ minDate: 0, maxDate: "+30D" });
            $("[id$='DepartureDateTB']").datepicker("option", "dateFormat", "dd/mm/yy");

    });
</script>  

On Page load, I am assigning a value to the textbox from database.

Then when the user changes it, there's a postback but the changed value is not retained.

Even when I tried assigning a Session variable to it and then equate it, all I get is an empty textbox.

Session["DepDateTB"] = (Request.Form[DepartureDateTB.UniqueID]).ToString();
                DepartureDateTB.Text = Session["DepDateTB"].ToString();

And when I position a break point and run it, when I hover over the Textbox the value of it is the changed date but the textbox in the front end is again empty.

The reason the date wasn't getting displayed was due to the following line in the jQuery code:

$("[id$='DepartureDateTB']").datepicker("option", "dateFormat", "dd/mm/yy");

Once the date format option was removed it worked fine.

I resorted to changing the date format in my c# code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM