获取列表Web方法并在列表框中显示数据

时间:2014-03-27 11:54:41

标签: c# asp.net sql web-services webmethod

我正在尝试在asp.net的列表框中显示日期。我在下面有以下网络方法,并在输入学生ID时正常工作。然后我如何在我的页面中调用此Web方法以在列表框中显示日期。

 [WebMethod]
                public List<Attendance> StudentAttendance(int SID)
                {

                    List<Attendance> listofAttendance = new List<Attendance>();
                    cn.Open();
                    SqlCommand com = new SqlCommand("SELECT * FROM tblAttendance WHERE StudentID = " + SID +"", cn);
                    SqlDataReader sr = com.ExecuteReader();
                    while (sr.Read())
                    {
                        Attendance getdattendance = new Attendance();
                        getdattendance.ID = sr.GetInt32(0);
                        getdattendance.StudentID = sr.GetInt32(1);
                        getdattendance.RegistrationDate = sr.GetDateTime(2);

                        listofAttendance.Add(getdattendance);
                    }
                    sr.Close();
                    cn.Close();
                    return listofAttendance;

                }

在列表框中显示日期

 protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserAuthentication"] != null)
            {
               Student s = (Student)Session["UserAuthentication"];

                Attendance[] a = attend.StudentAttendance(s.StudentID);
                ListBox1.Text = display list of dates with the following format  ("dd MMM yyyy"
            //  lbAttendance.Text = a.RegistrationDate.Date.ToString("dd MMM yyyy", System.Globalization.CultureInfo.InstalledUICulture);
            }
            else
            {
                Response.Redirect("Index.aspx");
            }



        }

1 个答案:

答案 0 :(得分:1)

您需要在此Web方法中修改注册日期格式。

 getdattendance.RegistrationDate =Convert.ToDateTime(sr.GetDateTime(2).ToString("dd MM yyyy"));

然后在页面加载中,您需要将列表框绑定到出勤列表中。

List<Attendance> attendances = attend.StudentAttendance(s.StudentID); ;
listbox1.DataSource = attendances;
listbox1.DataValueField = "Id";
listbox1.DataTextField = "RegistrationDate";
listbox1.DataBind();