0

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();" />
4
  • How are you invoking the function? What does your browser debug console/firebug/dev tools/etc. say, if any error? Commented May 14, 2013 at 10:17
  • did you check produced markup (script tag) ? I would bet that there is a problem with right folder. If your scripts folder is in site root try with the "/scripts/DeleteUser.js", btw. you don't need @ for slash (/) Commented May 14, 2013 at 10:22
  • @EdSF: I edited the question with how I invoke the function. Commented May 14, 2013 at 10:29
  • @AntonioBakula: The script is being referenced corrected as evident from the view-source page. Commented May 14, 2013 at 10:29

3 Answers 3

1

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')

Sign up to request clarification or add additional context in comments.

Comments

1

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;

    }

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.