如何在静态方法中访问下拉列表

时间:2014-06-30 07:16:59

标签: c# asp.net

我有以下静态方法。

public static List<string> GetAutoCompleteData(string StudentId)
    {
            List<string> result = new List<string>();
            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.Help me

1 个答案:

答案 0 :(得分:1)

Dropdownlist对象是您的类(Web页面)的非静态成员,静态方法不能访问非静态成员。调用时将dropdownList值传递给静态方法。

<强> Static Members

  

静态方法和属性无法访问非静态字段和   包含类型的事件,他们无法访问实例   任何对象的变量,除非它在方法中显式传递   参数。

静态方法定义

public static List GetAutoCompleteData(string StudentId, string dropdownvalue ) {
  //Your code
}

静态方法调用

StaticMethodClass.GetAutoCompleteData("studendId", dropdown.SelectedValue);