Solved

Question about quotes and apostrophes when using javascript in c#

Posted on 2007-11-16
9
997 Views
Last Modified: 2008-02-01
I have this code and just need to know how the quotes and apostrophes go when using javascript in c#:

It works until I add the  <input id=finish type=button value=Finish style=width: 165px onClick=window.location=default.aspx/>  I think it is failing at the window.location=default.aspx bit.

div_input_form_change_innerhtml += "   '<table width=100%><tr><td align=center>Your application has been received.<hr /></td></tr><tr><td align=right><input id=finish type=button value=Finish style=width: 165px onClick=window.location=default.aspx/></td></tr></table>';   ";

Thanks
0
Comment
Question by:scanet
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:McExp
ID: 20300160
<input id="finish" type="button" value="Finish" style="width: 165px;" onClick="window.location=default.aspx;" />
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 20300250
This is kind of splitting hairs, but this would be better as:
div_input_form_change_innerhtml += "<input id=\"finish\" type=\"button\" value=\"Finish\" style=\"width: 165px;\" onclick=\"window.location='default.aspx';\" />";

Open in new window

0
 

Author Comment

by:scanet
ID: 20300663
Nither worked im affraid:

the div_input_form_change_innerhtml += " " is c# and I want to put this into it:

'<table width=100%><tr><td align=center>Your application has been received.<hr /></td></tr><tr><td align=right><input id=finish type=button value=Finish style=width: 165px onClick=window.location=default.aspx/></td></tr></table>';

as I said if I just put this into it, it works fine:

'<table width=100%><tr><td align=center>Your application has been received.<hr /></td></tr><tr><td align=right></td></tr></table>';

But when I add the button and try to put in the windowslocation it needs quotes but is not working I thought you had to add multiple quotes and apostrophes to get it to work but dont know the combination.

Thanks

0
 
LVL 16

Expert Comment

by:McExp
ID: 20300907
What is contained in "div_input_form_change_innerhtml" before you append the string?
0
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

 
LVL 16

Expert Comment

by:McExp
ID: 20300953
In my first example the string was not escaped, as I wasn't sure exactly what you where trying to do.

Making the assumption that you are trying to create a javascript command that sets the innerhtml here is a solution that might work.

div_input_form_change_innerhtml += " '<table width=\"100%\"><tr><td align=\"center\">Your application has been received.<hr /></td></tr><tr><td align=\"right\"><input id=\"finish\" type=\"button\" value=\"Finish\" style=\"width: 165px;\" onClick=\"window.location=''default.aspx'';\"/></td></tr></table>';";

I have used escaped double quotes to get them into the c# string and then two single quotes arround the default.aspx to escape that for when it reaches your javascript.
0
 

Author Comment

by:scanet
ID: 20301041
Still not working, it is saying that it is expectiing a ; ,here is the whole peice of script with your latest answer McExp:

// Define the JavaScript function for the specified control.
string div_input_form_change_innerhtml = "   <script language='javascript'>document.getElementById('div_input_form').innerHTML =   ";

div_input_form_change_innerhtml += "  '<table width=\"100%\"><tr><td align=\"center\">Your application has been received.<hr /></td></tr><tr><td align=\"right\"><input id=\"finish\" type=\"button\" value=\"Finish\" style=\"width: 165px;\" onClick=\"window.location=''default.aspx'';\"/></td></tr></table>'; ";

div_input_form_change_innerhtml += "   </script>   ";

// Add the JavaScript code to the page.
ClientScript.RegisterStartupScript(typeof(Page), "div_input_form_change_innerhtml", div_input_form_change_innerhtml);
0
 
LVL 16

Expert Comment

by:McExp
ID: 20301131
Where do you get the expecting ; error is it in the browser or on compile?

One thing to note, you should use this.GetType() instead of typeof(page), in most cases it will always return the same, however if you ever use an alternative class derived from page it will be setting it as the wrong type.

ClientScript.RegisterStartupScript(typeof(Page),
0
 
LVL 16

Accepted Solution

by:
McExp earned 50 total points
ID: 20301199
The following should work for you:

        string div_input_form_change_innerhtml = "document.getElementById('div_input_form').innerHTML = '<table width=\"100%\"><tr><td align=\"center\">Your application has been received.<hr /></td></tr><tr><td align=\"right\"><input id=\"finish\" type=\"button\" value=\"Finish\" style=\"width: 165px;\" onClick=\"window.location=\\'default.aspx\\';\"/></td></tr></table>'; ";

        // Add the JavaScript code to the page.
        ClientScript.RegisterStartupScript(this.GetType(), "div_input_form_change_innerhtml", div_input_form_change_innerhtml,true);
0
 

Author Comment

by:scanet
ID: 20301410
Thanks McExp that worked a treat.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

13 Experts available now in Live!

Get 1:1 Help Now