Gán (binding) dữ liệu vào DropDownList (ASP.NET)

1 993 2
Gán (binding) dữ liệu vào DropDownList (ASP.NET)

Đang tải... (xem toàn văn)

Thông tin tài liệu

Gán (binding) dữ liệu vào DropDownList (ASP.NET) Để binding một SqlDataReader chúng ta có thể sử dụng một hàm để điền dữ liệu vào DropDownList. Cụ thể bạn có thể tạo hàm như sau: public static bool FillDropDownList(DropDownList dDl,string Select) { SqlConnection m_SqlConnection = new SqlConnection(CRbvDB.RbvWebDSN); SqlCommand m_SqlCommand = new SqlCommand(Select,m_SqlConnection); try { dDl.DataValueField = ''Id''; //trường này bạn có thể chỉnh sửa cho phù hợp dDl.DataTextField = ''Name''; //trường này bạn có thể chỉnh sửa cho phù hợp m_SqlConnection.Open(); SqlDataReader m_SqlDataReader = m_SqlCommand.ExecuteReader(); dDl.DataSource = m_SqlDataReader; dDl.DataBind(); m_SqlDataReader.Close(); // Close DataReader } // try catch // (Exception e) // Exception Removed { return false; //throw new Exception(''Có lỗi khi điền DropDownLit -> '' + e.ToString()); } // catch finally { m_SqlCommand.Dispose(); m_SqlConnection.Close(); // Đóng kết nối m_SqlConnection.Dispose(); } return true; } Trong ví dụ DropDownList sẽ điền với các trường trong database.Bạn có thể truyền biến để sử dụng hàm như sau: string selectCmd = ''SELECT Id,LastName +', '+ FirstName AS Name ''; //(các trường dữ liệu trên đây là giả lập) selectCmd += ''FROM aRepresentative WHERE Id=SLS ORDER BY LastName ''; dDl.FillDropDownList(sslSlsId,selectCmd); . Gán (binding) dữ liệu vào DropDownList (ASP. NET) Để binding một SqlDataReader chúng ta có thể sử dụng một hàm để điền dữ liệu vào DropDownList. . //(các trường dữ liệu trên đây là giả lập) selectCmd += ''FROM aRepresentative WHERE Id=SLS ORDER BY LastName ''; dDl.FillDropDownList(sslSlsId,selectCmd);

Ngày đăng: 26/10/2013, 01:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan