如何在 javascript 文件函数中找到 Radnotification id

How to find Radnotification id insidet the javascript file function

本文关键字:Radnotification id 函数 javascript 文件      更新时间:2023-09-26

我在我的 porject 中使用了 radcontrols,我想限制 radTextbox 不应该允许特殊字符,所以我在我的 aspx 页面中使用脚本函数它工作正常,但我想在整个项目中使用它,所以我想维护一个 javascript 文件,我想使用该函数,所以帮助我如何将文本框 ID 和通知 ID 传递给函数。这是我的代码

.aspx

脚本文件

function valueChangedHandler(sender, eArgs) {alert("Done");
    var pattern = /^[a-zA-Z0-9's]*$/;
    var value = sender.get_value();
    var matchArray = value.match(pattern);
    if (matchArray == null) {
        var notification = $find("<%=lblNotification.ClientID %>");
        notification.set_text('Enter AlphaNumerics Only!');
        notification.show();
        sender.clear();
        sender.focus();
        exit();
    }
}

通知代码

 <telerik:RadNotification runat="server" BorderColor="Black" BorderStyle="Solid" BackColor="ActiveBorder" BorderWidth="4" EnableRoundedCorners="true" EnableShadow="true" ID="lblNotification" Position="BottomRight" ShowCloseButton="true" Opacity="90" Title="Result" VisibleTitlebar="False">
            </telerik:RadNotification>

上面工作正常,但我没有收到通知消息,所以告诉我如何传递通知ID。

您根本无法在外部 JS 文件中使用服务器代码块。服务器仅在 aspx/ascx 文件中分析它们。您可以做的是在页面上声明一个函数,该函数具有将返回所需引用的通知:

function getNotification(){
     return $find("<%=lblNotification.ClientID %>");
}

然后 JS 文件中的函数将调用此函数:

var notification = getNotification();

还有其他方法可以在外部文件中使用 ClientID,例如在页面的全局变量中创建数组,但如果您不想依赖硬编码 ID,它们将在页面中包含代码。好吧,您也可以从代码隐藏中注入脚本,但没有太大区别。