将下拉值传递给静态方法后面的代码

时间:2018-06-13 10:16:56

标签: javascript c# asp.net

我正在尝试在自动填充方法后面的代码中使用语言选择值。我尝试使用hidden_​​field,但这不起作用。 ajax也不适合我。

这是我的html下拉菜单:

<!-- Language Picker-->
    <div class="row" hidden="hidden">
            <select class="btn btn-default dropdown-toggle" id="lang_sel" style="width: 200px; position: relative; left: 19px;"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <option value="no_sel">Please select a language</option>             
                <option value="German">German</option>
                <option value="French">French</option>
                <option value="Spanish">Spanish</option>
            </select>
    </div>

这是代码背后的方法:

[System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetCompletionList(string prefixText, int count)
        {
            string sys_lang = here the language should be transmitted;
            string table = null;
            //Database/Table Selection
            if (sys_lang == "German")
            {
                //MessageBox.Show("Deutsch");
                table = "translation.dbo.de_translation";
            }
            if (sys_lang == "Spanish")
            {
                //MessageBox.Show("Spanisch");
                table = "translation.dbo.sp_translation";
            }
            if (sys_lang == "French")
            {
                //MessageBox.Show("Französich");
                table = "translation.dbo.fr_translation";
            }
            if (sys_lang == "no_sel")
            {
                //MessageBox.Show("Keine Sprache gefunden");
                table = null;
            }

            MessageBox.Show("Sprache" + "=" + sys_lang);


            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = connection;

                using (SqlCommand com = new SqlCommand())
                {
                    com.CommandText = "SELECT Source FROM" + " " + table + " " + "WHERE Source like @Search + '%'";

                    com.Parameters.AddWithValue("@Search", prefixText);
                    com.Connection = con;
                    con.Open();
                    List<string> suggestions = new List<string>();
                    using (SqlDataReader sdr = com.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            suggestions.Add(sdr["Source"].ToString());
                        }
                    }
                    con.Close();
                    return suggestions;
                }
            }
        }

我只想做这样的事情:

string sys_lang = hidden_field.Value;

我希望有人有个主意。

更新

这是我的隐藏字段:

<asp:HiddenField ID="hidden_language" runat="server" ClientIDMode="Static"> </asp:HiddenField >

我初始化了这个变量:

public string language = null;

我尝试使用以下方法更改语言:

public void Lang_Change(string lang)
        {
            language = lang;
        }

我用这个ajax函数尝试了它:

$.ajax({  
                    type: "POST",  
                    url: "/default.aspx/Lang_Change",  
                    contentType: "application/json; charset=utf-8",  
                    data: '{"lang":"' + $("#hidden_language").val() + '"}',  
                    dataType: "html",  
                    success: function ()
                    {  
                        alert("SUCCESS");  
                    },  
                    error: function ()
                    {  
                        alert("FAIL");
                    }  
                });  

但每次都失败了。页面名称为default.aspx。我不知道路径是错还是什么。

1 个答案:

答案 0 :(得分:1)

所以我只发布“将参数传递给codebehind”解决方案。从Mudassar Ahmed Khan's website(我最喜欢的)我根据您的要求重新创建了示例,您将不得不重新编写它以进行数据库查询:

<强> AjaxTest.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <!-- Language Picker-->
            <select class="btn btn-default dropdown-toggle" id="lang_sel" style="width: 200px; position: relative; left: 19px;" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <option value="no_sel">Please select a language</option>
                <option value="German">German</option>
                <option value="French">French</option>
                <option value="Spanish">Spanish</option>
            </select>
            <br /> <br />
            <input id="btnSetLanguage" type="button" value="Set language" onclick="SetLanguage()" />
        </div>
    </form>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>

    <script type = "text/javascript">
        function SetLanguage() {
            $.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);
            }
            });
        }
        function OnSuccess(response) {
            alert(response.d);
        }
    </script>
</body>
</html>

AjaxTest.cs(代码隐藏)

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

public partial class AjaxTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

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

<强>结果:

enter image description here