最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript confirm asp.net button never submits? - Stack Overflow

programmeradmin2浏览0评论

i have a regular asp:button. I am working in .Net 3.5. I've tried adding a js confirm to the button with the OnClientClick attribute as well as adding in code-behind and the result is the same. No matter what the user clicks in the confirm pop-up the form will not submit??

BtnDeleteSelected.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');");

The confirm dialog appears and if i select "OK" it still does not submit.. Any ideas? thanks.

i have a regular asp:button. I am working in .Net 3.5. I've tried adding a js confirm to the button with the OnClientClick attribute as well as adding in code-behind and the result is the same. No matter what the user clicks in the confirm pop-up the form will not submit??

BtnDeleteSelected.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');");

The confirm dialog appears and if i select "OK" it still does not submit.. Any ideas? thanks.

Share Improve this question asked Sep 16, 2010 at 20:29 rodrickrodrick 872 silver badges8 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

That's because you should not be returning in all cases. If you view the source of the page and look for the button markup, you are likely to see this...

onclick="return confirm('Ok?'); __doPostback();"

The __doPostback invocation is inserted automatically by the ASP.NET framework in some cases. Since you return immediately regardless of the result of confirm, the postback never fires. The ultimate solution would be to not to set the onclick attribute, but instead use an unobtrusive approach. However, if there is pressing reason to control this on the server-side via onclick, you can use a simple if statement and only return when they press "Cancel"...

string js = "if(!confirm('Are you sure you want to delete?')) return false;";
BtnDeleteSelected.Attributes.Add("onclick", js);

I had this problem too. I was using adding the client side javascript in the OnItemCreated code of a datagrid like this:

 myTableCell = e.Item.Cells(iDeleteColumnNumber) 'Delete Button Column
 myDeleteButton = myTableCell.Controls(1)

 If myDeleteButton.UniqueID = "lbtnDelete" Then
    myDeleteButton.Attributes.Add("onclick", "if(!confirm('OK to Delete?'))return false;")
 End If

For some reason that didn't work. I then moved the JavaScript to the <asp:linkbutton> in the design view like this:

   <asp:LinkButton id="lbtnDelete" runat="server" CssClass="link-text" Text="<img border=0 src=../images/icons/icon-delete.gif alt=Delete>"
 CommandName="Delete" CausesValidation="false" OnClientClick="if(!confirm('OK to Delete?'))return false;"></asp:LinkButton>

And that worked for me.

Do you have to return TRUE if validation passes? Meaning, if confirm() returns false I don't think the form will submit.

Your code looks OK on the surface. Do you have any validators that might be preventing it being submitted? Try adding BtnDeleteSelected.CausesValidation = false to prevent the delete button calling any client-side validators.

Please try calling doPostBack function ;)

I mean:

BtnDeleteSelected.Attributes.Add("onclick", "CheckConfirm();");

<script language="javascript">
function CheckConfirm()
{
    if ( confirm('Are you sure you want to delete?') )
        __doPostBack('BtnDeleteSelected','');
    else
        return false;

    return true;
}
</script>

Hope that helps,

发布评论

评论列表(0)

  1. 暂无评论