Sharepoint 日期时间控件

发布于 2024-10-19 23:19:53 字数 1165 浏览 1 评论 0原文

我正在开发一个sharepoint webpart,其中我有2个日期时间控件,一个用于起始日期,另一个用于截止日期,我编写了一个javascript函数来验证用户输入的日期。如果“截止日期”小于“起始日期”,则会发出警报。我将此 JS 函数作为属性添加到按钮单击中。但是当我点击时这个函数没有执行。

我正在添加我的代码:

  void Registerscript()
    {
        string jc = @"<script> function DateMsg()
             {{ 

                var Fromdate = document.getElementById('{0}').value;
                var Todate = document.getElementById('{1}').value;

                 if(Fromdate != '' && Todate != '')
                {{             
                if(Date.parse(Fromdate) > Date.parse(Todate))
                  alert('From Date should be earlier than To Date.');
                }}

             }}</script> ";
        jc = string.Format(jc, dtFromdate.ClientID + "_dtFromdateDate", dtTodate.ClientID + "_dtTodateDate");
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", jc);
    }

我在 Web 部件的 Createchildcontrol 方法中调用了此函数。

并且我在 Web 部件的 OnLoad 事件上添加了此代码

btnView.Attributes.Add("onclick", "DateMsg();");

请帮助我解决此问题...

仅供参考:此 btnView(按钮控件)是在运行时动态创建..

提前致谢...

I am developing one sharepoint webpart, in this i have 2 date time control, one is for From date and another is for To date, And i wrote one javascript function to validate the date entered by the user. If the To date is less than the from Date, it will alerts. and i added this JS function to the button click as a attribute. But this function is not executing while i am clicking.

i am adding my code with this:

  void Registerscript()
    {
        string jc = @"<script> function DateMsg()
             {{ 

                var Fromdate = document.getElementById('{0}').value;
                var Todate = document.getElementById('{1}').value;

                 if(Fromdate != '' && Todate != '')
                {{             
                if(Date.parse(Fromdate) > Date.parse(Todate))
                  alert('From Date should be earlier than To Date.');
                }}

             }}</script> ";
        jc = string.Format(jc, dtFromdate.ClientID + "_dtFromdateDate", dtTodate.ClientID + "_dtTodateDate");
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", jc);
    }

I called this function in Createchildcontrol method of the webpart..

And i added this code on OnLoad event of the webpart

btnView.Attributes.Add("onclick", "DateMsg();");

Please help me for resoving this issue...

FYI: This btnView(Button control) is dynamically created at runtime..

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記憶穿過時間隧道 2024-10-26 23:19:53

我会采用不同的方式,以便更轻松地进行调试。

在 .aspx 文件中包含该函数:

<script type="text/javascript">
function DateMsg() {
    if (typeof _fromDateTextboxID != "undefined") {
        var oFromDateInput = document.getElementById(_fromDateTextboxID);
        var oToDateInput = document.getElementById(_toDateTextboxID);

        if (!oFromDateInput) {
            alert("element with ID of " + _fromDateTextboxID + " does not exist");
            return;
        }

        if (!oToDateInput) {
            alert("element with ID of " + _toDateTextboxID + " does not exist");
            return;
        }

        var strFromdate = oFromDateInput.value;
        var strTodate = oToDateInput.value;
        if(strFromdate.length == 0 || strTodate.length == 0) {
            alert("one or more values empty");
            return;
        }

        var dtFromDate = Date.parse(strFromdate);
        var dtToDate = Date.parse(Todate);
        if(dtFromDate > dtToDate) {
            alert('From Date should be earlier than To Date.');
        }

    }
    else {
        alert("ID not initialized");
    }
}
</script>

并仅输出后面代码中的 ID:

string jc = string.Format("var _fromDateTextboxID = \"{0}\"; var _toDateTextboxID = \"{1}\"; ", dtFromdate.ClientID + "_dtFromdateDate", dtTodate.ClientID + "_dtTodateDate");
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", jc, true);

保持 onclick 按钮不变,现在您可能会看到出了什么问题 - 让我们更新。

I would go in different way, allowing easier debug.

Have the function in the .aspx file:

<script type="text/javascript">
function DateMsg() {
    if (typeof _fromDateTextboxID != "undefined") {
        var oFromDateInput = document.getElementById(_fromDateTextboxID);
        var oToDateInput = document.getElementById(_toDateTextboxID);

        if (!oFromDateInput) {
            alert("element with ID of " + _fromDateTextboxID + " does not exist");
            return;
        }

        if (!oToDateInput) {
            alert("element with ID of " + _toDateTextboxID + " does not exist");
            return;
        }

        var strFromdate = oFromDateInput.value;
        var strTodate = oToDateInput.value;
        if(strFromdate.length == 0 || strTodate.length == 0) {
            alert("one or more values empty");
            return;
        }

        var dtFromDate = Date.parse(strFromdate);
        var dtToDate = Date.parse(Todate);
        if(dtFromDate > dtToDate) {
            alert('From Date should be earlier than To Date.');
        }

    }
    else {
        alert("ID not initialized");
    }
}
</script>

And output only the ID from the code behind:

string jc = string.Format("var _fromDateTextboxID = \"{0}\"; var _toDateTextboxID = \"{1}\"; ", dtFromdate.ClientID + "_dtFromdateDate", dtTodate.ClientID + "_dtTodateDate");
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", jc, true);

Leave the button onclick unchanged, and now you might see what's wrong - keep us updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文