Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未从ASP.NET用户控件调用Javascript_Javascript_Asp.net - Fatal编程技术网

未从ASP.NET用户控件调用Javascript

未从ASP.NET用户控件调用Javascript,javascript,asp.net,Javascript,Asp.net,我将ASP.NET页面DeleteUser.aspx转换为用户控件DeleteUser.ascx aspx页面有一个简单的javascript函数,如下所示: function deleteConfirmation() { var xusername = document.getElementById('<%= txtusercn.ClientID %>').value; if (xusername != null && xusername != "

我将ASP.NET页面
DeleteUser.aspx
转换为用户控件
DeleteUser.ascx

aspx页面有一个简单的javascript函数,如下所示:

function deleteConfirmation() {

    var xusername = document.getElementById('<%= txtusercn.ClientID %>').value;
    if (xusername != null && xusername != "") {
        var userTextBoxId = '<%= txtusercn.ClientID %>';
        var uname = document.getElementById(userTextBoxId).value;
        return confirm('Are you sure you want to delete \'' + uname + '\'?');
    }
    else {
        alert('Please enter Usercn');
        return false
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterClientScriptInclude(typeof(DeleteUser),"DeleteUserScript",@"../scripts/DeleteUser.js");
}
<asp:Button ID="btndeleteusercn" runat="server" Text="Delete Usercn" OnClick="btndeleteusercn_Click" OnClientClick="return deleteConfirmation();" />
这里没有调用javascript函数,但是当我检查包含aspx文件的页面源代码时,我可以看到javascript函数已正确注册

为什么页面引用javascript文件
DeleteUser.js


编辑:

像这样调用函数:

function deleteConfirmation() {

    var xusername = document.getElementById('<%= txtusercn.ClientID %>').value;
    if (xusername != null && xusername != "") {
        var userTextBoxId = '<%= txtusercn.ClientID %>';
        var uname = document.getElementById(userTextBoxId).value;
        return confirm('Are you sure you want to delete \'' + uname + '\'?');
    }
    else {
        alert('Please enter Usercn');
        return false
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterClientScriptInclude(typeof(DeleteUser),"DeleteUserScript",@"../scripts/DeleteUser.js");
}
<asp:Button ID="btndeleteusercn" runat="server" Text="Delete Usercn" OnClick="btndeleteusercn_Click" OnClientClick="return deleteConfirmation();" />

我猜,“”一定是在创建JavaScript错误,因为这样的服务器控件引用应该位于存在相关控件的页面上。您应该通过参数将ControlID传递给函数,然后它就可以工作了

function deleteConfirmation(ctrlID) {

var xusername = document.getElementById(ctrlID).value;
if (xusername != null && xusername != "") {
    var userTextBoxId = ctrlID;
    var uname = document.getElementById(userTextBoxId).value;
    return confirm('Are you sure you want to delete \'' + uname + '\'?');
}
else {
    alert('Please enter Usercn');
    return false
}}
并通过从那里传递ID从中调用方法。 比如


删除确认(“控制ID”)

请尝试此更新代码

function deleteConfirmation() {

        var xusername = document.getElementById('<%= txtusercn.ClientID %>').value;
        if (xusername != null && xusername != "") {
            return confirm('Are you sure you want to delete \'' + xusername + '\'?');
        }
        else {
            alert('Please enter Usercn');
            return false
        }
return true;

    }
函数deleteConfirmation(){
var xusername=document.getElementById(“”).value;
if(xusername!=null&&xusername!=“”){
return confirm('您确定要删除\''+xusername+'\'?');
}
否则{
警报(“请输入Usercn”);
返回错误
}
返回true;
}

文本框的客户端ID呈现为
DeleteUser1\u txtusercn
,因此我更改了javascript,如下所示:

function deleteConfirmation() {

    var uname = document.getElementById('DeleteUser1_txtusercn').value;
    if (uname != null && uname != "") {
        return confirm('Are you sure you want to delete \'' + uname + '\'?');
    }
    else {
        alert('Please enter UserCN');
        return false;
    }
}

您如何调用该函数?您的浏览器调试控制台/firebug/dev tools/等会说什么,如果有错误?您是否检查了生成的标记(脚本标记)?我敢打赌,正确的文件夹有问题。如果您的脚本文件夹位于站点根目录中,请尝试使用“/scripts/DeleteUser.js”,顺便说一句。您不需要@for slash(/)@EdSF:我用如何调用函数编辑了这个问题。@AntonioBakula:引用的脚本已从“查看源代码”页面中明显更正。