2017-03-01 35 views
0

假設我有一個textbox1,它具有一些值,例如日期。現在我有另一個textbox2,它有autocompleteextender,它的值是通過web方法定義的。現在我想要在webmethod中定義的變量應該從textbox1中獲取值。將文本框值解析爲asp.net中web方法中定義的變量

[System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()] 
public static List<string> SearchVehicles(string prefixText, int count) 
{ 
    MySqlConnection conn = new MySqlConnection(); 
    conn.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString; 
    MySqlCommand cmd = new MySqlCommand(); 
    cmd.CommandText = "SELECT flightNo FROM flight_train where (freq like @freq) and status = 'active'"; 
    cmd.Parameters.AddWithValue("@freq", "%" + prefixText + "%"); 
    cmd.Connection = conn; 
    conn.Open(); 
    List<string> strVehicle = new List<string>(); 
    MySqlDataReader sdr = cmd.ExecuteReader; 
    while (sdr.Read) { 
     strVehicle.Add(sdr("flightNo").ToString); 
    } 
    conn.Close(); 
    return strVehicle; 
} 

在上面的WebMethod變量(prefixText)的定義,我想從TextBox1的取值

+0

您必須在javascript/JQuery的SearchVehicles調用中傳遞textbox1值。 – Adil

+0

@Adil你可以請分享一個參考鏈接或張貼在答案如何做到這一點? – SUN

+0

@Adil是否有可能在session或viewstate中存儲textbox1值,然後在webmethod中檢索該值? – SUN

回答

0

你將不得不使用AJAX方法在JavaScript中執行that.In此的WebMethod您無法訪問的形式領域。

+0

是否有可能在session或viewstate中存儲textbox1值,然後在webmethod中檢索該值? – SUN

+0

是的,你可以使用會話。 'HttpContext.Current.Session [「SessionName」]。ToString()' – Mohammad