Skip to content Skip to sidebar Skip to footer

Pass Dropdown Value To Code Behind Static Method

I am trying to use a language selection value in my code behind autocomplete method. I tried using a hidden_field but that does not work. Also ajax did not work for me. This is my

Solution 1:

So I'll post just the "pass the parameter to codebehind" solution. From Mudassar Ahmed Khan's website (my favorite) I recreated the example according to your requirement, you'll have to recraft it for database queries:

AjaxTest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxTest.aspx.cs" Inherits="AjaxTest" %>

<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><!-- Language Picker--><selectclass="btn btn-default dropdown-toggle"id="lang_sel"style="width: 200px; position: relative; left: 19px;"data-toggle="dropdown"aria-haspopup="true"aria-expanded="false"><optionvalue="no_sel">Please select a language</option><optionvalue="German">German</option><optionvalue="French">French</option><optionvalue="Spanish">Spanish</option></select><br /><br /><inputid="btnSetLanguage"type="button"value="Set language"onclick="SetLanguage()" /></div></form><scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"type="text/javascript"></script><scripttype = "text/javascript">functionSetLanguage() {
            $.ajax({
                type: "POST",
                url: "AjaxTest.aspx/Lang_Change",
                data: '{language:"' + $("#lang_sel option:selected").text() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
            });
        }
        functionOnSuccess(response) {
            alert(response.d);
        }
    </script></body></html>

AjaxTest.cs (code behind)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

publicpartialclassAjaxTest : System.Web.UI.Page
{
    protectedvoidPage_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod]
    publicstaticstringLang_Change(string language)
    {
        return"Language selected: " + language;
    }
}

Result:

enter image description here

Post a Comment for "Pass Dropdown Value To Code Behind Static Method"