Redirect to Error page if QueryString in empty using jQuery in ASP.Net

Last Reply 11 months ago By dharmendr

Posted 11 months ago

how can we disable a search page if not data has been passed through query string to search page and redirect to error page (pagenotfound) using jquery asp.net

if data is passed..

then display it in search page...

check the difference..

we get result for first link

Search.aspx?q=asp

for the next pages ..

redirected to pagenotfound.aspx

Search.aspx?q=
Search.aspx?q
Search.aspx?
Search.aspx

 

Posted 11 months ago Modified on 11 months ago

Hi jovceka,

Check this example. Now please take its reference and correct your code.

HTML

Default

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#btnsearch").click(function () {
            var searchtxt = $('#txtsearch').val();
            //var url = "Search.aspx?s=" + encodeURIComponent(searchtxt);
            window.location.href = searchtxt;
        });
    });
</script>
<input type="text" id="txtsearch" style="width: 150px" value="Search.aspx" />
<input type="button" value="Search" id="btnsearch" />

Search

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    window.onload = function () {
        if (window.location.search != '' && window.location.search.split('?').length > 1) {
            var param = window.location.search.split('?')[1];
            if (param != '' && param.split('=')[1] != undefined && param.split('=')[1] != '') {
                var value = decodeURIComponent(param.split('=')[1]);
                // Your Ajax Call.
                alert("Serach value is " + value);
            } else {
                window.location.href = "Error.aspx";
            }
        } else {
            window.location.href = "Error.aspx";
        }
    }
</script>

Error

<h3>Error</h3>

Screenshot