Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi firends,
I have a button click event
in it
after satisfying some condition, I should get a confirmation dialog.
When I click "OK" button of that confirmation dialog, I want to proceed further in my code on server side.

protected void btnSelectInvioces_Click(object source, EventArgs e)
       {
           try
           {
                // some code here
                 ...
                 ...
                if(a==b)
                {
                   // open confirmation box
                   if(OK)  // i click ok button of confirm box
                   {
                        // proceed further...
                   }
                }
           }
catch()
{

}
}


I tried using
btnSelectInvioces.Attributes.Add("javascript:window.confirm","OnClcik");

Also tried
MIDL
string strScript = string.Empty;
   strScript = "javascript:window.confirm();";
   System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "lnkReturn", strScript, true);


But nothing worked.
Help me friends,

Regards,
Lok..
Posted

You can't show a message box in ASP.NET: it will try to show on the server, not the client. This will annoy the webserver administrator...
Use the Alert instead:
Response.Write("<script>alert('Hello')</script>");
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 3:11am    
A very funny way to bug the admin :-)
My 5.
--SA
Lokesh Zende 18-Feb-11 4:03am    
This is not what I wanted OriginalGriff.
It is possible to open popup from server side.
I just cant find it how do we do it.
Hi,

Here i am call a client side function and store its result in hidden field control.In button click event get that hidden value and checked out the condition.

Try this...i think its useful to you...

In Source Page.....
<asp:button id="btnOk" runat="server" text="Ok" onclientclick="Confirm();" onclick="btnOk_Click" xmlns:asp="#unknown" />
<asp:hiddenfield id="hdfValue" runat="server" xmlns:asp="#unknown" />


Call this function in OnClientClick of the button
<script type="text/javascript">
function Confirm()
{
   var sResult = confirm("Do you want to Proceed?");
   document.getElementById("hdfValue").value = sResult; 
}
</script>

In Code page...
protected void btnOk_Click(object sender, EventArgs e)
{
    // If Ok means
    if(hdfValue.Value.Equals(true))
    {
         // Your Code
    } 
    else
    {
         // Your Code
    } 
}



Cheers :)
 
Share this answer
 
Comments
Lokesh Zende 19-Feb-11 5:25am    
Hi T.Saravanann,
I already tried by this way.
It works but not what I wanted..
It first opens a popup and then I go to server side code.
What I want is,
1.I click a button,
2.xecute some code on btn_Click event,
3.at certain condition in code (like if(a==b)), open pop up
Dont proceed further unless and untill I click Ok/Cancel button on popup.
chetna2810 22-Jun-16 3:21am    
Hey dear,

Can you solve your problem?.. If, Yes then please share the solution. So that i can go through once.
One of the ways I deal with this is using the jquery alerts[^] plugin for jQuery

You can create a little wrapper function around the confirmation function as follows

C#
SomeNamespace.doConfirm = function (source, message, title) {
    jConfirm(message, title, function(r) {
        if (r == true) {
            __doPostBack(source.id, '');
        }
    });
    return false;
};


NB: The __doPostBack script function is the standard way asp.net web forms postback to the server and invoke the particular event handler for the control that raised the event.

Now, you can attach the script to your button in your page Load

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.btnSelectInvioces.Attributes.Add("onclick", "return SomeNamespace.doConfirm(this, 'Are you sure you want to do this?', 'Prompt Title');");
    }
}


Now when you click on the button, you'll see a confirmation dialog on the client. No postback has happened at this point.

If you click OK, then the standard btnSelectInvioces_Click event handler will be invoked. If you click No, no postback will occur
 
Share this answer
 
v2
Comments
Lokesh Zende 19-Feb-11 5:27am    
Hey Dylan Morley,
I have never used Jquery. So I dont know how do we do it.
But thanks for your helping hand.
chetna2810 22-Jun-16 3:23am    
@Dylan Morley, The above JQuery above link is not working can u post this again. I am facing the same problem.
Instead of using Response.Write, I would suggest you try using:
Page.RegisterClientScriptBlock Method [^] OR Page.RegisterStartupScript Method[^]

Using them, you can inject the scripts into a page from server side and based on certain condition (and specific to page.)
For samples on how to use it: How to Use RegisterClientScriptBlock & RegisterStartupScript[^]
Sample 2[^]
 
Share this answer
 
Comments
Lokesh Zende 19-Feb-11 5:19am    
Hi Sandeep,
Thanks for your reply.
I tried using RegisterClientScript.
What is happening is, it is executing my whole code and then giving me pop up.
I want to open popup at certian point. When it opens, further code should not be xecuted unless and untill I click OK/Cancel button of that confirmation box
Sandeep Mewara 19-Feb-11 13:35pm    
As such your implementation should had handled it, like the confirm via Javascript injected for that specific condition. Its difficult to tell that coding way without seeing your code and the scenario.

For now, have a look at these control, they should help:
http://www.codeproject.com/KB/ajax/popupboxes.aspx
http://www.codeproject.com/KB/webforms/NingLiangSimpleControl.aspx

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900