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,027

HOME > ASP.NET > ASP.NET Data Control > ASP.NET LinqDataSource - asp:LinqDataSource



Clound SSD Virtual Server

ASP.NET LinqDataSource - asp:LinqDataSource

ASP.NET LinqDataSource- asp:LinqDataSource เป็น Control ใน Visual Studio ที่ใช้สำหรับ LinqDataSource ซึ่งในตัวอย่างนี้จะเป็นการสร้าง LINQ To SQL (SQL Server) โดยใช้ Control ที่มีมาให้ของ Visual Studio

LINQ คืออะไร ขออ้างอิงจากบทความของคุณ blurEyes

Framework : 2,3,4

ASP.NET LinqDataSource


Syntax

<asp:LinqDataSource ID="myLinqDataSource" runat="server"></asp:LinqDataSource>



ASP.NET LinqDataSource

เริ่มจากการคลิกขวาที่ Project เลือก Add New Item

ASP.NET LinqDataSource

เลือก LINQ To SQL Classes

ASP.NET LinqDataSource

เลือก Yes เพื่อวาง Code ไว้ที่ App_Code

ASP.NET LinqDataSource

จะได้พื้นที่สำหรับการ Design Class Database








ASP.NET LinqDataSource

ให้เลือก Server Explorer ของ SQL Server

ASP.NET LinqDataSource

ลาก Table เข้ามาในส่วนของ Design Class พร้อมกับ Save

ASP.NET LinqDataSource

ลาก Control ชื่อ LinqDataSource เข้ามาใน Web Form และคลกที่ Configure Data Source

ASP.NET LinqDataSource

เลือก Class DataContext ที่ได้จากการสร้างข้างต้น

ASP.NET LinqDataSource

เลือก Table ที่ต้องการ

ASP.NET LinqDataSource

ทดสอบกับ Control ของ GridView

ASP.NET LinqDataSource

ตัวอย่างที่ 1 เลือก DataSource ของ LinqDataSource ที่ได้สร้างขั้นมา


ASP.NET LinqDataSource



ตัวอย่างที่ 2 สร้าง Control ของ GridView ไว้โดยไม่ต้องกำหนด DataSource

ASP.NET LinqDataSource

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Load
        '*** For GridView 2 ***'
        Dim dvLq As New myDataClassesDataContext
        Dim ds = From c In dvLq.customers _
                 Where c.CustomerID = "C001"

        Me.GridView2.DataSource = ds
        Me.GridView2.DataBind()
    End Sub

End Class


ทดสอบการ BindData จากคำสั่งของ Linq ให้เขียนคำสั่งเหมือนในตัวอย่าง



ASP.NET LinqDataSource

ผลลัพธ์ที่ได้

Code นี้สามารถประยุกต์ใช้ได้ทั้ง VB.NET และ C#

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ThaiCreate.Com Tutorial</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="myDataClassesDataContext" EnableDelete="True" 
            EnableInsert="True" EnableUpdate="True" TableName="customers">
        </asp:LinqDataSource>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" 
            DataSourceID="LinqDataSource1">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                    ShowSelectButton="True" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" 
                    SortExpression="CustomerID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="CountryCode" HeaderText="CountryCode" 
                    SortExpression="CountryCode" />
                <asp:BoundField DataField="Budget" HeaderText="Budget" 
                    SortExpression="Budget" />
                <asp:BoundField DataField="Used" HeaderText="Used" SortExpression="Used" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>





Declarative Syntax
<asp:LinqDataSource
    AutoGenerateOrderByClause="True|False"
    AutoGenerateWhereClause="True|False"
    AutoPage="True|False"
    AutoSort="True|False"
    ContextTypeName="string"
    EnableDelete="True|False"
    EnableInsert="True|False"
    EnableTheming="True|False"
    EnableUpdate="True|False"
    EnableViewState="True|False"
    GroupBy="string"
    ID="string"
    OnContextCreated="ContextCreated event handler"
    OnContextCreating="ContextCreating event handler"
    OnContextDisposing="ContextDisposing event handler"
    OnDataBinding="DataBinding event handler"
    OnDeleted="Deleted event handler"
    OnDeleting="Deleting event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnInserted="Inserted event handler"
    OnInserting="Inserting event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelected="Selected event handler"
    OnSelecting="Selecting event handler"
    OnUnload="Unload event handler"
    OnUpdated="Updated event handler"
    OnUpdating="Updating event handler"
    OrderBy="string"
    runat="server"
    Select="string"
    SkinID="string"
    StoreOriginalValuesInViewState="True|False"
    TableName="string"
    Visible="True|False"
    Where="string"
>
        <GroupByParameters />
        <InsertParameters />
        <OrderByParameters />
        <SelectParameters />
        <WhereParameters />
</asp:LinqDataSource>









   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2010-07-04 22:23:25 / 2017-03-28 21:54:43
  Download : Download  ASP.NET LinqDataSource - asp:LinqDataSource
 Sponsored Links / Related

 
ASP.NET Data Control
Rating :

 
ASP.NET SqlDataSource - asp:SqlDataSource
Rating :

 
ASP.NET AccessDataSource - asp:AccessDataSource
Rating :

 
ASP.NET ObjectDataSource - asp:ObjectDataSource
Rating :

 
ASP.NET XmlDataSource - asp:XmlDataSource
Rating :

 
ASP.NET SiteMapDataSource - asp:SiteMapDataSource
Rating :

 
ASP.NET ReportViewer - rsweb:ReportViewer
Rating :

 
ASP.NET DataPager - asp:DataPager
Rating :

 
ASP.NET Chart - asp:Chart
Rating :

 
ASP.NET QueryExtender - asp:QueryExtender
Rating :

 
ASP.NET EntityDataSource - asp:EntityDataSource
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
   







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