Hai....

"i want used confirm in javascript in code behind"

i am used confirm button ,in code behind.

if the confirm button worked

the function is return the true...

else

the function return the false..

i want to get true and false value for my further proces..

this my code..

Dim popupScript6 As String = "<script language='javascript'>var agree= confirm('Are you sure to cancel the ticket?');if(agree){ return true};else {return false;}</script>"
        'Page.RegisterStartupScript("PopupScript6", popupScript6)

in my codebehind window i want to get "agree" value....

in this popupScript6 used confirm coding is worked...but i wrote return word..this is nothing done..

i ma used return word is not worked..

please how can use this statement..

how can i get return value..

because i got the return value..

then my next code if(true) the redirect to another page...

so please hrlp me...

i am used this code also code..

but the same process done this also return word is used in not working..

this code..

Dim str As StringBuilder = New StringBuilder
        str.Append("<script language='javascript'>")

    str.Append("var agree= confirm('Are you sure to cancel the ticket?');")
  str.Append("if(agree){")

  str.Append("return true;}")
        str.Append("else {")

        str.Append("return false;}")
        str.Append("</script>")
       RegisterStartupScript("TicketNumber", str.ToString)

so please help me...

Dani AI

Generated

A short clarification for and follow‑ups to the replies from and : the codebehind runs on the server and finishes before the browser ever executes the confirm dialog. Emitting a top-level script that contains return true does not return that value to the server-side method that already ran. The confirm result only exists in the browser; to act on it server-side the browser must send it back (hidden field, form submit, or AJAX).

When the only goal is to prevent the postback when the user cancels, attach the confirm to the button click on the client side. Example markup:

<asp:Button ID="btnCancel" runat="server" Text="Cancel"
    OnClick="btnCancel_Click"
    OnClientClick="return confirm('Are you sure to cancel the ticket?');" />

To capture the choice in codebehind (true/false), store the result into a HiddenField immediately before submit and let the postback continue. Example pattern (VB):

' markup: <asp:HiddenField ID="hdnConfirm" runat="server" />

' codebehind (Page_PreRender)
btnCancel.Attributes("onclick") =
  String.Format("if(!confirm('Are you sure to cancel the ticket?')){{return false;}} " &
                "document.getElementById('{0}').value='true';", hdnConfirm.ClientID)

' server event
If hdnConfirm.Value = "true" Then
  ' proceed with cancellation
Else
  ' user cancelled
End If

If an UpdatePanel is in use, prefer ScriptManager.RegisterStartupScript or reassign the button attribute on each render (partial postbacks can drop client attributes). As noted, Page.RegisterStartupScript does not reliably survive partial renders. Final tip: always use the control ClientID when writing client code and escape quotes in strings; avoid expecting an inline script to synchronously "return" a value up to the already-executed server method.

Recommended Answers

All 5 Replies

Put your JS-code into some function like
function MyFunction()
{
... your code
}

And call this function by onClick event of your button (onClick="if(MyFunction()) { window.location.href='where.you.want.to.redirect'; }")

Hi dear....

This is my 1st Post...

RegisterStartupScript("Alert", "<script language=""javascript"">Confirm('Are you sure ');</script>")

this is the Best way to call javascript inside the Code....this should b written in Code behind file mean in . file....

I hope that you get Help from the Post...

Bye

Regards
Pushkar

i am using update pannel ,how to use this function at code behind

i am using update pannel ,how to use this function at code behind

Hi Raja.
Follow this link

When u want to use RegisterStartUpScript within update panel u need to Call function from ScriptManager not from ClientScript...

http://developers.de/blogs/damir_dobric/archive/2007/02/12/RegisterStartupScript-and-RegisterClientScriptBlock-can-work-with-UpdatePanel_3F002100_.aspx

ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), Msg2, True)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.