NetAdvantage asp.net ——使用WebDateChooser在用戶端驗證開始日期與結束日期

來源:互聯網
上載者:User
        對於日期選擇,我們經常使用 WebDateChooser控制項,當涉及到開始日期與結束日期的時候,就需要驗證日期的合法性,這種情況在開發過程中經常遇到,所以我們可以編寫一個通用的方法在用戶端就驗證日期的合法性,而不必等到提交到伺服器才進行驗證,方法如下:

public void BindCompareDateChoosers(Control userControl,Infragistics.WebUI.WebSchedule.WebDateChooser beginDateChooser, Infragistics.WebUI.WebSchedule.WebDateChooser endDateChooser)
        ...{
           
            string compareJS = "<script language=javascript>" +
               "function CompareDate(oCalendar, newValue, oEvent) " +
               "{" +
               "  var dateChooserBegin = igdrp_getComboById('" + beginDateChooser.ClientID + "'); " +
               "  var dateChooserEnd = igdrp_getComboById('" + endDateChooser.ClientID + "'); " +
               "  if(oCalendar==dateChooserBegin.Calendar)" + 
               "  {" +
               "     var dateBegin=newValue;" +
               "     var dateEnd = dateChooserEnd.getValue();" +
               "     var dateEndText = dateChooserEnd.getText();" +
               "     if(dateEndText!='---請選擇---' && dateEndText!='' && dateBegin>dateEnd)" +
               "     {" +
               "        alert('選擇日期區間時,起始日期不能晚於到期日!'); " +
               "        oEvent.cancel = true;" +
               "     }" +
               "  }" +
               "  if(oCalendar==dateChooserEnd.Calendar)" +
               "  {" +
               "     var dateBegin=dateChooserBegin.getValue();" +
               "     var dateEnd = newValue;" +
               "     var dateBeginText = dateChooserBegin.getText(); " +
               "     if(dateBeginText!='---請選擇---' && dateBeginText!='' && dateBegin>dateEnd)" +
               "     {" +
               "        alert('選擇日期區間時,起始日期不能晚於到期日!'); " +
               "        oEvent.cancel = true;" +
               "     }" +
               "  }" +
               "}" +
               "</script>";
            userControl.Page.ClientScript.RegisterStartupScript(userControl.GetType(), "CompareDate", compareJS);
            beginDateChooser.ClientSideEvents.CalendarValueChanging = "CompareDate";
            endDateChooser.ClientSideEvents.CalendarValueChanging = "CompareDate";

        }

我們可以在頁面或者控制項的Page_Load函數裡直接調用BindCompareDateChoosers(this,beginDateChooser,endDateChooser)來綁定驗證日期的指令碼。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1534780 

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.