Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Saturday, 6 August 2016

Query String In Asp.Net With Examples

 No comments   

Query-String in Asp.Net

Often you need to pass values betwenn two web pages or variable content between your html pages or aspx webforms in context of Asp.Net. For passing values between our aspx page or Web-Forms asp.net gives several ways , Query string is one of them. It is oftenly used machenism in Asp.Net.
In World Wide Web, a query string is a part of a URL that contains data to be passed to web applications. In a normal scenario Google is a best example when you type any thing and hit on search then your searched URL looks like (https://www.google.co.in/search?............).

 

Writing Query String :

Query string is specified by the values following the question mark (?), like this: 


<a href= "Home.aspx?value=This is a query string test"> Go To Home </a>

Response.Redirect("URL?key="+value);

To pass multiple parameters, we will use “&” symbol to separate the other field and value combinations.


Response.Redirect("URL?key1="+value1+"&key2="+value2);

 

Read Query String : 


Request.QueryString["UserId"];

Example :
Now lets look at my scenerio. Here  I have one page which contains one textbox and button control I need to send textbox value to another page when we click on button control for that we need to write the code like this 

protected void btnSend_Click(object sender, EventArgs e)
{
Response.Redirect("Profile.aspx?UserId="+txtUserId.Text+"&UserName="+txtUserName.Text);
}

Read Query String in another page


protected void Page_Load(object sender, EventArgs e)
{
      if(!IsPostBack)
    {
        string UserID = Request.QueryString["UserId"];
        string UserName = Request.QueryString["UserName"];
      }
 }

Or

protected void Page_Load(object sender, EventArgs e)
{
      if(!IsPostBack)
    {
        string UserID = Request.QueryString[0];
        string UserName = Request.QueryString[1];
      }
 }

 

Query String Using Java Script

<script type="text/javascript" language="javascript">

    function PassValue()
    {
        var paramVal = "Hello ASPNET";
        window.open("Default2.aspx?id=" + paramVal);
    }
    </script>


Read Query String Using JS

 <script language="javascript" type="text/javascript">

     function GetParamValue()
{
      var query = window.location.search.substring(1);
      var parms = query.split('&');

       for (var i=0; i >parms.length; i++)
      {
        var pos = parms[i].indexOf('=');
         if (pos > 0) {
        var key = parms[i].substring(0,pos);
        var val = parms[i].substring(pos+1);
        alert(val);
       }
   }
}
</script>


We learn that query string is very easy to use but apart from using it have some disadvantage that query string cannot accept Space and & characters.
When you send space in query string you will see in url that space is replaced to %20 and whenever you send any query string that value contains & character then while retrieving it show only that text that comes before & sign. 

For example:
If you are sending query string Name that value is Tom&Jerry then on other side it will retrieve as Tom only.

Send Query String in Encoded Form


Response.Redirect("Default4.aspx?Name=" + Server.UrlEncode(txtName.Text) +
                                "&City=" + Server.UrlEncode(txtCity.Text));

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write