Pages

Thursday 27 February 2014

How to Get Query String Parameter Values with Spaces in C#

How to Get Query String Parameter Values with Spaces in C#

we have to use decodeURIComponent JavaScript function in our code.

 <script type="text/javascript">
     $(function () {
         var str = decodeURIComponent('Welcome%20To%20http%3A%2f%2ffantasyaspnet.blogspot.in%2f')
         alert(str);
     })
</script>

Example:

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>How to Get Query String Parameter Values with Spaces in C#</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(function () {
        var name = GetParameterValues('username');
        var id = GetParameterValues('name');
        $('#spn_UserName').html('<strong>' + name + '</strong>');
        $('#spn_UserId').html('<strong>' + id + '</strong>');
    });
    function GetParameterValues(param) {
        var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < url.length; i++) {
            var urlparam = url[i].split('=');
            if (urlparam[0] == param) {
                return decodeURIComponent(urlparam[1]);
            }
        }
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>UserName: <span id="spn_UserName"></span></p>
<p>Name: <span id="spn_UserId"></span></p>
</div>
</form>
</body>
</html>

Demo: 


No comments:

Post a Comment