2017-04-07 39 views
0

私はjQueryの日付ピッカーを持っており、コードは以下の通りです:JQuery UIインラインDatePickerは、ASP.NETポストバック後に選択した日付を失います。

<div id="test"></div> 
<asp:TextBox ID="txtRDate" runat="server" Width="120px" ClientIDMode="Static" AutoPostBack="True" OnTextChanged="txtRDate_TextChanged"> 
</asp:TextBox> 

Javascriptのコードは次のとおりです。

<script type="text/javascript"> 
$('#test').datepicker({ 
dateFormat: 'yy/mm/dd', 
minDate: new Date(2017, 1, 1), 
defaultDate: new Date(), 

onSelect: function(date, obj){ 
$('#txtRDate').val(date); //Updates value of date 
$('#txtRDate').trigger('change'); 
} 
}); 
</script> 

テキストボックスのためのTextChanged日付ピッカーから選択した日付が解雇とポストバックを行うと。背後

C#コード:

 protected void txtRDate_TextChanged(object sender, EventArgs e) 
    { 
     GetData(txtRDate.Text); 
    } 

jQueryのUIのインライン日付ピッカーは、ASP.NETのポストバック後に、選択した日付を失い、どのようにそれを維持するには?

ありがとうございます。

enter image description here

+0

私はあなたのポストにjsfiddleのリンクを追加したあなたは正確に何を説明することができますそれで間違っていますか? –

+0

@ Vijendra Kulhade自動ポストバックイベント(txtRDate_TextChanged)の後に選択された日付の値でjquiry datepickerを保持する方法は? –

+0

日付ピッカーがテキストボックスと異なるオブジェクトであるのはなぜですか? #testを#txtRDateに変更し、aspxページに正常に投稿しました。 – Bindrid

答えて

0

私はいくつかのマイナーな変更を加えて、それはすべての私のテスト環境で動作します。
moments.js(http://momentjs.com)libを追加し、テキストボックスが空でない場合は、現在の日付に日付を初期化するために使用しました。

jQuery、特にターゲット要素に若干の変更を加えました。 2番目のテキストボックスが追加されたのは、サーバーが接続するたびに連結1で表示されたことだけです。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="LaptopWeb.test" %> 

    <!DOCTYPE html> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title></title> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
     <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"/> 
     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       if ($("#txtRDate").val() == "") { 
        $("#txtRDate").val(moment().format("YYYY/MM/DD")) 
       } 
       $('#txtRDate').datepicker({ 

        dateFormat: 'yy/mm/dd', 
        minDate: new Date(2017, 1, 1), 
        onSelect: function(date, obj){ 
         $('#txtRDate').trigger('change'); 
        } 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
     <div> 
      <asp:TextBox ID="txtRDate" AutoPostBack="true" runat="server"></asp:TextBox> 
      <asp:TextBox ID="txtCount" runat="server"></asp:TextBox> 
     </div> 
     </form> 
    </body> 
    </html> 

サーバ側:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 

    namespace LaptopWeb 
    { 
     public partial class test : System.Web.UI.Page 
     { 
      protected void Page_Load(object sender, EventArgs e) 
      { 
       txtCount.Text = txtCount.Text + " 1"; 
      } 
     } 
    } 
0

そのいくつかのマイナーな変更で働いていた:

<script type="text/javascript"> 
 
    $('#test').datepicker({ 
 
     dateFormat: 'yy/mm/dd', 
 
     minDate: new Date(2017, 1, 1), 
 
     defaultDate: $('#txtRDate').val(), 
 
     onSelect: function (date, obj) { 
 
      $('#txtRDate').val(date); //Updates value of date 
 
      //Add the value to hidden field 
 
      $('#HiddenField1').val(date); 
 
      $('#txtRDate').trigger('change'); 
 
      
 
      
 
     } 
 
     
 
    }); 
 

 
    $(document).ready(function() { 
 
     //Assign the value from hidden field to textbox 
 
    onLoad: $('#txtRDate').val($('#HiddenField1').val()); 
 
    }); 
 
</script>

関連する問題