Click here to Skip to main content
15,890,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all,

I have code in asp.net c# where I need to call the YesNo dialog in server side. Actually, the code will check the database and for certain condition it will ask the user does he want to continue or not? Now, I have got some functions or java scripts but all are in client side not server side. Can you please suggest the way to perform so. Thanks for your always help and cooperation.

BR//
Posted

If you did not get it yet, you never "call" any UI-related element on server side. You always do it on client side. Server side can only generate HTML text or part of it (or other resources) in HTTP response, but the UI itself always operates in the browser. What happens when the user do some action in browser, say, chooses Yes or No, depends on the purpose of handling this event. Sometimes everything happens on the client side. Everything else is performed through another HTTP request, always. This request could request the same or new URI, or send a request via a Web form (also with some or another URI called "action") or, importantly, Ajax HTTP requests. Assuming your "check the database" is performed on the server side (which is the most reasonable way), you need to generate another HTTP request in one of these ways.

So, we are getting back to client-side (!) UI and hence JavaScript. The simplest way to request confirmation is calling confirm:
JavaScript
var confirmation = confirm("Do you want to continue?");
if (confirmation) sendSomeHttpRequest(/* ... */);

The only problem, the dialog will show not "Yes"/"No", but "OK"/"Cancel" (the "if" branch with the call shown above will execute after the press on "OK"), which could be fine in most cases.

For more serious features and flexibility, you can use jQuery Dialog (highly recommended). You can see how it works here: http://jqueryui.com/dialog[^].

But I would add: requesting a user for something is usually not the best design, by far. But suggesting the alternatives need going into much finer detail of the problem.

—SA
 
Share this answer
 
v3
Comments
Member 9646334 19-Feb-15 17:37pm    
I have got one solution that is:
Page.ClientScript.RegisterStartupScript(this.GetType(), "Confi", "var confirm_value; if(confirm('Are you sure?') == true){ confirm_value.value ='YES';}else{confirm_value.value ='NO';}", true);
It gives the dialog from the server side. Problem is that how to handle OK or cancel from the user input? I tried to define var confirm but it does not work. Any idea how to fix it?
Sergey Alexandrovich Kryukov 19-Feb-15 17:43pm    
This is the same thing. No, it does not "give the dialog from the server side". Doesn't the name "RegisterStartupScript" tell you what it really does?
I already answered in full how to handle OK or Cancel. Did you really read my answer? Please look carefully.
Did you get it and are you going to accept the answer formally? In all cases, your follow-up questions will be welcome.
—SA
Member 9646334 19-Feb-15 17:48pm    
OK
Back to your answer which I read it carefully but could not get it. What do you mean by send httprequest?
Sergey Alexandrovich Kryukov 19-Feb-15 19:14pm    
Hm... I don't even know where to start. Do you know how the Web works at all? Do you know Ajax? Web Forms? Or just anchor (a href="...")? This is how you send HTTP request, and all the server side does is processing it and sending back HTTP response. How it's possible to do Web development without it?
—SA
King Fisher 19-Feb-15 23:34pm    
well Clear +5 :)
Here is another solution and it works

XML
//code similar to following need on button click
if (some conditional expression is true)
{
                    mpeConfirmation.Show();  //please make note of this
                    return;
}

//Following I have defined in Markup and gets displayed from server-side as mentioned in the code snippet above

<asp:Panel ID="pnlConfirmation" runat="server" Width="600px"  Style="display: none" EnableViewState="true">
            <div runat="server" id="divConfirmation" class="ModalPopupHeader">
    Confirm?
            </div>
            <uc:Confirmation ID="ucConfirm" runat="server" />
        </asp:Panel>
        <asp:Button ID="btnDummyConfirmation" runat="server" Style="display: none;" />
        <ajaxToolkit:ModalPopupExtender ID="mpeConfirmation" runat="server" BehaviorID="mpeConfirmationBehavior"
            TargetControlID="btnDummyConfirmation" RepositionMode="None" PopupControlID="pnlConfirmation"
            PopupDragHandleControlID="divConfirmation" DropShadow="true" BackgroundCssClass="modalBackground">
</ajaxToolkit:ModalPopupExtender>


ucConfirm is a UserControl created with required layout and buttons. 'Yes', 'No' I have defined two events inside this control like follows

  /// <summary>
       /// Event for On OK button clicked
       /// </summary>
       public event EventHandler OKClicked;

       /// <summary>
       /// Event for On Cancel button clicked
       /// </summary>
       public event EventHandler CancelClicked;


private void OnOKClicked(object sender, EventArgs e)
       {
           if (OKClicked != null)
           {
               OKClicked(sender, e);
           }
       }

       private void OnCancelClicked(object sender, EventArgs e)
       {
           if (CancelClicked != null)
           {
               CancelClicked(sender, e);
           }
       }

       protected void btnOk_Click(object sender, EventArgs e)
       {
           OnOKClicked(sender, e);
       }

       protected void btnCancel_Click(object sender, EventArgs e)
       {
           OnCancelClicked(sender, e);
       }

I've consumed these events in the user control wherever I needed the Confirmation message functionality initiated from Server side, rather than on button client-click. Since I did not want the confirmation to show always, but only on certain condition I took this approach


Hope that helps
Thanks
 
Share this answer
 
 
Share this answer
 
Is this you are looking for?

VB
if (System.Windows.Forms.MessageBox.Show("Do you want to continue","Confirmation alert",System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes){//do your function here}
 
Share this answer
 

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