Check if URL variable exists

I've never written a line of C#, so don't laugh at me here. All I need to do is check to see if a URL variable exists, if so I need to output a few lines of javascript. How shall I do so?

Thank you
elmoredanielAsked:
Who is Participating?
 
GavinMannionCommented:
Okay you can put it on the page inside scripting brackets.

<%
string userType = Request.QueryString["usertype"]
if( userType == "admin")
{
      this.RegisterStartupscript("alert","<script language='javascript'>alert('the variable is admin');</script>");
}
%>

If the usertype variable does not exist then it doesn't matter. It will then find nothing and do nothing.
The normal way for URL params to work is URL?ParamName=ParamValue

AS long as this structure is there then my code whould work. If you don't have this type of structure then it won't. if the URL looks like

mypage.aspx?usertype=

Then my code will still work
0
 
orbulatCommented:
1. drag a hidden variable in design mode, eg. <input type=hidden id=hidURL runat=server />
2. in your code behind add this function for checking url, this function may run a little bit slow

public bool checkURL(string url)
{
   try
   {
      HttpWebRequest hReq= (HttpWebRequest)WebRequest.Create(url);
      HttpWebResponse hResp = (HttpWebResponse)hReq.GetResponse();
      hResp .Close();
      return true;
    }
    catch
    {
       return false;
    }
}

3. in Page_Load

add the following:

if (!checkURL("http://www.your_url.com"))
{
     hidURL.value = "false";
}

4. in your javascript:

if (document.Form1.hidURL.value == "false")
   alert("url is invalid!");
0
 
elmoredanielAuthor Commented:
Humm, don't know if that's what I needed, maybe I'm wrong.

I have a javascript tag and if the page is accessed with "admin" in the query string ie. (page.aspx?admin)  Then I need to output:  a javascript alert.

So can't C# do something like:

<script language="javascript">

if(url_variable_admin_exists ...)
   alert('the variables exists');

</script>
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
irps20001Commented:
Hi,
You can get the url with the help of Request.
For instance, say

String URL = Request.Path.ToLower();

now you have the complete url with you.
URL has got different segments. You can get all the parts using Segments and use it as per your requirement

do let me know if you have further doubts so that i can help you on this
HTH

Happy Programming
Rana

0
 
GavinMannionCommented:
So you want to check what is in the URL?

Does it really look like page.aspx?admin or can you make it look something like page.aspx?usertype=admin

If you can make it look this my second option then in your codebehind do this

string userType = Request.QueryString["usertype"]
if( userType == "admin")
{
      this.RegisterStartupscript("alert","<script language='javascript'>alert('the variable is admin');</script>");
}

0
 
irps20001Commented:
Use "Request.Url.Segments" to fetch different parts of the URL
Rana
0
 
elmoredanielAuthor Commented:
Apparently no one read my question, because it says "I've never written a line of C#"   and I will never have to again after this.

GavinMannion your code looks the most promising. But I don't know what a codebehind is. Can't I place this code directly in the page. Also, it looks like your code requires the url variable "usertype" to exists, which it may not exist.

Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.