• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3000
  • Last Modified:

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
0
elmoredaniel
Asked:
elmoredaniel
  • 2
  • 2
  • 2
  • +1
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now