Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: EditColumnControl Events' help for dynamically created DataGrid?


Message #1 by "Jason Larion" <Jason.Larion@W...> on Tue, 9 Apr 2002 20:56:42
Hello all,

   Looking for some help with the events for an EditColumnControl, being 
used in a datagrid created in the CodeBehind page (i.e., not with 
<asp:datagrid...).

   In short, I have a table that contains a datagrid that has been created 
in the CodeBehind.  I would like to be able to use an EditColumnControl 
control to add edit/update/cancel features for this 'grid, but can't for 
the life of me figure out how to do it.

   I've attached a mock-up, called debug.aspx.  It gives the basic 
structure of what I'm attempting to do.  If anyone could point me in the 
correct direction (all examples I can find use the <asp:DataGrid tag), as 
to how to add edit-able (?) functionality to that 'grid, it would be much 
appreciated!

Thanks,
Jason

NOTE: The code references a local SQL DB/table, debug and TableTest, 
respectively.  They look like this:

 FirstName  LastName
 ---------- ----------
 John       Smith
 John       Doe
 Jane       Doe


====================== Debug.aspx [part of solution called 'Debug']

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Debug.aspx.vb" 
Inherits="Debug.Debug" Debug="True" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>Debug</title>
		<meta name="GENERATOR" content="Microsoft Visual 
Studio.NET 7.0">
		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" 
content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<asp:Table id="myTable" runat="server">
			<asp:tablerow>
				<asp:TableCell ID="myTableCell1" 
Runat="server">
				</asp:TableCell>
			</asp:tablerow>
		</asp:Table>
	</body>
</HTML>


====================== Debug.aspx.vb

Imports System.Data
Imports System.Data.SqlClient

Public Class Debug
    Inherits System.Web.UI.Page
    Protected WithEvents myTable As System.Web.UI.WebControls.Table
    Protected WithEvents myTableCell1 As 
System.Web.UI.WebControls.TableCell


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then
            'Um...stuff?
        End If
    End Sub

    Private Sub myTableCell1_Init(ByVal sender As System.Object, ByVal e 
As System.EventArgs) Handles myTableCell1.Init
        'DataGrids must be within a form tag
        Dim f As HtmlForm = New HtmlForm()
        'Add the table containing the datagrid [generated later] to the 
form
        f.Controls.Add(DataGridTest)

        'Add the form to the cell
        myTableCell1.Controls.Add(f)

    End Sub



#Region "Custom Functions"

    Function DataGridTest() As Table
        Dim dg As DataGrid = New DataGrid()
        dg = GetData()

        'Initialise and build EditCommandColumn
        Dim EditCmdCol As EditCommandColumn = New EditCommandColumn()
        EditCmdCol.ButtonType = ButtonColumnType.LinkButton '
        EditCmdCol.UpdateText = "Update"
        EditCmdCol.EditText = "Edit"
        EditCmdCol.CancelText = "Cancel"

        'Add ECC Column to Grid
        dg.Columns.Add(EditCmdCol)
        dg.DataBind()

        'Create quick table to return grid
        Dim t As Table = New Table()
        Dim r As TableRow = New TableRow()
        Dim c As TableCell = New TableCell()
        c.Controls.Add(dg)
        r.Cells.Add(c)
        t.Rows.Add(r)

        'Return created table
        DataGridTest = t

    End Function    'DataGridTest

    Function GetData() As DataGrid

        Dim cString As String = New String("server=127.0.0.1;" & _
                                            "database=debug;" & _
                                            "User Id=test;" & _
                                            "pwd=test")

        Dim mySqlConn As SqlConnection = New SqlConnection(cString)
        Dim mySqlCommand = New SqlCommand("SELECT " & _
                                            "FirstName, " & _
                                            "LastName " & _
                                          "FROM TableTest", mySqlConn)

        Dim mySqlAdp As SqlDataAdapter = New SqlDataAdapter(mySqlCommand)
        Dim dt As DataTable = New DataTable()

        Try
            mySqlConn.Open()
            mySqlAdp.Fill(dt)
        Catch e As Exception

        Finally
            'Clean up Adapter
            If Not (mySqlAdp Is Nothing) Then
                mySqlAdp = Nothing
            End If
            'Close DB
            If (mySqlConn.State = ConnectionState.Open) Then
                mySqlConn.Close()
            End If
        End Try

        'Set some column names on the table
        dt.Columns(0).ColumnName = "First Name"
        dt.Columns(1).ColumnName = "Last Name"

        'Add table to grid
        Dim dv As DataView = New DataView(dt)       'Make view of table 
returned by SQL
        Dim dg As DataGrid = New DataGrid()
        dg.DataSource = dv
        dg.DataBind()

        'Return created grid
        GetData = dg

    End Function    'GetData
#End Region


End Class



  Return to Index