简体   繁体   中英

Set ASP.NET Session variable from javascript?

I need to set ac# session variable from javascript: I tried the following:

var mySearchPar = "search1";
<%Session["SearchPar"] = "' + mySearchPar + '"; %>;

but by c# instead of seeing the content of "search1" in the session I see " + mySearchPar + "

Can anyone tell me what I'm doing wrong?

I followed your suggestions.
the result is this:

Sarch Html form:

<div class="row">
    <asp:HiddenField ID="hfSearch" runat="server" ClientIDMode="static" />
    <asp:HiddenField ID="hfSearchtxt" runat="server" ClientIDMode="static" />
    <div class="col-xs-8 col-xs-offset-2" style="margin-left: 0; padding-top: 1px; padding-bottom: 2px;">
        <div class="form-group" style="width:100%;">
            <div class="input-group" style="width:100%;">
                <div class="input-group-btn search-panel">
                    <button  type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                        <span id="search_concept">Filter by</span> <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#contains">Contains</a></li>
                        <li><a href="#its_equal">It's equal</a></li>
                        <li><a href="#greather_than">Greather than ></a></li>
                        <li><a href="#less_than">Less than < </a></li>
                        <li class="divider"></li>
                        <li><a href="#all">Anything</a></li>
                    </ul>
                </div>
                <input type="hidden" name="search_param" value="all" id="search_param">
                <input type="text" class="form-control" name="x" onkeyup="Search(this);" placeholder="Search term...">
                <span class="input-group-btn" style="float: left;">
                    <asp:LinkButton ID="lnkSearch" runat="server" OnClick="Search_Click" class="btn btn-default" type="button"><span class="glyphicon glyphicon-search"></span></asp:LinkButton>
                </span>
            </div>
        </div>
    </div>
</div>

javaScript:

<script type="text/javascript">
    function Search(obj) {
        document.getElementById("hfSearchtxt").value = obj.value;
        var btn = document.getElementById('<%=lnkSearch.ClientID%>');
        btn.click();
    }

    $(document).ready(function (e) {
        $('.search-panel .dropdown-menu').find('a').click(function (e) {
            e.preventDefault();
            var param = $(this).attr("href").replace("#", "");
            var concept = $(this).text();
            
            $('.search-panel span#search_concept').text(concept);
            $('.input-group #search_param').val(param);
            document.getElementById("hfSearch").value = concept;
        });
    });
    
</script>

c#

protected void Search_Click(object sender, EventArgs e)
{
    Session["SearchPar"] = this.hfSearch.Value;
    //search code body....
}

You should store the value in a hidden input field and store it into the session on the next postback in the server sided code.

Here is a little example:

HTML

<asp:HiddenField ID="hfSearch" runat="server" ClientIDMode="static" />

JS

var mySearchPar = "search1";
document.getElementById("hfSearch").value = mySearchPar;

C#

// later in your click event:
Session["SearchPar"] = this.hfSearch.Value;

To be honest it looks like you want to store the last search term in the session. In this case, you don't need a hidden field, simply access the asp:TextBox on the click event and store the value in the session.

Maybe it's even better to not use the Session for such storage at all. Session will be stored on the server. Use cookies to persist the last search, instead. Cookies will be stored at the client and won't cost any resources of the server.

In c# you can create a session variable using:

session["var"]="hello"; 

To display this value you can use:

Response.write(session["var"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM