Confirmation dialog box in Code Behind using C#( C-Sharp ) in ASP.NET


You can call a confirmation dialog box from code behind using c#. Copy follow the code in respective files.

Copy this is HTML

<p>
        <asp:Button id="btnSave" runat="server" Text="Save and Update 'RFFM'" OnClientClick="return UpdateRFFM();" />
    </p>

Copy this in C# code behind.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using ADI.Web;

namespace SIS
{
    public partial class update_sis : System.Web.UI.Page
    {
        SqlCommand cmd;
        SqlParameter pm;
        string strForm = "";
        StringBuilder sb;
        string strError = "";
        int UpdateStatus = -1;
        string strEmailTemplate = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/plain";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (Request["form_name"] == null)
            {
                Response.Write("");
                Response.End();
            }
            strForm = Utils.ToStringNullSafe(Request["form_name"]);
            sb = new StringBuilder();

            if (Session["ApplicationId"] == null)
            {
                Response.Write("Bad Request...");
                Response.End();
            }

            int ApplicationId = int.Parse(Session["ApplicationId"].ToString());

            switch (strForm)
            {
                case "rffm_create_user":
                    if (Session["email"] == null)
                    {
                        Response.Write("Bad Request");
                        Response.End();
                    }
                    Session["errmsg"] = "";
                    try
                    {
                        cmd = new SqlCommand("usp_SIS_PutUser");

                        // output parameter
                        pm = new SqlParameter("@UpdateStatus", SqlDbType.Int);
                        pm.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(pm);

                        // input parameters
                        cmd.Parameters.Add("Id", SqlDbType.Int).Value = 0;
                        cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = Session["Email"].ToString();

                        string strPassword = System.Guid.NewGuid().ToString().Substring(0, 8);
                        
                        cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = Utils.base64Encode(strPassword);
                        cmd.Parameters.Add("@Active", SqlDbType.Bit).Value = false;
                        int ClubId = int.Parse(Session["ClubId"].ToString());
                        if (ClubId > 0)
                        {
                            cmd.Parameters.Add("@Group_Id", SqlDbType.Int).Value = 31;
                            cmd.Parameters.Add("@Club_Id", SqlDbType.Int).Value = ClubId;
                            cmd.Parameters.Add("@NonClub_Id", SqlDbType.Int).Value = (object)DBNull.Value;
                        }
                        else
                        {
                            cmd.Parameters.Add("@Group_Id", SqlDbType.Int).Value = 32;
                            cmd.Parameters.Add("@Club_Id", SqlDbType.Int).Value = (object)DBNull.Value;
                            cmd.Parameters.Add("@NonClub_Id", SqlDbType.Int).Value = int.Parse(Session["Non_ClubId"].ToString());
                        }

                        Utils.executeSP(cmd);
                        Session.Remove("emil");
                        strError = "";

                        if (Session["errmsg"].ToString() != "") 
                            strError = "-1" + Session["errmsg"].ToString();

                        if (strError == "")
                        {
                            int UpdateStatus = int.Parse(cmd.Parameters["@UpdateStatus"].Value.ToString());
                            if (UpdateStatus < 0)
                                strError = "-1There was a problem when creating new user. Please contact Admin";
                            else
                            {
                                // send email
                                strEmailTemplate = Utils.ReadTemplate("newuser.txt");
                                strError = "00" + SIS_Site.SendEmail(UpdateStatus, strEmailTemplate);
                            }
                        }
                    } catch (Exception ex) {
                        strError = "-1" + ex.Message;
                    }
                    break;
                case "rffm_SendVerificationEmail":
                    // send email
                    if (Session["UserId"] == null)
                    {
                        Response.Write("Bad Request");
                        Response.End();
                    }
                    strError = "";
                    strEmailTemplate = Utils.ReadTemplate("newuser.txt");
                     strError = "00" + SIS_Site.SendEmail(int.Parse(Session["UserId"].ToString()), strEmailTemplate);
                    Session.Remove("UserId");
                    break;
                case "rffm_UserSetupVerify":
                    strError = "";
                    try
                    {
                        cmd = new SqlCommand("usp_SIS_UserSetupVerify");

                        // output parameter
                        pm = new SqlParameter("@UpdateStatus", SqlDbType.Int);
                        pm.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(pm);

                        SqlParameter pm2 = new SqlParameter("@EMail", SqlDbType.VarChar, 500);
                        pm2.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(pm2);

                        SqlParameter pm3 = new SqlParameter("@Updatetext", SqlDbType.VarChar, 200);
                        pm3.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(pm3);

                        SqlParameter pm4 = new SqlParameter("@UserId", SqlDbType.Int);
                        pm4.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(pm4);

                        // input parameters
                        cmd.Parameters.Add("@ApplicationId", SqlDbType.Int).Value = ApplicationId;

                        Utils.executeSP(cmd);

                        if (Session["errmsg"] != null)
                            strError = Session["errmsg"].ToString();

                        if (strError == "")
                        {
                            UpdateStatus = int.Parse(cmd.Parameters["@UpdateStatus"].Value.ToString());
                            if (UpdateStatus < 0)
                                strError = "There was an error when processing your request";
                        }

                        Session["email"] = cmd.Parameters["@EMail"].Value.ToString();
                        Session["UserId"] = cmd.Parameters["@UserId"].Value.ToString();
                        Session["Updatetext"] = cmd.Parameters["@Updatetext"].Value.ToString();

                        switch (UpdateStatus)
                        {
                           case 1:
                                strError = "1";
                                break;
                            case 2:
                                strError = "2";
                                break;
                            case 3:
                                strError = "3";
                                break;
                            case 4:
                                strError = Session["Updatetext"].ToString();
                                break;
                            case 5:
                                strError = "5" + Session["Updatetext"].ToString();
                                break;
                            default:
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        strError = ex.Message;
                    }
                    break;

                case "rffm":
                    Session["current_tab"] = "rffm";
                    try
                    {
                        cmd = new SqlCommand("usp_SIS_PutProjectProfile");

                        // output parameter
                        pm = new SqlParameter("@UpdateStatus", SqlDbType.Int);
                        pm.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(pm);

                        // input parameters
                        cmd.Parameters.Add("@ApplicationId", SqlDbType.Int).Value = ApplicationId;
                        cmd.Parameters.Add("@LastSavedBy", SqlDbType.VarChar, 50).Value = Session["user"].ToString();

                        cmd.Parameters.Add("@FeasibilityStudyDocument", SqlDbType.VarChar, 8000).Value = Request["FeasibilityStudy"];
                        cmd.Parameters.Add("@PlanningPermissionNotRequired", SqlDbType.VarChar, 8000).Value = Request["PlanningPermissionNotRequired"];
                        cmd.Parameters.Add("@Sustainable", SqlDbType.VarChar, 8000).Value = Request["Sustainable"];

                        string strRDMRecommendation = Request["RDMRecommendation"];
                        cmd.Parameters.Add("@RDMRecommendation", SqlDbType.VarChar, 8000).Value = (strRDMRecommendation == null) ? (object)DBNull.Value : strRDMRecommendation;

                        cmd.Parameters.Add("@Risk", SqlDbType.VarChar, 10).Value = Request["Risk"];
                        cmd.Parameters.Add("@Region", SqlDbType.VarChar, 50).Value = Request["Region"];
                        cmd.Parameters.Add("@RDMName", SqlDbType.VarChar, 50).Value = Session["user"].ToString();
                        cmd.Parameters.Add("@RDMReviewDate", SqlDbType.DateTime).Value = DateTime.Now;
                        cmd.Parameters.Add("@DesignAndPlanningPermission", SqlDbType.VarChar, 8000).Value = Request["DesignAndPlanningPermission"];
                        cmd.Parameters.Add("@RecommendationOfProjectNeedAgainstSDA", SqlDbType.VarChar, 8000).Value = Request["RecommendationOfProjectNeedAgainstSportsDevelopmentActivity"];
                        cmd.Parameters.Add("@ValueForMoneyAssessment", SqlDbType.VarChar, 8000).Value = Request["ValueForMoneyAssessment"];
                        cmd.Parameters.Add("@ActionId", SqlDbType.Int).Value = int.Parse(Request["ActionId"].ToString());
                        cmd.Parameters.Add("@ActionNotes", SqlDbType.VarChar, 500).Value = "RFFM";

                        Utils.executeSP(cmd);

                        if (Session["errmsg"] != null)
                            strError = Session["errmsg"].ToString();

                        if (strError == "")
                        {
                            UpdateStatus = int.Parse(cmd.Parameters["@UpdateStatus"].Value.ToString());
                            if (UpdateStatus < 0)
                                strError = "There was an error when processing your request";
                            else
                                Session["current_tab"] = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        strError = ex.Message;
                    }
                    break;

                case "check_session":
                    if (Session["ApplicationId"] != null)
                    {
                        string hfID = Request["hf_ApplicationId"].ToString();
                        string sfID = Session["ApplicationId"].ToString();

                        if (hfID != sfID)
                        {
                            strError = "-1";
                        }
                    }
                    break;
                default:
                    strError = "Bad request";
                    break;

            }
            
            Response.Write(strError);
        }
    }
} 
 
 
 
Copy this in Javascript file

function UpdateRFFM()
{
    var msg;
    var status_code;
    var rffmAction = $(".rffmAction").val();
    var Risk = $(".ddlRisk").val();
    var Region = $(".ddlRegion").val();
    var strError = "";
    
    if (rffmAction == "151") {
        if (Risk == "")
            strError = strError + "Please select a Risk\n";
        if (Region == "")
            strError = strError + "Please select a Region\n";                
    }
    
    if (strError != "") {
        alert(strError);
        return false;
    }
    
    var reply = false;

    if (rffmAction == "151") {
        $.post(getFullPath("update_sis.aspx"), 
            {form_name: 'rffm_UserSetupVerify'}, function(data) {
            if (data.length > 0)
                status_code = data.substring(0, 1);
            else
                status_code = '';
            
            switch(status_code) {
                case '0':
                    UpdateRFFM_Normal();
                    break;
                case '1':
                    $.post(getFullPath("update_sis.aspx"),
                        {form_name: 'rffm_create_user'}, function(data) {
                            status_code = data.substring(0, 2);
                            msg = data.substring(2);
                            if (status_code == "-1") 
                                alert(msg);
                            else {
                                alert('created new user account\n\n' + msg);
                                UpdateRFFM_Normal();
                            }                                    
                        });
                    break;
                case '2':
                    UpdateRFFM_Normal();
                    break;
                case '3':
                     $.post(getFullPath("update_sis.aspx"),
                        {form_name: 'rffm_SendVerificationEmail'}, function(data) {
                            status_code = data.substring(0, 2);
                            msg = data.substring(2);
                            if (status_code == "-1") 
                                alert(msg);
                            else {
                                alert('verification email sent\n\n' + msg);
                                UpdateRFFM_Normal();
                            }
                        });
                    break;
                case '4':
                    alert(data);
                    break;
                case '5':
                    msg = data.substring(1);
                    if (confirm(msg) == true) {
                      $.post(getFullPath("update_sis.aspx"),
                        {form_name: 'rffm_SendVerificationEmail'}, function(data) {
                            status_code = data.substring(0, 2);
                            msg = data.substring(2);
                            if (status_code == "-1") 
                                alert(msg);
                            else 
                                alert('verification email was sent\n\n' + msg);
                                UpdateRFFM_Normal();
                        });
                    }
                    break;                                                                                                
                default:
                    alert(data);
                    break;
            }
        });
    } else
        UpdateRFFM_Normal();

    return reply;
}
function UpdateRFFM_Normal() {
    var reply = false;         
    $.post(getFullPath("update_sis.aspx"), {form_name: 'rffm',
        FeasibilityStudy: $(".FeasibilityStudy").val(),
        PlanningPermissionNotRequired: $(".PlanningPermissionNotRequired").val(),
        Sustainable: $(".Sustainable").val(),
        RDMRecommendation: $(".RDMRecommendation").val(),
        Risk: $(".ddlRisk").val(),
        Region: $(".ddlRegion").val(),
        DesignAndPlanningPermission: $(".DesignAndPlanningPermission").val(),
        RecommendationOfProjectNeedAgainstSportsDevelopmentActivity: $(".RecommendationOfProjectNeedAgainstSportsDevelopmentActivity").val(),
        ValueForMoneyAssessment: $(".ValueForMoneyAssessment").val(),
        ActionNotes: $(".ActionNotes").val(),
        ActionId: $(".rffmAction").val()
        }, function(data) {
           if (data.length > 0) {
                alert(data);
                return false;
            } else {
                window.location = getFullPath("application_form.aspx");
                return false;
            }
        });
}

No comments:

Post a Comment

Recent Posts