injecting javascript fails all the time?

A

Andy B

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.
 
M

Mark Rae [MVP]

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!');");
}
 
A

Andy B

I tried the below code just as it is shown here and it still doesnt do
anything at all. Any other ideas?
 
M

Mark Rae [MVP]

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...?
 
M

Mark Rae [MVP]

Nope... breakpoints aren't noticed

and looking at the source shows that there are no <script> blocks
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...
 
A

Andy B

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...
 
A

Andy B

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?
 
M

Mark Rae [MVP]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top