如何在静态方法中访问下拉列表
我有以下静态方法。
public static List GetAutoCompleteData(string StudentId) { List result = new List(); using (SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=true;Initial Catalog=SMS")) { //using (SqlCommand cmd = new SqlCommand("select StudentId,StudentName from tblStudent where StudentId LIKE '%'+@SearchText+'%'", con)) using (SqlCommand cmd = new SqlCommand("select T1.StudentName,T1.StudentId from tblStudent T1 where StudentId LIKE '%'+@SearchText+'%' except select T2.StudentName, T2.StudentId from tblStudentAcademics T2 where T2.AcademicYear='" +Dropdownvalue + "'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", StudentId); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["StudentId"].ToString() + "-" + dr["StudentName"].ToString()); } return result; } } }
我需要用ddlAcadeic.selectedvalue.tostring()来代替Dropdownvalue。帮我吧
Dropdownlist对象是您的类(Web页面)的非静态成员, 静态方法不能访问非静态成员 。 调用时,将dropdownList值传递给静态方法。
静态成员
静态方法和属性无法访问其包含类型中的非静态字段和事件,并且除非在方法参数中显式传递,否则它们无法访问任何对象的实例变量。
静态方法定义
public static List GetAutoCompleteData(string StudentId, string dropdownvalue ) { //Your code }
静态方法调用
StaticMethodClass.GetAutoCompleteData("studendId", dropdown.SelectedValue);