Call Server side code on Sweet Alert Confirm Button using jQuery Ajax in ASP.Net

Last Reply 11 days ago By crajesh

Posted 12 days ago

hi,

i am using Sweet Alert For Display Message in My Project,

i Showed Confirm Message Box on Gridview Linkbutton Onclient Click,

it works but when I Clicked OK Button ,Gridview Linkbutton Not Firing.

Can Any One Help Me For This.

 

Thanks In advance

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sweet_Alert_Confirm_On_Gridview_LinkButton_Example2.aspx.cs"
    Inherits="Sweet_Alert_Confirm_On_Gridview_LinkButton_Example2" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <link href="Sweet_Alert/css/sweetalert.css" rel="stylesheet" type="text/css" />
    <script src="Sweet_Alert/js/sweetalert-dev.js" type="text/javascript"></script>
    <script src="Sweet_Alert/js/sweetalert.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <script type="text/javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandle);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);

        function beginRequestHandle(sender, Args) {
        }

        function endRequestHandle(sender, Args) {

        }
    </script>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <table>
                    <tr>
                        <td align="left" valign="top" style="padding: 05px;">
                            <asp:GridView ID="GridView1" CellPadding="4" runat="server" Width="100%" AutoGenerateColumns="false"
                                DataKeyNames="ID,NAME" OnRowDataBound="GridView1_RowDataBound">
                                <Columns>
                                    <asp:TemplateField HeaderText="Sr#" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <%# Container.DataItemIndex + 1 %>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="ID" HeaderText="ID" HeaderStyle-HorizontalAlign="Center"
                                        ItemStyle-HorizontalAlign="Right" />
                                    <asp:BoundField DataField="NAME" HeaderText="NAME" HeaderStyle-HorizontalAlign="Center"
                                        ItemStyle-HorizontalAlign="Left" />
                                    <asp:BoundField DataField="Salary" HeaderText="Salary" HeaderStyle-HorizontalAlign="Center"
                                        ItemStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
                                    <asp:TemplateField HeaderText="View">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnk_View1" runat="server" Font-Bold="true" Font-Underline="true"
                                                Text="View" ForeColor="Blue" OnClick="lnk_View1_Click"></asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>                                    
                                </Columns>
                                <HeaderStyle BackColor="#9CAAC1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                                <PagerStyle BackColor="#9CAAC1" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" />
                                <FooterStyle BackColor="#9CAAC1" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" />
                                <RowStyle BackColor="White" ForeColor="Black" Font-Names="Verdana" Font-Size="12px" />
                                <SelectedRowStyle BackColor="lightGray" Font-Bold="false" ForeColor="Black" Font-Names="Verdana"
                                    Font-Size="12px" />
                            </asp:GridView>
                        </td>
                    </tr>
                </table>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <script type="text/javascript">

        $(document).ready(function () {


        });

    </script>    
    <script type="text/javascript">

        function Swal_Confirm_Msg1(ctl, event, title_msg, text_msg, btn_type) {

            event.preventDefault();

            swal({
                title: title_msg,
                text: text_msg,
                type: btn_type,
                showCancelButton: true,
                confirmButtonClass: "btn-danger",
                confirmButtonText: "Yes, delete it!",
                cancelButtonText: "No, cancel plx!",
                showLoaderOnConfirm: true,
                animation: 'slide-from-top',
                allowEscapeKey: false,
                allowOutsideClick: false,
                html: true
            },

            function (isConfirm) {
                if (isConfirm) {
                    //__doPostBack(ctl, null);
                    return true;
                } else {
                    return false;
                }
            });
        }
    </script>
    <style type="text/css">
        .sweet-alert
        {
            color: #fff;
            background-color: White !important;
            width: 450px !important;
        }
        
        .sweet-alert .sa-icon.sa-success .sa-placeholder
        {
            width: 80px;
            height: 80px;
            border: 4px solid #a5dc86 !important;
        }
    </style>
   
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Sweet_Alert_Confirm_On_Gridview_LinkButton_Example2 : System.Web.UI.Page
{
    string str = "";
    public enum Sweet_Alert_Type { basic = 1, success = 2, warning = 3, error = 4 }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Fill_GV();
        }
    }


    public void Fill_GV()
    {
        DataTable dt = new DataTable("Rajesh Report1");
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID", typeof(Int32)), new DataColumn("NAME", typeof(String)), new DataColumn("Salary", typeof(int)) });

        dt.Rows.Add(1, "R111", 1000);
        dt.Rows.Add(2, "R222", 2000);
        dt.Rows.Add(3, "R333", 3000);
        dt.Rows.Add(4, "R444", 4000);
        dt.Rows.Add(5, "R555", 5000);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.Attributes["style"] = "border:1px solid black;";
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.textDecoration ='none';this.style.cursor='hand';this.style.backgroundColor='PeachPuff'");
            e.Row.Attributes.Add("onmouseout", "this.style.textDecoration ='none';this.style.backgroundColor=this.originalstyle;");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ID = DataBinder.Eval(e.Row.DataItem, "ID").ToString();
            string NAME = DataBinder.Eval(e.Row.DataItem, "NAME").ToString();

            LinkButton lnk_View1 = (LinkButton)e.Row.FindControl("lnk_View1");
            LinkButton lnk_View2 = (LinkButton)e.Row.FindControl("lnk_View2");

            if (lnk_View1 != null)
            {
                string msg = "ID :" + ID + "-" + "NAME :" + NAME;
                lnk_View1.OnClientClick = "javascript:return Swal_Confirm_Msg1('" + lnk_View1.ID + "', event,'Are you sure?','" + msg + "','warning');";
            }
        }
    }



    protected void lnk_View1_Click(object sender, EventArgs e)
    {
        try
        {
            LinkButton lnk = sender as LinkButton;
            GridViewRow GVR = (GridViewRow)lnk.NamingContainer;
            LinkButton lnk_View1 = (LinkButton)GVR.FindControl("lnk_View1");

            string Get_Row_Index = GVR.RowIndex.ToString();
            GridView1.SelectedIndex = GVR.DataItemIndex;

            string ID = GridView1.DataKeys[GVR.RowIndex].Values["ID"].ToString();
            string NAME = GridView1.DataKeys[GVR.RowIndex].Values["NAME"].ToString();

            string msg1 = "ID :" + ID + "-" + "NAME :" + NAME;

            string str_alert_Msg = "";
            string type = Sweet_Alert_Type.success.ToString();
            string title = "MyTitle";
            string msg = "Hi Success Alert Msg.<br/>" + msg1;

            str_alert_Msg = @" swal({
                                    type: '" + type + @"',
                                    title:'" + title + @"',
                                    text: '" + msg + @"',
                                    confirmButtonColor: '#DD6B55',
                                    animation: 'slide-from-top',
                                    allowEscapeKey: true,
                                    allowOutsideClick: false,
                                    html: true
                               });";

            ScriptManager.RegisterStartupScript(this, GetType(), "Popup", str_alert_Msg, true);

        }
        catch (Exception ex)
        {
            string Ex_msg = ex.Message.ToString().Replace("\r", "").Replace("\n", "\\n").Replace("'", "").Replace("/", "");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "javascript:alert('" + Ex_msg.ToString() + ".!');", true);
        }
    }
 
  

    

    #region Method

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }


    #endregion Method
}

 

Results 1 - 5 of 6 12
Posted 12 days ago
Hi @crajesh,
Please try the following

Unable to fire jquery alert when clicking on link button inside gridview

It might help you.

Cheers Andrea.

Posted 12 days ago

Hi@Andrea

i tried for this , but not working for me,

please provide me any other solution.

 

thanks

 


Posted 12 days ago

try to remove your update panel


Posted 11 days ago

hi@ZZZ,

 

i tried this ,  but not working.


Posted 11 days ago

Hi crajesh,

I have tried your code and it seems like event.preventDefault once executed then it doesnt allow to run the server side code so for that you can do one thing is make a ajax call in the function of swal and achieve your task like below you can achieve it

<script type="text/javascript">
    function Swal_Confirm_Msg1(ctl, event, title_msg, text_msg, btn_type) {
        event.preventDefault();
        swal({
            title: title_msg,
            text: text_msg,
            type: btn_type,
            showCancelButton: true,
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel plx!",
            showLoaderOnConfirm: true,
            animation: 'slide-from-top',
            allowEscapeKey: false,
            allowOutsideClick: false,
            html: true
        },
        function (isConfirm) {
            if (isConfirm) {
                //Do your stuff if clicked yes.
                $.ajax({
                    type: "POST",
                    url: "YourPageName/YourMethodName",
                    data: "Data you want to pass",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        //response will return the value you assigned while returning on success call of ajax.
                    },
                    error: function (response) {
                        // reponse will return server side error message.
                    }
                });                
            } else {
                //Do your stuff if clicked No.
            }
        });
    }
</script>

 

I agree, here is the link: http://e-iceblue.com/free-api