javascript confirm asp.net button never submits?

为君一笑 提交于 2019-12-22 12:37:09

问题


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.


回答1:


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



回答2:


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.




回答3:


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




回答4:


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.




回答5:


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,



来源:https://stackoverflow.com/questions/3730518/javascript-confirm-asp-net-button-never-submits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!