本文是備份,原作請看:ADO.NET #3 (GridView + SqlDataSource)完全手寫、後置程式碼!

http://www.dotblogs.com.tw/mis2000lab/archive/2008/09/15/5377.aspx

 

 

 

 

這篇文章超悶!

因為全部都用後置程式碼(Code Behind)來作,

所以,GridView的「分頁」、「編輯」、「刪除」、「更新」......通通採用後置程式碼(自己動手寫)

在許多網路論壇上,會有人發問這樣的問題。因為入門書比較不會講到這些,但學到一陣子之後,就會遇上相關問題了。

 

一般入門書,介紹 GridView + SqlDataSource都是靠精靈,設定畫面一步一步地完成。請看這篇文章(PDF檔)......文章下載

 

這個範例,請搭配我書本裡面的 test資料表,可以當成 本書 10.3節 Ch 14章的補充範例。

Ch 13/14這兩章,是我書本裡面的特色。......新書上市-- ASP.NET專題實務(文魁出版)

目前的ASP.NET 2.0 /3.5的書籍,連ADO.NET都少見了(大多只講 SqlDataSource),更別提到「手寫」的ADO.NET程式。

 

===============================================

建議:初學者勿看!

基礎不好,看了會消化不良、拉肚子的。

先把基本功夫練好,下面的範例才會感到有用。

已經學會 GridView的樣版(Template)、SqlDataSource之後再看下去

底下的範例,因為自己寫程式,會瞭解 GridView的各種事件。

例如:GridView是如何完成編輯、分頁、更新等等動作,下面程式碼其實是完全公開了這些過程。

===============================================

 

第一,HTML畫面設計。

      只有一個 GridView,並啟動「編輯」、「分頁」等功能。重點是設定好 DataKeys屬性!

      但畫面上沒有 SqlDataSource作資料繫結。

 

第二,後置程式碼(Code Behind)。

      因為自己動手在後置程式碼裡面,撰寫 SqlDataSource,我上MSDN也沒找到好的範例。

      自己「硬Coding」,寫得不好,請大家見諒。

 

    Sub myDBInit()   '==== 自己寫的程式 ====
        '--------------------------------------------------
        '-----  手動撰寫 SqlDataSource      -----
        '--------------------------------------------------
        Dim SqlDataSource1 As SqlDataSource = New SqlDataSource
        '== 1.連結資料庫的連接字串 ConnectionString  ,事先已經寫在 Web.Config檔案裡面了==
        SqlDataSource1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString
        '== 2.撰寫SQL指令 ==
        SqlDataSource1.SelectCommand = "SELECT * FROM [test]"
        '== 3.執行SQL指令 .select() ==
        SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet

        Dim args As New DataSourceSelectArguments
        Dim dv As Data.DataView = SqlDataSource1.Select(args)

        GridView1.DataSource = dv
        GridView1.DataBind()


        SqlDataSource1.Dispose()
    End Sub   '==== 自己寫的程式 ====

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            myDBInit()

            Response.Write("第一次進入畫面.....If Not Page.IsPostBack Then.....<br>")
        Else
            Response.Write("第<b>N</b>次進入畫面.....<br>")
        End If
    End Sub

 

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        myDBInit()

        Response.Write("GridView1_PageIndexChanging()....分頁ing...<br>")
    End Sub

    Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
        Response.Write("GridView1_PageIndexChanged()....分頁完成<br>")
    End Sub

 

 

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        myDBInit()
        Response.Write("GridView1_RowEditing()....進入編輯模式....<br>")
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        myDBInit()
        Response.Write("GridView1_RowCancelingEdit()....離開(取消)編輯模式....<br>")
    End Sub

 

 

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
'==這邊的程式很難。要抓格子,還要自己算一下是第幾格?請看本書P. 10-20頁的圖片與解說


        Dim myTest1 As TextBox = GridView1.Rows(e.RowIndex).Cells(2).Controls(0)
        Dim myTest2 As TextBox = GridView1.Rows(e.RowIndex).Cells(3).Controls(0)

        Response.Write("GridView1_RowUpdating()....開始更新資料....<br>")

        Dim my_test_time, my_title, my_summary, my_article As New TextBox
        my_test_time = GridView1.Rows(e.RowIndex).Cells(2).Controls(0)
        my_title            = GridView1.Rows(e.RowIndex).Cells(3).Controls(0)
        my_summary = GridView1.Rows(e.RowIndex).Cells(4).Controls(0)
        my_article       = GridView1.Rows(e.RowIndex).Cells(5).Controls(0)

        Dim my_id As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value)  '====主索引鍵====

'==下面的程式,只是作SQL指令的更新動作而已

        Dim SqlDataSource1 As SqlDataSource = New SqlDataSource       
        SqlDataSource1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString

        '== 設定SQL指令將會用到的參數 ==
        SqlDataSource1.UpdateParameters.Add("id", my_id)
        SqlDataSource1.UpdateParameters.Add("test_time", my_test_time.Text)
        SqlDataSource1.UpdateParameters.Add("title", my_title.Text)
        SqlDataSource1.UpdateParameters.Add("summary", my_summary.Text)
        SqlDataSource1.UpdateParameters.Add("article", my_article.Text)

        SqlDataSource1.UpdateCommand = "UPDATE [test] SET [test_time] = @test_time, [title] = @title, [summary] = @summary, [article] = @article WHERE [id] = @id"

        SqlDataSource1.Update()

        SqlDataSource1.Dispose()
        '==========================================
    End Sub


    Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated
        '==== 離開編輯模式 ====
        GridView1.EditIndex = -1
        myDBInit()
    End Sub

 

 

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        Dim SqlDataSource1 As SqlDataSource = New SqlDataSource
        '== 連結資料庫的連接字串 ConnectionString  ==
        SqlDataSource1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString

        Dim my_id As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value.ToString)    '====主索引鍵====
        SqlDataSource1.DeleteParameters.Add("id", my_id)  '== 設定SQL指令將會用到的參數 ==

        SqlDataSource1.DeleteCommand = "Delete from [test] WHERE [id] = @id"
        SqlDataSource1.Delete()

        Response.Write("*** 刪除成功! ***")
    End Sub

    Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles GridView1.RowDeleted
        myDBInit()
    End Sub

 

考考您 (Q&A):

    上面的各個事件裡面,為何都要重複執行 myDBInit()?

    如果不執行的話,會怎麼樣?

 

      答案請看本書 10.5節,我用一整節的文章來作說明。......新書上市-- ASP.NET專題實務(文魁出版)

 

 

---------------------------------------------------------------------

 不是我藏私啦

一本書上限600頁,我都寫到快850頁,還有好多東西塞不進去。

 

這個範例,在以前 ASP.NET 1.x版,是人人都會寫的範例。

因為那個時候,沒有 SqlDataSource這類的資料來源控制項,很多事都要自己動手寫。

 

到了ASP.NET 2.0以後,一切都方便、自動化了。書本也不提這一部份了,書本只談新的控制項,不會回頭去談一些基礎。

(因為新東西太多,新的都談不完,哪還有空回頭談別的) 

 

「出來混,總是要還的」,寫到(或 學到)某一種程度,就會遇上了。

所以網路論壇上,還是很常見到這些問題。  

 

這次公開了,基礎夠的話,研究一下就會懂。     如果不懂,也不要急,一定是有些基礎還不穩固,有些該學的還沒學到。

我的書本裡面,第十章也有這個範例,但採用 DataSet + DataAdapter來作(已經出書了,這範例暫不公開)

 

真的!學會 ADO.NET的程式,自己動手寫。

很多問題都可以自己動手解決掉,硬要使用 Web控制項來湊合,反而有點礙手礙腳。

arrow
arrow
    全站熱搜

    MIS2000 Lab 發表在 痞客邦 留言(0) 人氣()