<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="false" CodeFile="testmaster.aspx.vb" Inherits="testmaster" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script runat="server">
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Dim dtReader As SqlDataReader
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Data Source=DELL_BLUE-PC;Initial Catalog=UTCCResearch;Integrated Security=True"
objConn = New SqlConnection(strConnString)
objConn.Open()
IF Not Page.IsPostBack() Then
BindData()
End IF
End Sub
Sub BindData()
strSQL = "SELECT * FROM Member"
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
myRepeater.DataSource = dtReader
myRepeater.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub myRepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs) Handles myRepeater.ItemCommand
IF e.CommandName = "Delete" Then
'*** Userid ***'
Dim lblUserId As Label = CType(e.Item.FindControl("lblUserid"), Label)
strSQL = "DELETE FROM Member WHERE userid = '" & lblUserid.Text & "' "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End IF
End Sub
Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
'*** UserID ***'
Dim lblUserID As Label = CType(e.Item.FindControl("lblUserID"), Label)
If Not IsNothing(lblUserID) Then
lblUserID.Text = e.Item.DataItem("userID")
End If
'*** Username ***'
Dim lblUsername As Label = CType(e.Item.FindControl("lblUsername"), Label)
If Not IsNothing(lblUsername) Then
lblUsername.Text = e.Item.DataItem("username")
End If
'*** Password ***'
Dim lblPassword As Label = CType(e.Item.FindControl("lblPassword"), Label)
If Not IsNothing(lblPassword) Then
lblPassword.Text = e.Item.DataItem("UserPassword")
End If
'*** Name ***'
Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)
If Not IsNothing(lblName) Then
lblName.Text = e.Item.DataItem("Name")
End If
'*** LastName ***'
Dim lbllname As Label = CType(e.Item.FindControl("lbllname"), Label)
If Not IsNothing(lbllname) Then
lbllname.Text = e.Item.DataItem("Lastname")
End If
'*** Email ***'
Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)
If Not IsNothing(lblEmail) Then
lblEmail.Text = e.Item.DataItem("Email")
End If
'*** Address ***'
Dim lblAddress As Label = CType(e.Item.FindControl("lblAddress"), Label)
If Not IsNothing(lblAddress) Then
lblAddress.Text = e.Item.DataItem("Address")
End If
'*** Delete ***'
Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');")
End If
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>UserId</th>
<th>Username</th>
<th>Password</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
<th>Address</th>
<th>Delete</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:Label id="lblUserID" runat="server"></asp:Label></td>
<td align="center"><asp:Label id="lblUsername" runat="server"></asp:Label></td>
<td><asp:Label id="lblPassword" runat="server"></asp:Label></td>
<td><asp:Label id="lblName" runat="server"></asp:Label></td>
<td align="center"><asp:Label id="lbllname" runat="server"></asp:Label></td>
<td align="right"><asp:Label id="lblEmail" runat="server"></asp:Label></td>
<td align="right"><asp:Label id="lbladdress" runat="server"></asp:Label></td>
<td align="right"><asp:LinkButton id="lnkDelete" CommandName="Delete" runat="server">Delete</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
ไฟล์ที่ไม่โชว์ข้อมูลบนมาสเตอร์เพจ
Code (ASP)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Dim dtReader As SqlDataReader
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Data Source=DELL_BLUE-PC;Initial Catalog=UTCCResearch;Integrated Security=True"
objConn = New SqlConnection(strConnString)
objConn.Open()
IF Not Page.IsPostBack() Then
BindData()
End IF
End Sub
Sub BindData()
strSQL = "SELECT * FROM Member"
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
myRepeater.DataSource = dtReader
myRepeater.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub myRepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs) Handles myRepeater.ItemCommand
IF e.CommandName = "Delete" Then
'*** Userid ***'
Dim lblUserId As Label = CType(e.Item.FindControl("lblUserid"), Label)
strSQL = "DELETE FROM Member WHERE userid = '" & lblUserid.Text & "' "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End IF
End Sub
Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
'*** UserID ***'
Dim lblUserID As Label = CType(e.Item.FindControl("lblUserID"), Label)
If Not IsNothing(lblUserID) Then
lblUserID.Text = e.Item.DataItem("userID")
End If
'*** Username ***'
Dim lblUsername As Label = CType(e.Item.FindControl("lblUsername"), Label)
If Not IsNothing(lblUsername) Then
lblUsername.Text = e.Item.DataItem("username")
End If
'*** Password ***'
Dim lblPassword As Label = CType(e.Item.FindControl("lblPassword"), Label)
If Not IsNothing(lblPassword) Then
lblPassword.Text = e.Item.DataItem("UserPassword")
End If
'*** Name ***'
Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)
If Not IsNothing(lblName) Then
lblName.Text = e.Item.DataItem("Name")
End If
'*** LastName ***'
Dim lbllname As Label = CType(e.Item.FindControl("lbllname"), Label)
If Not IsNothing(lbllname) Then
lbllname.Text = e.Item.DataItem("Lastname")
End If
'*** Email ***'
Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)
If Not IsNothing(lblEmail) Then
lblEmail.Text = e.Item.DataItem("Email")
End If
'*** Address ***'
Dim lblAddress As Label = CType(e.Item.FindControl("lblAddress"), Label)
If Not IsNothing(lblAddress) Then
lblAddress.Text = e.Item.DataItem("Address")
End If
'*** Delete ***'
Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');")
End If
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - SQL Server</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Select1_onclick() {
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>UserId</th>
<th>Username</th>
<th>Password</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
<th>Address</th>
<th>Delete</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:Label id="lblUserID" runat="server"></asp:Label></td>
<td align="center"><asp:Label id="lblUsername" runat="server"></asp:Label></td>
<td><asp:Label id="lblPassword" runat="server"></asp:Label></td>
<td><asp:Label id="lblName" runat="server"></asp:Label></td>
<td align="center"><asp:Label id="lbllname" runat="server"></asp:Label></td>
<td align="right"><asp:Label id="lblEmail" runat="server"></asp:Label></td>
<td align="right"><asp:Label id="lbladdress" runat="server"></asp:Label></td>
<td align="right"><asp:LinkButton id="lnkDelete" CommandName="Delete" runat="server">Delete</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
Imports System.Data.SqlClient
Imports System.Data
Partial Class testmaster
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Dim dtReader As SqlDataReader
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnString As String
strConnString = "Data Source=DELL_BLUE-PC;Initial Catalog=UTCCResearch;Integrated Security=True"
objConn = New SqlConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
End Sub
Sub BindData()
strSQL = "SELECT * FROM Member"
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
myRepeater.DataSource = dtReader
myRepeater.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub myRepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs) Handles myRepeater.ItemCommand
If e.CommandName = "Delete" Then
'*** Userid ***'
Dim lblUserId As Label = CType(e.Item.FindControl("lblUserid"), Label)
strSQL = "DELETE FROM Member WHERE userid = '" & lblUserid.Text & "' "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
'*** UserID ***'
Dim lblUserID As Label = CType(e.Item.FindControl("lblUserID"), Label)
If Not IsNothing(lblUserID) Then
lblUserID.Text = e.Item.DataItem("userID")
End If
'*** Username ***'
Dim lblUsername As Label = CType(e.Item.FindControl("lblUsername"), Label)
If Not IsNothing(lblUsername) Then
lblUsername.Text = e.Item.DataItem("username")
End If
'*** Password ***'
Dim lblPassword As Label = CType(e.Item.FindControl("lblPassword"), Label)
If Not IsNothing(lblPassword) Then
lblPassword.Text = e.Item.DataItem("UserPassword")
End If
'*** Name ***'
Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)
If Not IsNothing(lblName) Then
lblName.Text = e.Item.DataItem("Name")
End If
'*** LastName ***'
Dim lbllname As Label = CType(e.Item.FindControl("lbllname"), Label)
If Not IsNothing(lbllname) Then
lbllname.Text = e.Item.DataItem("Lastname")
End If
'*** Email ***'
Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)
If Not IsNothing(lblEmail) Then
lblEmail.Text = e.Item.DataItem("Email")
End If
'*** Address ***'
Dim lblAddress As Label = CType(e.Item.FindControl("lblAddress"), Label)
If Not IsNothing(lblAddress) Then
lblAddress.Text = e.Item.DataItem("Address")
End If
'*** Delete ***'
Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');")
End If
End Sub
End Class
Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
'*** UserID ***'
Dim lblUserID As Label = CType(e.Item.FindControl("lblUserID"), Label)
'If Not IsNothing(lblUserID) Then
lblUserID.Text = e.Item.DataItem("userID")
'End If
'*** Username ***'
Dim lblUsername As Label = CType(e.Item.FindControl("lblUsername"), Label)
'If Not IsNothing(lblUsername) Then
lblUsername.Text = e.Item.DataItem("username")
'End If
'*** Password ***'
Dim lblPassword As Label = CType(e.Item.FindControl("lblPassword"), Label)
'If Not IsNothing(lblPassword) Then
lblPassword.Text = e.Item.DataItem("UserPassword")
'End If
'*** Name ***'
Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)
'If Not IsNothing(lblName) Then
lblName.Text = e.Item.DataItem("Name")
'End If
'*** LastName ***'
Dim lbllname As Label = CType(e.Item.FindControl("lbllname"), Label)
'If Not IsNothing(lbllname) Then
lbllname.Text = e.Item.DataItem("Lastname")
'End If
'*** Email ***'
Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)
'If Not IsNothing(lblEmail) Then
lblEmail.Text = e.Item.DataItem("Email")
'End If
'*** Address ***'
Dim lblAddress As Label = CType(e.Item.FindControl("lblAddress"), Label)
'If Not IsNothing(lblAddress) Then
lblAddress.Text = e.Item.DataItem("Address")
'End If
'*** Delete ***'
Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
'If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');")
'End If
End Sub
End Class
เพราะมันเป็น control ที่ใช้สร้างตารางข้อมูลโดยตรงอยู่แล้ว
Code (VB.NET)
Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
Dim Drv As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'*** UserID ***'
Dim lblUserID As Label = DirectCast(e.Item.FindControl("lblUserID"), Label)
If Not IsNothing(lblUserID) Then
lblUserID.Text = Drv("userID").ToString()
End If
'*** Username ***'
Dim lblUsername As Label = DirectCast(e.Item.FindControl("lblUsername"), Label)
If Not IsNothing(lblUsername) Then
lblUsername.Text = Drv("username").ToString()
End If
'*** Password ***'
Dim lblPassword As Label = DirectCast(e.Item.FindControl("lblPassword"), Label)
If Not IsNothing(lblPassword) Then
lblPassword.Text = Drv("UserPassword").ToString()
End If
'*** Name ***'
Dim lblName As Label = DirectCast(e.Item.FindControl("lblName"), Label)
If Not IsNothing(lblName) Then
lblName.Text = Drv("Name").ToString()
End If
'*** LastName ***'
Dim lbllname As Label = DirectCast(e.Item.FindControl("lbllname"), Label)
If Not IsNothing(lbllname) Then
lbllname.Text = Drv("Lastname").ToString()
End If
'*** Email ***'
Dim lblEmail As Label = DirectCast(e.Item.FindControl("lblEmail"), Label)
If Not IsNothing(lblEmail) Then
lblEmail.Text = Drv("Email").ToString()
End If
'*** Address ***'
Dim lblAddress As Label = DirectCast(e.Item.FindControl("lblAddress"), Label)
If Not IsNothing(lblAddress) Then
lblAddress.Text = Drv("Address").ToString()
End If
'*** Delete ***'
Dim lnkDelete As LinkButton = DirectCast(e.Item.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');")
End If
End If
End Sub