Javascript is not being called from ASP.NET User Control -
i converted asp.net page deleteuser.aspx
user control deleteuser.ascx
.
the aspx page had simple javascript function 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 sure want delete \'' + uname + '\'?'); } else { alert('please enter usercn'); return false } }
now since converted user control, deleteuser.ascx
, first moved javascript code containing aspx file in <head>
tag. page throwing exception txtusercn
not in scope of containing aspx file.
then moved javascript function separate javascript file deleteuser.js , referenced deleteuser.ascx.cs
this:
protected void page_load(object sender, eventargs e) { page.clientscript.registerclientscriptinclude(typeof(deleteuser),"deleteuserscript",@"../scripts/deleteuser.js"); }
the javascript function not being called here, when check containing aspx file's page source, can see javascript function registered properly.
why javascript function not being called after page refers javascript file deleteuser.js
edit:
invoking function this:
<asp:button id="btndeleteusercn" runat="server" text="delete usercn" onclick="btndeleteusercn_click" onclientclick="return deleteconfirmation();" />
i guess, "<%= txtusercn.clientid %>" must creating javascript error such server control reference should on page on related control exists. should pass controlid through parameters function , 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 sure want delete \'' + uname + '\'?'); } else { alert('please enter usercn'); return false }}
and call method passing id there. such
deleteconfirmation('id of control')
Comments
Post a Comment