<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ControlAdd.aspx.cs" Inherits="ControlAdd" %> <html> <head id="Head1" runat="server"> <title>ThaiCreate.Com ASP.NET - Control.Add</title> </head> <body> <form id="form1" runat="server"> <asp:Panel id="pnlMain1" runat="server"></asp:Panel><br /> <asp:Panel id="pnlMain2" runat="server"></asp:Panel><br /> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; public partial class ControlAdd : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { //*** Create New Label ***' Label lblText = new Label(); { lblText.Text = "My Customer"; lblText.Font.Size = 30; } this.pnlMain1.Controls.Add(lblText); //*** Create DataGrid ***' DataGrid myDataGrid = new DataGrid(); { myDataGrid.AutoGenerateColumns = true; myDataGrid.DataSource = DataSource(); myDataGrid.DataBind(); } this.pnlMain2.Controls.Add(myDataGrid); } protected object DataSource() { OleDbConnection objConn = new OleDbConnection(); OleDbDataAdapter dtAdapter = default(OleDbDataAdapter); DataTable dt = new DataTable(); string strConnString = null; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";"; objConn = new OleDbConnection(strConnString); objConn.Open(); string strSQL = null; strSQL = "SELECT * FROM customer"; dtAdapter = new OleDbDataAdapter(strSQL, objConn); dtAdapter.Fill(dt); dtAdapter = null; objConn.Close(); objConn = null; //*** Return DataTable ***' return dt; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท