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();" />