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.Register StartupScr ipt. 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
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
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
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?
ASKER
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
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())
StringBuilder.Append("conf irm('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).
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).
ASKER
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:chkAct ion(<% = txtLastName.ClientID%>).va lue;"
thx
Verdy
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:
The code I have in becomes generated as:
onclick="javascript:chkAct
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;"
<button OnClientClick="javascript: chkAction( <% = txtLastName.ClientID%>)">
or if not working try:
<button onclick="javascript:chkAct ion(<% = txtLastName.ClientID%>)">
or if not working try:
<button onclick="javascript:chkAct
ASKER
Hi
When I use :
onclick="javascript:chkAct ion(<% = txtLastName.ClientId%>);"
the left arrow (less than sign) gets changed to
onclick="javascript:chkAct ion(<% = txtLastName.ClientId%>);"
I then get a syntax error. Is there any way around this?
cheers
Verdy
When I use :
onclick="javascript:chkAct
the left arrow (less than sign) gets changed to
onclick="javascript:chkAct
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:chkAct ion('<% = txtLastName.ClientId%>');"
onclick="javascript:chkAct
ASKER
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
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?
'<% Response.Write(txtLastName
try this?
ASKER
Thanks so much for your help, but the problem is that '<' is still rendering as '<'. 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.Register StartupScr ipt 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/10 15724/1364 852.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
I read a different Internet post at http://forums.asp.net/p/10
yours in frustration
Verdy
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(tx tLastName. ClientID); No error but dialogue box advises 'You are changing the name to null'
var txtlastname = document.getElementById("t xtLastName ").value; ERROR on click Object required.
Not sure if we are just pouring in time for no reason??
thx
Verdy
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("<
var txtlastname = document.getElementById(tx
var txtlastname = document.getElementById("t
Not sure if we are just pouring in time for no reason??
thx
Verdy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..
confirm ("Are you really sure")
{
// true, here code for true
}
{
// false, here code for false
}
Is that what you want ??