public string deleteConfirm(object comp_id)
{
string ID = (string)comp_id;
return @"javascript:var result = confirm('Are you sure you want to delete selected item?');
if(result)
{
window.location='delete.aspx?id=' + ID;
}
else{
return false;
}";
the popup message was appear, but it wont bring me to the new page after i click "Yes", i bet there is a problem with my javascript at code behind, please advice me.
Also I just noticed that you are using ID within string. Infact you have to concatinate in string.
i.e. your code should be
public string deleteConfirm(object comp_id)
{
string ID = (string)comp_id;
return @"javascript:var result = confirm('Are you sure you want to delete selected item?');
if(result)
{
window.location='delete.aspx?id='"+ID+"';
}
else{
return false;
}";
Please Mark as Answer if the post helps you.
My BLOG
i tried several ways to open the new windows, but none of it working. but if i just simple link it to another page without the confirmation box, it's working...
so, i believe the problem is not the way we open/link to a new page..
is there any way i can rewrite the javascript code without "return" in front of the ' @"javascript ' ?
melvintcs
Member
183 Points
244 Posts
problem with window.location at code behind
Nov 19, 2012 01:03 AM|LINK
i put a hyperlink under my gridview.
<asp:TemplateField HeaderText="regular"> <ItemTemplate> <asp:HyperLink ID="hlReset" runat="server" NavigateUrl='<%# deleteConfirm(Eval("comp_id")) %>' Text='Delete'> </asp:hyperLink> </asp:TemplateField>Code Behind:
public string deleteConfirm(object comp_id) { string ID = (string)comp_id; return @"javascript:var result = confirm('Are you sure you want to delete selected item?'); if(result) { window.location='delete.aspx?id=' + ID; } else{ return false; }";the popup message was appear, but it wont bring me to the new page after i click "Yes", i bet there is a problem with my javascript at code behind, please advice me.
iGulfam
Contributor
4794 Points
947 Posts
Re: problem with window.location at code behind
Nov 19, 2012 04:18 AM|LINK
try using window.open if you want to use same logic
i.e.
public string deleteConfirm(object comp_id) { string ID = (string)comp_id; return @"javascript:var result = confirm('Are you sure you want to delete selected item?'); if(result) { window.open('delete.aspx?id=' + ID, '_self'); } else{ return false; }";Or you can you do this without using the Code behind.
Use this code
<asp:TemplateField HeaderText="regular"> <ItemTemplate> <a href='javascript:if confirm('Are you sure you want to delete selected item?') { windo.open('delete.aspx?id='+<%# Eval("comp_id") %>, '_self'); } else { return false; }>Delete</a> </asp:TemplateField>My BLOG
melvintcs
Member
183 Points
244 Posts
Re: problem with window.location at code behind
Nov 19, 2012 04:42 AM|LINK
thx for the reply, i tried and it's still the same :(
iGulfam
Contributor
4794 Points
947 Posts
Re: problem with window.location at code behind
Nov 19, 2012 04:54 AM|LINK
Then try my other solution, which is
<asp:TemplateField HeaderText="regular"> <ItemTemplate> <a href='javascript:if confirm('Are you sure you want to delete selected item?') { windo.open('delete.aspx?id='+<%# Eval("comp_id") %>, '_self'); } else { return false; }>Delete</a> </asp:TemplateField>My BLOG
iGulfam
Contributor
4794 Points
947 Posts
Re: problem with window.location at code behind
Nov 19, 2012 04:55 AM|LINK
Also I just noticed that you are using ID within string. Infact you have to concatinate in string.
i.e. your code should be
public string deleteConfirm(object comp_id) { string ID = (string)comp_id; return @"javascript:var result = confirm('Are you sure you want to delete selected item?'); if(result) { window.location='delete.aspx?id='"+ID+"'; } else{ return false; }";My BLOG
melvintcs
Member
183 Points
244 Posts
Re: problem with window.location at code behind
Nov 19, 2012 05:15 AM|LINK
i tried several ways to open the new windows, but none of it working. but if i just simple link it to another page without the confirmation box, it's working...
so, i believe the problem is not the way we open/link to a new page..
is there any way i can rewrite the javascript code without "return" in front of the ' @"javascript ' ?
iGulfam
Contributor
4794 Points
947 Posts
Re: problem with window.location at code behind
Nov 19, 2012 05:35 AM|LINK
Why don't you try the Client side solution I told? It down't involved the Code behding (the return statement)
<asp:TemplateField HeaderText="regular"> <ItemTemplate> <a href='javascript:if confirm('Are you sure you want to delete selected item?') { windo.open('delete.aspx?id='+<%# Eval("comp_id") %>, '_self'); } else { return false; }>Delete</a> </asp:TemplateField>My BLOG
melvintcs
Member
183 Points
244 Posts
Re: problem with window.location at code behind
Nov 19, 2012 05:55 AM|LINK
thx, but your code is not working..
melvintcs
Member
183 Points
244 Posts
Re: problem with window.location at code behind
Nov 19, 2012 05:59 AM|LINK
it's working if i do it this way:
function del() { if(confirm('delete?')){ window.open('delete.aspx'); } else { return false; } <a onclick=del()>Delete</a>but if i include the Eval(as below), it's said that del does not exist in the current context...
<a onClick='<%# del(Eval("comp_id")) %>'> delete </a>iGulfam
Contributor
4794 Points
947 Posts
Re: problem with window.location at code behind
Nov 19, 2012 06:17 AM|LINK
Use this code for your a tag
<a onClick='javascript:del(<%# Eval("comp_id") %>);'> delete </a>My BLOG