473,413 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

injecting javascript fails all the time?

I have the following code inside of a Wizard_Finish_Button_Click event:
NewsDB NewsHelper = new NewsDB();
if(NewsHelper.AddNews(DateTime.Now, SubjectTextBox.Text, BodyTextBox.Text))
{
StringBuilder PopUp = new StringBuilder();

PopUp.Append("<script language = 'javascript'>");

PopUp.Append("alert('Your news article was added successfully!');<");

PopUp.Append("/script>");

ClientScript.RegisterClientScriptBlock(GetType(), "PopupScript",
PopUp.ToString());

Server.Transfer("index.aspx");

}

else {

StringBuilder PopUp = new StringBuilder();

PopUp.Append("<script language = 'javascript'>");

PopUp.Append("alert('Warning! Couldn't add your news article for some
reason!');<");

PopUp.Append("/script>");

ClientScript.RegisterClientScriptBlock(GetType(), "PopupScript",
PopUp.ToString());

AddNewsWizard.ActiveStepIndex=0; //if adding failed, reset and start over...

}

When the event runs, everything works/fails like expected except for the
javascript part. For some reason it gets ignored. Any way to fix this? I got
my example from
http://dotnetslackers.com/articles/a...ges_Part1.aspx
The whole idea was to show a popup saying the add worked if it worked and
show one saying it failed if it failed.


Jan 5 '08 #1
8 1690
"Andy B" <a_*****@sbcglobal.netwrote in message
news:uH**************@TK2MSFTNGP04.phx.gbl...
PopUp.Append("<script language = 'javascript'>");
PopUp.Append("/script>");
Firstly, don't do that... For one thing, the language tag is now
deprecated... Instead, use the boolean overload which will make ASP.NET add
the script tags for you correctly:
http://msdn2.microsoft.com/en-us/library/z9h4dk8y.aspx
StringBuilder PopUp = new StringBuilder();
Secondly, there's really no need to use a StringBuilder for this...
ClientScript.RegisterClientScriptBlock
Thirdly, you're using the wrong method...
Server.Transfer("index.aspx");
Finally, if you do that, then your page is never going to get streamed to
the browser so your JavaScript will never run no matter how you inject it...
:-)
Any way to fix this?
if(NewsHelper.AddNews(DateTime.Now, SubjectTextBox.Text, BodyTextBox.Text))
{
ClientScript.RegisterStartupScript(GetType(), "PopupScript",
"alert('Your news article was added
successfully!');window.location='index.aspx';");
}
else
{
AddNewsWizard.ActiveStepIndex=0; //if adding failed, reset and start
over...
ClientScript.RegisterStartupScript(GetType(), "PopupScript",
"alert('Warning! Couldn't add your news article for some reason!');");
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 6 '08 #2
I tried the below code just as it is shown here and it still doesnt do
anything at all. Any other ideas?
Any way to fix this?

if(NewsHelper.AddNews(DateTime.Now, SubjectTextBox.Text,
BodyTextBox.Text))
{
ClientScript.RegisterStartupScript(GetType(), "PopupScript",
"alert('Your news article was added
successfully!');window.location='index.aspx';");
}
else
{
AddNewsWizard.ActiveStepIndex=0; //if adding failed, reset and start
over...
ClientScript.RegisterStartupScript(GetType(), "PopupScript",
"alert('Warning! Couldn't add your news article for some reason!');");
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 6 '08 #3
"Andy B" <a_*****@sbcglobal.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Any way to fix this?

if(NewsHelper.AddNews(DateTime.Now, SubjectTextBox.Text,
BodyTextBox.Text))
{
ClientScript.RegisterStartupScript(GetType(), "PopupScript",
"alert('Your news article was added
successfully!');window.location='index.aspx';") ;
}
else
{
AddNewsWizard.ActiveStepIndex=0; //if adding failed, reset and start
over...
ClientScript.RegisterStartupScript(GetType(), "PopupScript",
"alert('Warning! Couldn't add your news article for some reason!');");
}

I tried the below code just as it is shown here and it still doesn't do
anything at all. Any other ideas?
Is it even being reached? If you put a breakpoint on the first line, does it
get hit...?

If you do a View Source after the page has loaded, is the script there...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 6 '08 #4
Nope... breakpoints aren't noticed and looking at the source shows that
there are no <scriptblocks anywhere...

Is it even being reached? If you put a breakpoint on the first line, does
it get hit...?

If you do a View Source after the page has loaded, is the script there...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 7 '08 #5
"Andy B" <a_*****@sbcglobal.netwrote in message
news:uz****************@TK2MSFTNGP03.phx.gbl...
>Is it even being reached? If you put a breakpoint on the first line, does
it get hit...?

Nope... breakpoints aren't noticed
>If you do a View Source after the page has loaded, is the script
there...?

and looking at the source shows that there are no <scriptblocks
anywhere...
In which case, you have a much more fundamental problem, and you'll need to
look at your entire page, rather than just the JavaScript injection, to work
out why your function is not getting called...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 7 '08 #6
"Andy B" <a_*****@sbcglobal.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
Here is the code for the method AddNews that might be in question:
Might there ge something wrong here?
There might... step through it and check...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 7 '08 #7
How? when I do it loads the index.aspx for the whole site and the page is
naturally inaccessible as it is... any idea how to fix this?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Andy B" <a_*****@sbcglobal.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
>Here is the code for the method AddNews that might be in question:
>Might there ge something wrong here?

There might... step through it and check...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 7 '08 #8
"Andy B" <a_*****@sbcglobal.netwrote in message
news:u5****************@TK2MSFTNGP04.phx.gbl...
How? when I do it loads the index.aspx for the whole site and the page is
naturally inaccessible as it is... any idea how to fix this?
Put a breakpoint on the first line of the first Page_xxx method in the page
in question...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 7 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: George Sakkis | last post by:
Is there a general way of injecting code into a function, typically before and/or after the existing code ? I know that for most purposes, an OO solution, such as the template pattern, is a cleaner...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
10
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks ...
17
by: Luke Matuszewski | last post by:
Hi ! Simple question (but thus it may appear no simple answer): If i putting a script onto the page i simply could inline it in <script> element or via its src attribute so (second way): <script...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
14
by: ofiras | last post by:
Hii everyone, I'm a web programmer, but I never understood sql injecting. All I found was that you can write "a' or 'a'='a" in the password field to try to connect without knowing the password. I...
2
by: CrystalMikeMD | last post by:
Greetings, I've been at this problem for some time now and have decided to seek out some help. Essentially, this is what I have. A basic ASP.NET 2.0 page. On this page is the standard...
4
by: Andy B | last post by:
How do you add javascript to a page from codebehind? I have a method I am working on that has a few conditions in it: 1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.