Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > ASP.NET > ASP.NET DetailsView Control > ASP.NET DetailsView Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)


ASP.NET DetailsView Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)

ASP.NET DetailsView Control - Visual Studio 2005,2008,2010 (Framework 2.0,3.5,4.0) จากตัวอย่างก่อนหน้านี้จะเป็นการใช้งาน DetailsView และเขียนในรูปแบบของ ASP Style คือเขียนร่วมกับกับส่วนของ HTML Tag แต่ในตัวอย่างนี้ผมได้ยกการเขียนร่วมกับ Visual Studio 2005,2008,2010 ซึ่ง Run บน Framework 2.0,3.5,4.0 ในรูปแบบของ Code-Behind แยกในส่วนของ Tag และ Code ไว้คนล่ะส่วน และใช้การจัดการ Control ผ่าน Code-Behind ทั้งหมด

Code นี้จะเป็นการ Add,Edit,Update และ Delete ข้อมูลในฐานข้อมูล

Language Code : VB.NET || C#

Framework : 2,3,4


DetailsView1.aspx

01.<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DetailsView1.aspx.vb" Inherits="DetailsView1" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04. 
05.<html xmlns="http://www.w3.org/1999/xhtml" >
06.<head runat="server">
07.    <title>ThaiCreate.Com ASP.NET - DetailsView</title>
08.</head>
09.<body>
10.    <form id="form1" runat="server">
11.    <asp:DetailsView id="myDetailsView" runat="server" AutoGenerateRows="False">
12.            <Fields>
13.                <asp:TemplateField HeaderText="CustomerID">
14.                    <ItemTemplate>
15.                        <asp:Label id="lblCustomerID" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:Label>
16.                    </ItemTemplate>
17.                    <EditItemTemplate>
18.                        <asp:TextBox id="txtEditCustomerID" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:TextBox>
19.                    </EditItemTemplate>                   
20.                    <InsertItemTemplate>
21.                        <asp:TextBox id="txtAddCustomerID" size="5" runat="server"></asp:TextBox>
22.                    </InsertItemTemplate>
23.                </asp:TemplateField>
24. 
25.                <asp:TemplateField HeaderText="Name">
26.                    <ItemTemplate>
27.                        <asp:Label id="lblName" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
28.                    </ItemTemplate>
29.                    <EditItemTemplate>
30.                        <asp:TextBox id="txtEditName" size="10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
31.                    </EditItemTemplate>
32.                    <InsertItemTemplate>
33.                        <asp:TextBox id="txtAddName" size="10" runat="server"></asp:TextBox>
34.                    </InsertItemTemplate>                   
35.                </asp:TemplateField>
36. 
37.                <asp:TemplateField HeaderText="Email">
38.                    <ItemTemplate>
39.                        <asp:Label id="lblEmail" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
40.                    </ItemTemplate>
41.                    <EditItemTemplate>
42.                        <asp:TextBox id="txtEditEmail" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:TextBox>
43.                    </EditItemTemplate> 
44.                    <InsertItemTemplate>
45.                        <asp:TextBox id="txtAddEmail" size="20" runat="server"></asp:TextBox>
46.                    </InsertItemTemplate>                                         
47.                </asp:TemplateField>
48. 
49.                <asp:TemplateField HeaderText="CountryCode">
50.                    <ItemTemplate>
51.                        <asp:Label id="lblCountryCode" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:Label>
52.                    </ItemTemplate>
53.                    <EditItemTemplate>
54.                        <asp:TextBox id="txtEditCountryCode" size="2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:TextBox>
55.                    </EditItemTemplate>
56.                    <InsertItemTemplate>
57.                        <asp:TextBox id="txtAddCountryCode" size="2" runat="server"></asp:TextBox>
58.                    </InsertItemTemplate>                                         
59.                </asp:TemplateField>
60. 
61.                <asp:TemplateField HeaderText="Budget">
62.                    <ItemTemplate>
63.                        <asp:Label id="lblBudget" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:Label>
64.                    </ItemTemplate>
65.                    <EditItemTemplate>
66.                        <asp:TextBox id="txtEditBudget" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:TextBox>
67.                    </EditItemTemplate>   
68.                    <InsertItemTemplate>
69.                        <asp:TextBox id="txtAddBudget" size="6" runat="server"></asp:TextBox>
70.                    </InsertItemTemplate>                                      
71.                </asp:TemplateField>
72. 
73.                <asp:TemplateField HeaderText="Used">
74.                    <ItemTemplate>
75.                        <asp:Label id="lblUsed" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:Label>
76.                    </ItemTemplate>
77.                    <EditItemTemplate>
78.                        <asp:TextBox id="txtEditUsed" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:TextBox>
79.                    </EditItemTemplate> 
80.                    <InsertItemTemplate>
81.                        <asp:TextBox id="txtAddUsed" size="6" runat="server"></asp:TextBox>
82.                    </InsertItemTemplate>                                         
83.                </asp:TemplateField>
84.                <asp:CommandField ShowEditButton="True" />
85.                <asp:CommandField ShowInsertButton="True" />
86.            </Fields>   
87.       </asp:DetailsView>
88.    </form>
89.</body>
90.</html>



DetailsView1.aspx.vb

001.Imports System.Data
002.Imports System.Data.OleDb
003.Partial Class DetailsView1
004.    Inherits System.Web.UI.Page
005. 
006.    Dim objConn As OleDbConnection
007.    Dim objCmd As OleDbCommand
008.    Dim strSQL As String
009.    Dim strCusID As String = "C001"
010. 
011.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
012.        Dim strConnString As String
013.        strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";"
014.        objConn = New OleDbConnection(strConnString)
015.        objConn.Open()
016. 
017.        If Not Page.IsPostBack() Then
018.            BindData()
019.        End If
020.    End Sub
021. 
022.    Protected Sub BindData()
023.        strSQL = "SELECT * FROM customer WHERE CustomerID = '" & strCusID & "' "
024. 
025.        Dim dtReader As OleDbDataReader
026.        objCmd = New OleDbCommand(strSQL, objConn)
027.        dtReader = objCmd.ExecuteReader()
028. 
029.        '*** BindData to DetailsView ***'
030.        myDetailsView.DataSource = dtReader
031.        myDetailsView.DataBind()
032. 
033.        dtReader.Close()
034.        dtReader = Nothing
035. 
036.    End Sub
037. 
038.    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
039.        objConn.Close()
040.        objConn = Nothing
041.    End Sub
042. 
043.    Protected Sub myDetailsView_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles myDetailsView.ItemInserting
044.        '*** CustomerID ***'
045.        Dim txtCustomerID As TextBox = CType(myDetailsView.FindControl("txtAddCustomerID"), TextBox)
046.        '*** Email ***'
047.        Dim txtName As TextBox = CType(myDetailsView.FindControl("txtAddName"), TextBox)
048.        '*** Name ***'
049.        Dim txtEmail As TextBox = CType(myDetailsView.FindControl("txtAddEmail"), TextBox)
050.        '*** CountryCode ***'
051.        Dim txtCountryCode As TextBox = CType(myDetailsView.FindControl("txtAddCountryCode"), TextBox)
052.        '*** Budget ***'
053.        Dim txtBudget As TextBox = CType(myDetailsView.FindControl("txtAddBudget"), TextBox)
054.        '*** Used ***'
055.        Dim txtUsed As TextBox = CType(myDetailsView.FindControl("txtAddUsed"), TextBox)
056. 
057.        strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
058.        " VALUES ('" & txtCustomerID.Text & "','" & txtName.Text & "','" & txtEmail.Text & "' " & _
059.        " ,'" & txtCountryCode.Text & "','" & txtBudget.Text & "','" & txtUsed.Text & "') "
060.        objCmd = New OleDbCommand(strSQL, objConn)
061.        objCmd.ExecuteNonQuery()
062. 
063.        strCusID = txtCustomerID.Text
064.        myDetailsView.ChangeMode(DetailsViewMode.ReadOnly)
065.        BindData()
066.    End Sub
067. 
068.    Protected Sub myDetailsView_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs) Handles myDetailsView.ItemUpdating
069.        '*** CustomerID ***'
070.        Dim txtCustomerID As TextBox = CType(myDetailsView.FindControl("txtEditCustomerID"), TextBox)
071.        '*** Email ***'
072.        Dim txtName As TextBox = CType(myDetailsView.FindControl("txtEditName"), TextBox)
073.        '*** Name ***'
074.        Dim txtEmail As TextBox = CType(myDetailsView.FindControl("txtEditEmail"), TextBox)
075.        '*** CountryCode ***'
076.        Dim txtCountryCode As TextBox = CType(myDetailsView.FindControl("txtEditCountryCode"), TextBox)
077.        '*** Budget ***'
078.        Dim txtBudget As TextBox = CType(myDetailsView.FindControl("txtEditBudget"), TextBox)
079.        '*** Used ***'
080.        Dim txtUsed As TextBox = CType(myDetailsView.FindControl("txtEditUsed"), TextBox)
081. 
082.        strSQL = "UPDATE customer SET CustomerID = '" & txtCustomerID.Text & "' " & _
083.        " ,Name = '" & txtName.Text & "' " & _
084.        " ,Email = '" & txtEmail.Text & "' " & _
085.        " ,CountryCode = '" & txtCountryCode.Text & "' " & _
086.        " ,Budget = '" & txtBudget.Text & "' " & _
087.        " ,Used = '" & txtUsed.Text & "' " & _
088.        " WHERE CustomerID = '" & strCusID & "'"
089.        objCmd = New OleDbCommand(strSQL, objConn)
090.        objCmd.ExecuteNonQuery()
091. 
092.        myDetailsView.ChangeMode(DetailsViewMode.ReadOnly)
093.        BindData()
094.    End Sub
095. 
096.    Protected Sub myDetailsView_ModeChanging(ByVal sender As Object, ByVal e As DetailsViewModeEventArgs) Handles myDetailsView.ModeChanging
097.        Select Case e.NewMode
098.            Case DetailsViewMode.Edit
099.                myDetailsView.ChangeMode(DetailsViewMode.Edit)
100.            Case DetailsViewMode.ReadOnly
101.                myDetailsView.ChangeMode(DetailsViewMode.ReadOnly)
102.            Case DetailsViewMode.Insert
103.                myDetailsView.ChangeMode(DetailsViewMode.Insert)
104.        End Select
105.        BindData()
106.    End Sub
107. 
108.End Class




Screenshot

ASP.NET DetailView Control

ASP.NET DetailView Control

ASP.NET DetailView Control

   
Hate it
Don't like it
It's ok
Like it
Love it
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-11-14 23:50:27 / 2017-03-28 21:33:28
  Download : Download  ASP.NET DetailsView Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)
 Sponsored Links / Related

 
ASP.NET DetailsView Control
Rating :

 
ASP.NET DetailsView Control - DataBind
Rating :

 
ASP.NET DetailsView Control - AlternatingRowStyle
Rating :

 
ASP.NET DetailsView Control - TemplateField
Rating :

 
ASP.NET DetailsView Control - DataBound
Rating :

 
ASP.NET DetailsView Control - AllowPaging
Rating :

 
ASP.NET DetailsView Control - Edit/Update
Rating :

 
ASP.NET DetailsView Control - Add/Insert
Rating :

 
ASP.NET DetailsView Control - AccessDataSource
Rating :

 
ASP.NET DetailsView Control - GridView Control
Rating :

 
ASP.NET DetailsView Control - GridView Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)
Rating :

 
ASP.NET DetailsView Control - Microsoft Access (.mdb) - System.Data.OleDb
Rating :

 
ASP.NET DetailsView Control - SQL Server 2000,2005,2008 - System.Data.SqlClient
Rating :

 
ASP.NET DetailsView Control - MySQL Database - MySql.Data.MySqlClient
Rating :

 
ASP.NET DetailsView Control - Oracle Database - System.Data.OracleClient
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่