将下拉值传递给静态方法后面的代码 [英] Pass dropdown value to code behind static method
问题描述
我正在尝试在自动完成方法后面的代码中使用语言选择值.我尝试使用hidden_field,但这不起作用.同样,ajax也不适合我.
这是我的html下拉菜单:
<!-语言选择器->< 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">请选择一种语言</option>< option value =德语">德语</option>< option value =法语">法语</option>< option value ="Spanish"> Spanish</option></select></div>
这是我后面代码中的方法:
[System.Web.Script.Services.ScriptMethod()][System.Web.Services.WebMethod]公共静态List< string>GetCompletionList(字符串prefixText,int计数){字符串sys_lang =这里应该传送语言;字符串表= null;//数据库/表选择如果(sys_lang ==德语"){//MessageBox.Show("Deutsch);table ="translation.dbo.de_translation";}如果(sys_lang ==西班牙文"){//MessageBox.Show("Spanisch);table ="translation.dbo.sp_translation";}如果(sys_lang ==法语"){//MessageBox.Show("Französich);table ="translation.dbo.fr_translation";}如果(sys_lang =="no_sel"){//MessageBox.Show("Keine Sprache gefunden);table = null;}MessageBox.Show("Sprache" +"=" + sys_lang);使用(SqlConnection con = new SqlConnection()){con.ConnectionString =连接;使用(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>建议=新的List< string>();使用(SqlDataReader sdr = com.ExecuteReader()){而(sdr.Read()){proposal.Add(sdr ["Source"].ToString());}}con.Close();返回建议;}}}
我只想做这样的事情:
string sys_lang = hidden_field.Value;
我希望有人有一个主意.
更新
这是我的隐藏字段:
< asp:HiddenField ID ="hidden_language" runat ="server" ClientIDMode ="Static"></asp:HiddenField>
我初始化了这个变量:
公共字符串语言= null;
我尝试通过以下方法更改语言:
public void Lang_Change(字符串lang){语言= lang;}
然后我使用此ajax函数进行了尝试:
$.ajax({输入:"POST",网址:"/default.aspx/Lang_Change",contentType:"application/json; charset = utf-8",数据:'{"lang":'+ $("#hidden_language).val()+'"}',dataType:"html",成功:功能(个){alert("SUCCESS");},错误:函数(){alert("FAIL");}});
但是每次都会失败.页面的名称是default.aspx.我不知道路径是错误的还是什么.
因此,我将仅发布将参数传递给代码隐藏"解决方案.来自
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 html dropdown menu:
<!-- 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>
And this is my method in code behind:
[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;
}
}
}
I just want to do something like this:
string sys_lang = hidden_field.Value;
I hope someone has an idea.
Update
This is my hidden field:
<asp:HiddenField ID="hidden_language" runat="server" ClientIDMode="Static"> </asp:HiddenField >
I initialized this variable:
public string language = null;
I tried to change the language with the following method:
public void Lang_Change(string lang)
{
language = lang;
}
And I tried it with this ajax function:
$.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");
}
});
But it fails every time. The name of the page is default.aspx. I don't know if the path is wrong or something.
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>
<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 (code behind)
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;
}
}
Result:
这篇关于将下拉值传递给静态方法后面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!