Click here to Skip to main content
14,639,661 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,
I have a button in my page which is used to save data of gridview which is dynamically populated. Before saving I want to check some condition. The javascript function is as follows:
<script type="text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("This will completely delete the project. Are you sure?")) {
                confirm_value.value = "Yes";
            }
            else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>
<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" onClientClick="Confirm()"></button>

The code behind is as follows:
protected void lnkBtn_Click(object sender, EventArgs e)
        {
            string str=gdView.HeaderRow.Cells[8].Text;
            System.Web.UI.WebControls.TextBox txtID = (System.Web.UI.WebControls.TextBox)gdView.Rows[0].Cells[8].FindControl(str);
            if (txtID.Text != "")
            {
                string confirmValue = Request.Form["confirm_value"];
                if (confirmValue == "Yes")
                {
                    MyAlert("Yes clicked");
                }
                else
                {
                    MyAlert("No clicked");
                }
            }
            else
            {
                MyAlert("No Text found.");
            }
        }

Now the problem is since I have the function Confirm() in the onClientClick event, the confirm dialog appears the moment the user clicks the button which is actually not intended, instead it should appear only when a string is found in txtID.

What I have tried:

I tried to modify the code by removing the onClientClick event as follows:
<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" ></button>

protected void lnkBtn_Click(object sender, EventArgs e)
        {
            string str=gdView.HeaderRow.Cells[8].Text;
            System.Web.UI.WebControls.TextBox txtID = (System.Web.UI.WebControls.TextBox)gdView.Rows[0].Cells[8].FindControl(str);
            
            if (txtID.Text != "")
            {
                
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "confirm", "Confirm();", true);
                string confirmValue = Request.Form["confirm_value"];
                
                if (confirmValue == "Yes")
                {
                    MyAlert("Yes clicked");
                }
                else
                if (confirmValue == "No")
                {
                    MyAlert("No clicked");
                }
            }
        }

Here also it is not working properly. The confirmValue is getting the value but using it only during the subsequent click of the button. It is not passing value stored during the present click event instead the previously stored value is passed to if block. Please help to find what should I do get it right.
Thanks.
Posted
Updated 31-Dec-19 19:10pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to bear in mind the asp.net page life cycle. Your .net code runs in its entirety and generates html which is sent to the client to interpret so you can't mix server code and client code flow, which is effectively what you want to do. There are a few techniques on how you can get a client to confirm you want to continue with something server-side here

Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]
   
Comments
L B Choudhury 21-Jun-17 14:39pm
   
Thanks a lot. It really saved my day.
Rate this:
Please Sign up or sign in to vote.

Solution 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CsBaseCore = Lib.LibCS.Base.Core;
using TelerikWebUI = Telerik.Web.UI;
using CSBizEntityTransactionAdmin = Lib.LibCS.Business.Entity.Transaction.Admin;

namespace bERPWebClient.Applications.OnlineMeetingRoomBooking
{
    public partial class MeetingRoomPopupGuest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["RoomBookingID"] != null)
                {
                    String strBookingID = Session["RoomBookingID"].ToString().ToLower();
                    BindExternalGuestInfoGrid(strBookingID);
                    BindInternalEmployeeInfoGrid(strBookingID);
                }
            }
            catch (System.Exception se)
            {
                string script = "alert('" + se.Message.Replace("'", "\"").Replace(@"\", @"\\") + "');";
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", script, true);
            }
        }


        void BindExternalGuestInfoGrid(String pstrBookingID)
        {
            IList lstGuestInfo = new List();
            CSBizEntityTransactionAdmin.MeetingRoomBooking objMeetingRoom = new CSBizEntityTransactionAdmin.MeetingRoomBooking();
            lstGuestInfo = objMeetingRoom.GetExternalGuestInfo(pstrBookingID);
            rgridDisplayGuestInfo.DataSource = lstGuestInfo;
            rgridDisplayGuestInfo.DataBind();         
        }

        void BindInternalEmployeeInfoGrid(String pstrBookingID)
        {
            IList lstEmployeeInfo = new List();
            CSBizEntityTransactionAdmin.MeetingRoomBooking objMeetingRoom = new CSBizEntityTransactionAdmin.MeetingRoomBooking();
            lstEmployeeInfo = objMeetingRoom.GetInternalEmployeeInfo(pstrBookingID);
            rgridDisplayEmployeeInfo.DataSource = lstEmployeeInfo;
            rgridDisplayEmployeeInfo.DataBind();
        }

    }
}

//Contact number of the person who is booking the room
   
Comments
Dave Kreskowiak 1-Jan-20 1:19am
   
This doesn't have anything to do with the question that was asked two years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100