Showing posts with label page_load. Show all posts
Showing posts with label page_load. Show all posts

Monday, March 26, 2012

NEWBIE: passing error to javascript alert

Hello,
is there a way call a javascript function directly from within asp
page?
Example, here is page load event in C#

protected void Page_Load(object sender, EventArgs e){

// create error
int Var0 = 0;
try {
int iVar1 = 8/Var0;
}

catch (Exception ex){

string sExMsg = ex.Message;

//javascript works fine with string constant
Response.Write("<script type='text/javascript'>alert('In Catch');</
script>");

//can write error to page no problem
Response.Write(sExMsg);

// would like a popup with error
Response.Write("<script language='javascript'>doAlert(" + sExMsg +
");</script>");
Response.Write("<script language='javascript'>function doAlert(sExMsg)
{alert(sExMsg);}</script>");

//write error to log since I can't pop-it-up
//StreamWriter sw = File.AppendText(Server.MapPath("~/error.log"));
//sw.WriteLine(sExMsg);
//sw.Close();

}

}

Any help for a newbie is appreciated

Gi<gijeet@.yahoo.comwrote in message
news:1193076186.658316.66870@.e9g2000prf.googlegrou ps.com...

Quote:

Originally Posted by

Any help for a newbie is appreciated


ClientScript.RegisterStartupScript(GetType(), "error", "alert('In Catch');",
true);

--
Mark Rae
ASP.NET MVP
http://www.markrae.net
ClientScript.RegisterStartupScript(GetType(), "error", "alert('In Catch');",

Quote:

Originally Posted by

true);


Thanks but not sure how to use this. Where do I put this? class
level before page load event? within page load? do I need a
RegisterStartupScript for each alert I wish to use?

Gi
<gijeet@.yahoo.comwrote in message
news:1193079485.948563.150590@.i38g2000prf.googlegr oups.com...

Quote:

Originally Posted by

Quote:

Originally Posted by

>ClientScript.RegisterStartupScript(GetType(), "error", "alert('In
>Catch');",
>true);


>
Thanks but not sure how to use this. Where do I put this?


Wherever you like, so long as you don't Redirect from the page...

Basically, it will send down the JavaScript to run after the page had
loaded...

Quote:

Originally Posted by

do I need a RegisterStartupScript for each alert I wish to use?


Yes.

Incidentally, why are you actually doing this...?

--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Incidentally, why are you actually doing this...?
basically for debugging. I don't like response.writes and I'm trying
to learn javascript. but if you know of a better way to popup alerts,
let me know.
use the debugger to debug. alert debugging is asp classic style :)

"gijeet@.yahoo.com" wrote:

Quote:

Originally Posted by

Quote:

Originally Posted by

Incidentally, why are you actually doing this...?


basically for debugging. I don't like response.writes and I'm trying
to learn javascript. but if you know of a better way to popup alerts,
let me know.
>
>
>
>
>
>


This can help you:
How to Pass Messages and Actions between Server and Client
http://usableasp.net/DeveloperPage...erAndClient.htm
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
<gijeet@.yahoo.comwrote in message
news:1193076186.658316.66870@.e9g2000prf.googlegrou ps.com...

Quote:

Originally Posted by

Hello,
is there a way call a javascript function directly from within asp
page?
Example, here is page load event in C#
>
protected void Page_Load(object sender, EventArgs e){
>
// create error
int Var0 = 0;
try {
int iVar1 = 8/Var0;
}
>
catch (Exception ex){
>
string sExMsg = ex.Message;
>
>
//javascript works fine with string constant
Response.Write("<script type='text/javascript'>alert('In Catch');</
script>");
>
//can write error to page no problem
Response.Write(sExMsg);
>
// would like a popup with error
Response.Write("<script language='javascript'>doAlert(" + sExMsg +
");</script>");
Response.Write("<script language='javascript'>function doAlert(sExMsg)
{alert(sExMsg);}</script>");
>
//write error to log since I can't pop-it-up
//StreamWriter sw = File.AppendText(Server.MapPath("~/error.log"));
//sw.WriteLine(sExMsg);
//sw.Close();
>
}
>
}
>
Any help for a newbie is appreciated
>
Gi
>


"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@.mMvVpPsS.orgwrote in
message news:%23DynzGXFIHA.4880@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

This can help you:
How to Pass Messages and Actions between Server and Client
http://usableasp.net/DeveloperPage...erAndClient.htm


Not much use in this particular case, though, as the OP's requirements for
this were for debugging a la ASP Classic...

--
Mark Rae
ASP.NET MVP
http://www.markrae.net

newbie: Problem getting the querystring!

hey

asp.net 2.0

in the page_load event of a web page I place this code:
NameValueCollection parameters = Request.QueryString;
string username = parameters[0].ToString();

The URL has a querystring, for example like this: Default.aspx?user=newbie

But this code don't get the querystring, the parameters collection gets no
entries from this line "NameValueCollection parameters =
Request.QueryString;"

What am I doing wrong here?

JeffJeff wrote:

Quote:

Originally Posted by

hey
>
asp.net 2.0
>
in the page_load event of a web page I place this code:
NameValueCollection parameters = Request.QueryString;
string username = parameters[0].ToString();
>
The URL has a querystring, for example like this: Default.aspx?user=newbie
>
But this code don't get the querystring, the parameters collection gets no
entries from this line "NameValueCollection parameters =
Request.QueryString;"
>
What am I doing wrong here?
>
Jeff


string username = Request.QueryString["user"];
Best bet is to access the querystring collection directly. I'm not sure if
it's something that can do like this, as normally a lot of collections are
copied using the CopyTo method, but this is usually done for arrays not
specialized collections.

Your best bet though is to access it directly from the querystring. Also,
don't use positional identifiers to determine a parameter, it's too easy to
change the order that things will appear in so it's best to use the keyname
instead. Also, don't forget to test for null first or else it'll bomb out
when you run into a case where it's an empty value, that's the most common
gatcha when using the querystring.

--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"Jeff" <it_consultant1@.hotmail.com.NOSPAMwrote in message
news:udZYNHc%23GHA.4472@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

hey
>
asp.net 2.0
>
in the page_load event of a web page I place this code:
NameValueCollection parameters = Request.QueryString;
string username = parameters[0].ToString();
>
The URL has a querystring, for example like this: Default.aspx?user=newbie
>
But this code don't get the querystring, the parameters collection gets no
entries from this line "NameValueCollection parameters =
Request.QueryString;"
>
What am I doing wrong here?
>
Jeff
>


Default.aspx?user=newbie
string username = Request.QueryString["user"]; gives username a NULL
value...

Any more suggestions?

"Simon Rigby" <google@.simonrigby.co.ukwrote in message
news:1161951708.089584.264150@.m7g2000cwm.googlegro ups.com...

Quote:

Originally Posted by

>
Jeff wrote:

Quote:

Originally Posted by

>hey
>>
>asp.net 2.0
>>
>in the page_load event of a web page I place this code:
>NameValueCollection parameters = Request.QueryString;
>string username = parameters[0].ToString();
>>
>The URL has a querystring, for example like this:
>Default.aspx?user=newbie
>>
>But this code don't get the querystring, the parameters collection gets
>no
>entries from this line "NameValueCollection parameters =
>Request.QueryString;"
>>
>What am I doing wrong here?
>>
>Jeff


>
string username = Request.QueryString["user"];
>


"Jeff" <it_consultant1@.hotmail.com.NOSPAMwrote in message
news:udZYNHc%23GHA.4472@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

hey
>
asp.net 2.0
>
in the page_load event of a web page I place this code:
NameValueCollection parameters = Request.QueryString;
string username = parameters[0].ToString();
>
The URL has a querystring, for example like this: Default.aspx?user=newbie
>
But this code don't get the querystring, the parameters collection gets no
entries from this line "NameValueCollection parameters =
Request.QueryString;"
>
What am I doing wrong here?
>
Jeff
>


I usually use the form
if Request.QueryString.Item("_o") <string.empty then
stroID = Request.QueryString.Item("_o").tostring()
end if

to get the whole collection you can use

Dim coll As NameValueCollection
coll=request.querystring

and then you can access it using coll("_o") for example

Mike
It's my own fault... I thought the URL had a querystring, but because I had
set a breakpoint and then checked the URL during debug, I didn't see the
real URL, I only saw the last URL....

"Jeff" <it_consultant1@.hotmail.com.NOSPAMwrote in message
news:udZYNHc%23GHA.4472@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

hey
>
asp.net 2.0
>
in the page_load event of a web page I place this code:
NameValueCollection parameters = Request.QueryString;
string username = parameters[0].ToString();
>
The URL has a querystring, for example like this: Default.aspx?user=newbie
>
But this code don't get the querystring, the parameters collection gets no
entries from this line "NameValueCollection parameters =
Request.QueryString;"
>
What am I doing wrong here?
>
Jeff
>

newbie: Problem getting the querystring!

hey
asp.net 2.0
in the page_load event of a web page I place this code:
NameValueCollection parameters = Request.QueryString;
string username = parameters[0].ToString();
The URL has a querystring, for example like this: Default.aspx?user=newbie
But this code don't get the querystring, the parameters collection gets no
entries from this line "NameValueCollection parameters =
Request.QueryString;"
What am I doing wrong here?
JeffJeff wrote:
> hey
> asp.net 2.0
> in the page_load event of a web page I place this code:
> NameValueCollection parameters = Request.QueryString;
> string username = parameters[0].ToString();
> The URL has a querystring, for example like this: Default.aspx?user=newbie
> But this code don't get the querystring, the parameters collection gets no
> entries from this line "NameValueCollection parameters =
> Request.QueryString;"
> What am I doing wrong here?
> Jeff
string username = Request.QueryString["user"];
Best bet is to access the querystring collection directly. I'm not sure if
it's something that can do like this, as normally a lot of collections are
copied using the CopyTo method, but this is usually done for arrays not
specialized collections.
Your best bet though is to access it directly from the querystring. Also,
don't use positional identifiers to determine a parameter, it's too easy to
change the order that things will appear in so it's best to use the keyname
instead. Also, don't forget to test for null first or else it'll bomb out
when you run into a case where it's an empty value, that's the most common
gatcha when using the querystring.
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199...2006
"Jeff" <it_consultant1@.hotmail.com.NOSPAM> wrote in message
news:udZYNHc%23GHA.4472@.TK2MSFTNGP05.phx.gbl...
> hey
> asp.net 2.0
> in the page_load event of a web page I place this code:
> NameValueCollection parameters = Request.QueryString;
> string username = parameters[0].ToString();
> The URL has a querystring, for example like this: Default.aspx?user=newbie
> But this code don't get the querystring, the parameters collection gets no
> entries from this line "NameValueCollection parameters =
> Request.QueryString;"
> What am I doing wrong here?
> Jeff
>
Default.aspx?user=newbie
string username = Request.QueryString["user"]; gives username a NULL
value...
Any more suggestions?
"Simon Rigby" <google@.simonrigby.co.uk> wrote in message
news:1161951708.089584.264150@.m7g2000cwm.googlegroups.com...
> Jeff wrote:
> string username = Request.QueryString["user"];
>
"Jeff" <it_consultant1@.hotmail.com.NOSPAM> wrote in message
news:udZYNHc%23GHA.4472@.TK2MSFTNGP05.phx.gbl...
> hey
> asp.net 2.0
> in the page_load event of a web page I place this code:
> NameValueCollection parameters = Request.QueryString;
> string username = parameters[0].ToString();
> The URL has a querystring, for example like this: Default.aspx?user=newbie
> But this code don't get the querystring, the parameters collection gets no
> entries from this line "NameValueCollection parameters =
> Request.QueryString;"
> What am I doing wrong here?
> Jeff
>
I usually use the form
if Request.QueryString.Item("_o") <> string.empty then
stroID = Request.QueryString.Item("_o").tostring()
end if
to get the whole collection you can use
Dim coll As NameValueCollection
coll=request.querystring
and then you can access it using coll("_o") for example
Mike