Groups | Blog | Home
all groups > asp.net > january 2008 >

asp.net : injecting javascript fails all the time?


Andy B
1/5/2008 6:50:26 PM
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/aspnet/JavaScript_with_ASP_NET_2_0_Pages_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.





Mark Rae [MVP]
1/6/2008 12:09:51 AM
[quoted text, click to view]

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

[quoted text, click to view]

Secondly, there's really no need to use a StringBuilder for this...

[quoted text, click to view]

Thirdly, you're using the wrong method...

[quoted text, click to view]

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...
:-)

[quoted text, click to view]

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
Andy B
1/6/2008 7:16:10 AM
I tried the below code just as it is shown here and it still doesnt do
anything at all. Any other ideas?



[quoted text, click to view]

Mark Rae [MVP]
1/6/2008 12:34:07 PM
[quoted text, click to view]

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
Andy B
1/6/2008 10:36:54 PM
Nope... breakpoints aren't noticed and looking at the source shows that
there are no <script> blocks anywhere...


[quoted text, click to view]

Andy B
1/7/2008 1:11:57 AM
Here is the code for the method AddNews that might be in question:

public bool AddNews(DateTime Date, String subjectm, String Body) {

NewsDataSet.NewsArticlesDataTable NewsArticles = new
NewsDataSet.NewsArticlesDataTable();

NewsDataSet.NewsArticlesRow NewsArticle = NewsArticles.NewNewsArticlesRow();

NewsArticle.Date=Date;

NewsArticle.Text=Body;

NewsArticle.Title=subjectm;

NewsArticles.AddNewsArticlesRow(NewsArticle);

int RowsAffected = NewsArticlesAdapter.Update(NewsArticles);

return RowsAffected==1;

}



Might there ge something wrong here? The code runs fine, its just getting
the popups to work somehow...





Mark Rae [MVP]
1/7/2008 3:49:10 AM
[quoted text, click to view]

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
Andy B
1/7/2008 8:20:01 AM
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?


[quoted text, click to view]

Mark Rae [MVP]
1/7/2008 11:17:44 AM
[quoted text, click to view]

There might... step through it and check...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Mark Rae [MVP]
1/7/2008 3:10:38 PM
[quoted text, click to view]

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
AddThis Social Bookmark Button