Solved

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

Posted on 2008-11-02
16
525 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:verdante
  • 8
  • 8
16 Comments
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
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 ??
0
 

Author Comment

by:verdante
Comment Utility
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
0
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
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?
0
 

Author Comment

by:verdante
Comment Utility
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

0
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
      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).
0
 

Author Comment

by:verdante
Comment Utility
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

0
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
<button OnClientClick="javascript:chkAction(<% = txtLastName.ClientID%>)">

or if not working try:

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

Author Comment

by:verdante
Comment Utility
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
 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:wilq32
Comment Utility
ahh i think i know what is problem here, try this:

onclick="javascript:chkAction('<% = txtLastName.ClientId%>');"
0
 

Author Comment

by:verdante
Comment Utility
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
0
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
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?
0
 

Author Comment

by:verdante
Comment Utility
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
0
 
LVL 18

Assisted Solution

by:wilq32
wilq32 earned 500 total points
Comment Utility
ok lets try how they propose, just do like this:
<script type="text/javascript" language="javascript">
//
function chkAction() {
var txtlastname = '<%= txtLastName.ClientID %>';

//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>



<button onclick="javascript:chkAction()">
0
 

Author Comment

by:verdante
Comment Utility
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
0
 

Accepted Solution

by:
verdante earned 0 total points
Comment Utility
Hi wilq32
After more digging using that great tool you mentioned earlier I finally found a useful post on another site (sorry EEs). Gath Adams posted the code contained in the snippet which allows you to use the element's ID as opposed to the one that .NET generates. The second portion of the snippet includes my function which shows just the standard javascript function.
I should also add (for javascript newbies like me) that specifying 'return' in the OnClientClick event is required to prevent the OnClick event from firing:  
OnClientClick="return chkAction();"
Now I just want to award you the points for persisting with this as your leads helped me to my final solution and if you hadn't have kept posting I would have given up days ago.
cheers
Verdy
PS this post was interrupted by an extremely unusual event which I feel compelled to relay. I live in an inner-city residence in Australia and looked up from my computer to see my dog tussling with a small snake in my bedroom. Having despatched the snake I can now complete this message.

document.newGetElementById = document.getElementById;

document.getElementById = function(sElementID)

{

var oFirstTry;
 

oFirstTry = document.newGetElementById(sElementID);

if (oFirstTry)

	return oFirstTry;

else

	return document.getElementById('ctl00_ContentPlaceHolder1_' + sElementID);

}

Open in new window

0
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
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..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now