Animesh D
Animesh D

Reputation: 5002

Javascript is not being called from ASP.NET User Control

I converted an ASP.NET page DeleteUser.aspx into a user control DeleteUser.ascx.

The aspx page had a simple javascript function like this:

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
    }
}

Now since this is converted into a user control, DeleteUser.ascx, I first moved this javascript code into the containing aspx file in the <head> tag. The page was throwing an exception txtusercn is not in the scope of the containing aspx file.

Then I moved the javascript function into a separate javascript file DeleteUser.js and referenced it from DeleteUser.ascx.cs like this:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterClientScriptInclude(typeof(DeleteUser),"DeleteUserScript",@"../scripts/DeleteUser.js");
}

The javascript function is not being called here, but when I check the containing aspx file's page source, I can see that the javascript function is registered properly.

Why is the javascript function not being called even after the page refers to the javascript file DeleteUser.js


Edit:

Invoking the function like this:

<asp:Button ID="btndeleteusercn" runat="server" Text="Delete Usercn" OnClick="btndeleteusercn_Click" OnClientClick="return deleteConfirmation();" />

Upvotes: 0

Views: 660

Answers (3)

Animesh D
Animesh D

Reputation: 5002

The client ID of the text box is being rendered as DeleteUser1_txtusercn, so I changed the javascript like this and it worked:

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;
    }
}

Upvotes: 0

Nitin Aggarwal
Nitin Aggarwal

Reputation: 461

Please try this updated code.

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;

    }

Upvotes: 1

user2160658
user2160658

Reputation: 29

I guess, "<%= txtusercn.ClientID %>" must be creating the JavaScript Error as such Server Control reference should be on the page on which the related control exists. You should pass the ControlID through parameters to the function and then it should work.

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
}}

and call the Method from by passing the ID from there. such as

deleteConfirmation('ID of Control')

Upvotes: 1

Related Questions