Macb3th
Macb3th

Reputation: 101

ASP on button click validation

I have this code inside GridView:

<asp:TemplateField >
<ItemTemplate>
<input type="button"  id="Button98"
    usesubmitbehavior="true"
    Text="pavadinimas" 
    onclientclick="ConfirmOnDelete()"
    onclick="location.href='SMSReport.aspx?data=<%#Eval("data")%>&amp;db=1'" 
    />
    <script type="text/javascript" language="javascript">
        function ConfirmOnDelete()
        {
          if (confirm("Are you sure?")==true)
            return true;
          else
            return false;
        }
    </script>
</ItemTemplate>
</asp:TemplateField>

I'm not sure why, but onclientclick part is not working. I was looking and trying many solutions, but non is working. Any ideas how to add check on click?

Upvotes: 0

Views: 165

Answers (3)

user3875617
user3875617

Reputation:

<form id="form1" runat="server">      
    <input type="button" id="btn_11" runat="server" onclick="ConfirmOnDelete(); location.href = 'http://www.google.com'" />
    <script type="text/javascript" language="javascript">
        function ConfirmOnDelete() {
            if (confirm("Are you sure?") == true)
                return true;
            else
                return false;
        }
    </script>         
</form>

Upvotes: 1

Shaminder Singh
Shaminder Singh

Reputation: 1293

OnClientClick only works for asp server button control (System.Web.UI.WebControls) , but you are using the html button control so, and put your javascript code seperately which means that put your script tag outside your GridView control. Try the below code:

    <asp:TemplateField >
        <ItemTemplate>
        <input type="button"  id="Button98"
            Text="pavadinimas" value="delete"
            onclick="ConfirmOnDelete('<%#Eval("data")%>');" 
            />

     </ItemTemplate>
     </asp:TemplateField>

            <script type="text/javascript" language="javascript">
                function ConfirmOnDelete(para)
                {
                  if (confirm("Are you sure?")==true)
                   location.href='SMSReport.aspx?data=' + para + '&amp;db=1';
                  else
                    return false;
                }
            </script>

Upvotes: 1

Simon Price
Simon Price

Reputation: 3271

I had the exact same issue last week,

your asp.net button should have an OnClientClick="DoValidation(); click event assigned to is and thenin your js file you should have

asp.button

<asp:Button ID="btnSave" CssClass="btn btn-primary" OnClientClick="DoValidation()" runat="server" Text="Search" Height="36px" />

javascript

function DoValidation(parameter) {

 //your validation code here

if (valid == true) {
    __doPostBack('btnSave', parameter);
   }
}

Upvotes: 0

Related Questions