sk7730
sk7730

Reputation: 736

call OnClientClick and Onclick together in c# Button Event

I have a Asp.Net c# application in which, having Logout Button on mAsterpage.

I am trying to alert confirmation window on click of LogOut. If i choose Yes, then I will redirect it into Log-in Page.

So I have tried below. OnClient Click I called below JavaScript Function.

   <script type = "text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to save data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>

On Button Click I have wriiten Below code.

         protected void ImgbtnLogOut_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                Session.Clear();
                Session.Abandon();
                Response.Redirect(ConfigurationManager.AppSettings["LogoutURL"].ToString());
                //Server.Transfer(ConfigurationManager.AppSettings["LogoutURL"].ToString());
            }
            else
            {
                //Do Nothing
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }

I am getting an error Unable to evaluate Expression, The code is Optimized....error in below line. And Page is not being redirected to required page.

                Response.Redirect(ConfigurationManager.AppSettings["LogoutURL"].ToString());

can anyone please suggest, how can i achieve that.

Upvotes: 0

Views: 607

Answers (1)

Gautam Kumar Sahu
Gautam Kumar Sahu

Reputation: 621

This confirm box help you to get correct response with having Ajax.

java script code for geting confirm value.

<script type="text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Do you want to proceed continue ?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>

On C# Code for geting exect data.

string confirmValue = Request.Form["confirm_value"];
          string[] Value_Confirm = confirmValue.Split(',');
          if (Value_Confirm[Value_Confirm.Length-1] == "Yes")
          {}

or check here : http://www.codeproject.com/Answers/1070495/Ask-yes-no-cancel-window-in-csharp#answer6

Upvotes: 1

Related Questions