Link to home
Start Free TrialLog in
Avatar of verdante
verdante

asked on

What is the syntax for including a Confirm statement into this javascript string?

I am attempting to incorporate a javascript function into my VB.NET project using Page.ClientScript.RegisterStartupScript. I have managed to adapt some other code and have got as far as using a javascript Alert to display the value of my asp.textbox control. But I want to provide a Confirm statement so that the value of the textbox can be confirmed by the user prior to saving.  So I am hoping someone can help me with the syntax required. My code is attached.
kind regards
Verdy
Partial Class NCACCH_Jtest
    Inherits System.Web.UI.Page
    Private Const CrLf As String = _
        Microsoft.VisualBasic.Constants.vbCrLf
    Private Const Tab As String = _
        Microsoft.VisualBasic.Constants.vbTab
    Private Const Quote As String = """"
 
    Public Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Confirm(Me.txtLastName)
    End Sub
 
    Private Sub Confirm( _
        ByVal Control As System.Web.UI.Control)
        Dim StringBuilder As System.Text.StringBuilder
        StringBuilder = New System.Text.StringBuilder()
        StringBuilder.Append(CrLf)
        StringBuilder.Append("<script type=")
        StringBuilder.Append(Quote)
        StringBuilder.Append("text/javascript")
        StringBuilder.Append(Quote)
        StringBuilder.Append(">")
        StringBuilder.Append(CrLf)
        StringBuilder.Append("<!--")
        StringBuilder.Append(CrLf)
        StringBuilder.Append(Tab)
        StringBuilder.Append("window.alert(document.getElementById(")
        StringBuilder.Append(Quote)
        StringBuilder.Append(Control.ClientID)
        StringBuilder.Append(Quote)
        StringBuilder.Append(").value);")
        StringBuilder.Append(CrLf)
        StringBuilder.Append("//-->")
        StringBuilder.Append(CrLf)
        StringBuilder.Append("</script>")
        StringBuilder.Append(CrLf)
        Page.ClientScript.RegisterStartupScript( _
            GetType(String), _
            "SetFocusScript", _
            StringBuilder.ToString())
 
    End Sub

Open in new window

Avatar of Pawel Witkowski
Pawel Witkowski
Flag of Poland image

You will need to add to your code:

confirm ("Are you really sure")
{
  // true, here code for true

}


{
  // false, here code for false

}


Is that what you want ??
Avatar of verdante
verdante

ASKER

Hi
I understand the basic flow of the confirm statement but no matter what syntax I use I can't get the script to run. If I use your configuration of bracketing I end up with the following generated code:
confirm('Are You Sure?'){return true;}else{return false;}

This throws an error - expected ';'
thx
Verdy
oh no ... javascript wont work like that, you put HTML tags into javascript .. thats  a ... well .. maybe some basics of HTML and javascript would help?
sorry that was the cut and paste inserting those html tags. I've put the actual confirm code in the attached snippet. I don't think my error message would have been so kind if that was what my code looked like. I am actually looking for script that will result in pop up box asking:
You are changing the name to Smith. Is this Correct?
cheers
Verdy

    Private Sub Confirm( _
        ByVal Control As System.Web.UI.Control)
        Dim StringBuilder As System.Text.StringBuilder
        StringBuilder = New System.Text.StringBuilder()
        StringBuilder.Append(CrLf)
        StringBuilder.Append("<script type=")
        StringBuilder.Append(Quote)
        StringBuilder.Append("text/javascript")
        StringBuilder.Append(Quote)
        StringBuilder.Append(">")
        StringBuilder.Append(CrLf)
        StringBuilder.Append("<!--")
        StringBuilder.Append(CrLf)
        StringBuilder.Append(Tab)
        StringBuilder.Append("confirm('Are You Sure?'){")
        StringBuilder.Append("{")
        StringBuilder.Append("return true;")
        StringBuilder.Append("}")
        StringBuilder.Append("else")
        StringBuilder.Append("{")
        StringBuilder.Append("return false;")
        StringBuilder.Append("}")
        StringBuilder.Append("}")
        StringBuilder.Append(CrLf)
        StringBuilder.Append("//-->")
        StringBuilder.Append(CrLf)
        StringBuilder.Append("</script>")
        StringBuilder.Append(CrLf)
        Page.ClientScript.RegisterStartupScript( _
             GetType(String), _
            "SetSearch2Script", _
                StringBuilder.ToString())

Open in new window

      StringBuilder.Append("confirm('Are You Sure?'){")

you have to change this line to get string that you need (I guess name), but from code that you provided I cant tell you what you want to do when user hit yes or no. As you can see you have 2 options but you have to program them separately. I dont know what you desire exacly (of course beside of just showing this box).
Hi
Well I worked out what I was doing wrong but I need to use onclientclick. Now I am left with the problem that in using a .net master page I'm not sure how to get the value of my textbox to be read by the javascript function.  I have pasted my javascript function and the onclientclick event. Do you know what code I can put in  onclientclick to pass the txtLastName control value to (ie OnClientClick="javascript:etc ")?
 
The code I have in becomes generated as:
onclick="javascript:chkAction(<% = txtLastName.ClientID%>).value;"
thx
 
Verdy

<script type="text/javascript" language="javascript">
//
function chkAction(txtlastname) {
//window.alert("You are looking at " + txtlastname + ". Do you wish to continue?")
var ans = confirm("You are changing the name to " + txtlastname + ". Do you wish to continue?")
	if (ans){
return true
	}
	else{
	return false
	}
}
//-->
</script>
 
 
 
OnClientClick="javascript:chkAction(<% = txtLastName.ClientID%>).value;"

Open in new window

<button OnClientClick="javascript:chkAction(<% = txtLastName.ClientID%>)">

or if not working try:

<button onclick="javascript:chkAction(<% = txtLastName.ClientID%>)">
Hi
When I use :
onclick="javascript:chkAction(<% = txtLastName.ClientId%>);"
the left arrow (less than sign) gets changed to
onclick="javascript:chkAction(<% = txtLastName.ClientId%>);"
I then get a syntax error. Is there any way around this?
cheers
Verdy
 
ahh i think i know what is problem here, try this:

onclick="javascript:chkAction('<% = txtLastName.ClientId%>');"
Hi
Thanks for hanging in. We got rid of the error but it has been replaced by a literal interpretation of the string in between the ' ' rather than the value of the control - ie:

You are changing the name to <% = txtLastName.ClientId%>. Do you wish to continue?

thx
Verdy
well im only javascript expert - the question here is how to use <% bracers in ASP or .NET (dunno what is that) to make parser put there a value... but since you program in this language i though that it will be simple assignement... uh I look awhile on google (realy useful tool) and got:
 
'<% Response.Write(txtLastName.ClientId)%>'

 try this?
Thanks so much for your help, but the problem is that '<' is still rendering as '&lt'. I'm not sure where to go with this. I just wanted to include some client-side code that would allow user input in a dialogue box prior to the page being posted and processed at the server. No matter what technique I use to try to get my javascript to read first I still end up having my server-side code read anyway. If I use Page.ClientScript.RegisterStartupScript  in my server-side sub, I can see that some code following this line is executed anyway. I have a textbox that is filled with a message saying 'successful update' and that code is executed before my dialogue box is displayed??
I read a different Internet post at http://forums.asp.net/p/1015724/1364852.aspx which has just confused me even more. Maybe I am asking the wrong question. If you were to include some client-side code in a .NET application to bring up a dialogue box confirming details of changed text fields would you use the method I am attempting here or something different?
yours in frustration
Verdy
SOLUTION
Avatar of Pawel Witkowski
Pawel Witkowski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi
The misery just continues. I must say I am finding .NET pretty disappointing in integrating javascript.
I have used all of the following with the errors shown afterwards:
 
var txtlastname = '<%= txtLastName.ClientID %>'; ERROR on page load -  Name 'txtLastName' is not declared.



var txtlastname = document.getElementById("<%= txtLastName.ClientID %>").value; ERROR on page load - Name 'txtLastName' is not declared
 
var txtlastname = document.getElementById(txtLastName.ClientID);  No error but dialogue box advises 'You are changing the name to null'

var txtlastname = document.getElementById("txtLastName").value; ERROR on click Object required.
Not sure if we are just pouring in time for no reason??
thx
Verdy
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To tell the truth - I'm completly  don't know ASP :) I know many other languages and to tell you the truth - from what I see in parsing this string... well... this language SUX. But this is only mine opinion of course :) When i write something in PHP i fell no limitation at all, but programing in ASP is more confusing and iritating. The javascript I provided you should work as expected, but ASP ? brrrr that's weirdo @$@# ;) I just get used to php notation where every time I want to use PHP in html i have to use <?php ?> bracers. You cant put things directly into HTML like ASP do ... and... this is really good! It does not confusing when or whenether not to put something in <% %> bracers on html. Blah..