Click here to Skip to main content
15,353,818 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi all,

Can anyone suggest me how to write the confirm box in code behind
and i want to get the return value whether the user clicked 'ok' or
'cancel',
Depending on that i need to process further using if else .
"I want to pass one value which is from database to confirm message."
for ex: return confirm(10 elements)
This 10 in the message is getting from database
Thats y i am using this calling from code behind.


Thanks in advance,
Deepthi
Posted
Updated 21-Apr-11 23:04pm
v2

hi deepthi ,

try this one this will help ; use AjaxControlToolKit DLL

C#
<cc1:ModalPopupExtender ID="MPE"  runat="server" CancelControlID="btnCancel" OkControlID="btnOk"
    TargetControlID="HiddenField1" PopupControlID="MessageDiv" BackgroundCssClass="popupbg">

<asp:HiddenField ID="HiddenField1" runat="server" />
<div  runat="server" id="MessageDiv" class="popupdiv" style="width: 450px; display: none">   
    <table width="100%">
        <tr>
            <td>
                <asp:Label ID="lblMsg" runat="server">
            </td>
        </tr>
        <tr></tr>
        <tr>
            <td align="center">
                <asp:Button ID="btnOK" runat="server" Text="Ok" OnClick="btnOK_Click" />
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
            </td>
        </tr>
    </table>
    <br />   


//-----On code behind

on button click event
---------------------------
C#
lblMsg.Text ="Your Message";
MPE.Show();
   
v3
Comments
Deepthi Aravind 27-Apr-11 7:44am
   
Hi Anvas,
Finally i found it. Its wrkng fine..
Thank u very much ...
My 5 for u...
If I understand, You need bellow
in your aspx page write following
XML
<script language="javascript" type="text/javascript">
    function confirmSearch() {
        if (confirm("Are you sure you want to search!"))
            return true;
        return false;
    }
</script>


and in Button do the followings

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return confirmSearch()" OnClick="searchfromDate" />


Hope it help
   
Comments
ShreeGaneshPatil1988 24-Aug-12 3:14am
   
not working
Abdul Rahman Hamidy 25-Aug-12 0:49am
   
write the script inside the aspx page, you can write it in heading or body.. if you design a simple page having one button and the code i write, that definitly works because i checked it.
[no name] 4-Sep-12 7:58am
   
ya, its working

my 5
Hey,

Consider the code below :
C#
string messageFromDatabase = "Are you sure you want to do this?";
StringBuilder javaScript = new StringBuilder();
javaScript.Append("<script type=\"text/javascript\">\n");
javaScript.Append("function doConfirm(){\n");
javaScript.Append("\tif (confirm('");
javascript.Append(messageFromDatabase);
javaScript.Append("'){\n");
javaScript.Append("\t\t// code when the user clicks OK\n");
javascript.Append("\t} else {\n");
javaScript.Append("\t\t// code when the user clicks cancel\n");
javascript.Append("\t}\n");
javaScript.Append("}\n");
javaScript.Append("</script>\n");


You can add the script to the page in Page_Load()
C#
Page.ClientScript.RegisterClientScriptBlock(typeof(Page),
    "confirmationScript", javaScript.ToString());


Hope it helps,

Cheers, Eduard
   
v2
Comments
Deepthi Aravind 22-Apr-11 5:30am
   
hi Eduard.
I cant get the solution...
My actual requirement is,
In my page, On clicking of search button i need to show this confirmation message.This message is from database.
After that i want to know user clicked is yes or no.
if user clicked yes,i want to retrieve list of items from database
Eduard Keilholz 22-Apr-11 5:57am
   
I don't see why you cannot get that working with the code above. Have you tried to implement it?
Deepthi Aravind 22-Apr-11 6:26am
   
ya..
I hv added this line to my buttin click event
string messageFromDatabase = "Are you sure you want to do this?";
StringBuilder javaScript = new StringBuilder();
javaScript.Append("<script type=\"text/javascript\">\n");
javaScript.Append("function doConfirm(){\n");
javaScript.Append("\tif (confirm('");
javascript.Append(messageFromDatabase);
javaScript.Append("'){\n");
javaScript.Append("\t\t// code when the user clicks OK\n");
javascript.Append("\t} else {\n");
javaScript.Append("\t\t// code when the user clicks cancel\n");
javascript.Append("\t}\n");
javaScript.Append("}\n");
javaScript.Append("</script>\n");

and added the clientscript also..
Eduard Keilholz 22-Apr-11 10:03am
   
Did you READ my answer? Add the script in the page load event and add a client script handler that calls the doConfirm function
try this one;
MIDL
ScriptManager.RegisterStartupScript(this,this.GetType(), "Message","confirm('Number of rows ');", true);
   
Comments
Deepthi Aravind 22-Apr-11 5:32am
   
in this solution y can i get the result,ie,Ok or cancel
romiltonfdo 9-Aug-12 0:44am
   
ScriptManager.RegisterStartupScript(this,this.GetType(), "Message","confirm('Number of rows ');", true);

Am using the above code.Its working fine. But i have one doubt suppose i ll select ok means do some thing and cancel means do something. How can i check whether the click is ok or cancel in code behind...
ShreeGaneshPatil1988 24-Aug-12 3:13am
   
how to get its confirmation in this case please tell me..

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