Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 20th, 2004, 08:03 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Dynamic Datagrid Generation from Xml

I am buliding one datagrid using xml. From Xml i am getting all the details for the generation of Datagrid. the problem is i am able to call the event for edit but on the particular edititemindex none of the events r working-Update,Cancel. i am Struggling to complete this form .plz Give me a solution. This Form is incomplete.



   Case "DATAGRID"
                Dim objDb As New DBOperations()
                Dim DataGrid1 As New DataGrid()
                DataGrid1.EditItemStyle.CssClass = "eis"
                DataGrid1.AlternatingItemStyle.CssClass = "ais"
                DataGrid1.ItemStyle.CssClass = "is"
                DataGrid1.HeaderStyle.CssClass = "hs"
                DataGrid1.FooterStyle.CssClass = "fs"
                DataGrid1.ShowFooter = True
                DataGrid1.ShowHeader = True
                DataGrid1.ID = "DataGrid1"
                If hdnString.Value <> "" Then
                    DataGrid1.EnableViewState = True
                End If
                DataGrid1.AutoGenerateColumns = False
                DataGrid1.Width = Unit.Percentage(100)
                strCaption = CStr(attrColl("MemberName").Value).Split(",")
                strControls = CStr(attrColl("MemberControls").Value).Split(",")
                If attrColl("Edit").Value = "True" Then
                    Dim tc1 As New TemplateColumn()
                    tc1.HeaderTemplate = New DataGridTemplateLink(ListItemType.Header, "")
                    tc1.ItemTemplate = New DataGridTemplateLink(ListItemType.Item, strCaption(i))
                    tc1.EditItemTemplate = New DataGridTemplateLink(ListItemType.EditItem, strCaption(i))
                    tc1.FooterTemplate = New DataGridTemplateLink(ListItemType.Footer, strCaption(i))
                    DataGrid1.Columns.Add(tc1)

                    Dim bc As New ButtonColumn()
                    bc.CommandName = "Delete"
                    bc.Text = "Delete"
                    DataGrid1.Columns.Add(bc)
                End If
                For i = 0 To attrColl("DTCount").Value - 1
                    Dim tc1 As New TemplateColumn()
                    tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, strCaption(i), strControls(i))
                    tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, strCaption(i), strControls(i))
                    tc1.EditItemTemplate = New DataGridTemplate(ListItemType.EditItem, strCaption(i), strControls(i))
                    tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, strCaption(i), strControls(i))
                    DataGrid1.Columns.Add(tc1)
                Next
                If Not ViewState("EditItemIndex") Is Nothing Then
                    CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = ViewState("EditItemIndex")
                End If

                AddHandler DataGrid1.UpdateCommand, AddressOf UpdateCommand
                AddHandler DataGrid1.ItemCommand, AddressOf ItemCommand
                AddHandler DataGrid1.ItemDataBound, AddressOf DataGrid1_ItemDataBound
                AddHandler DataGrid1.EditCommand, AddressOf EditCommand
                AddHandler DataGrid1.DeleteCommand, AddressOf DeleteCommand
                AddHandler DataGrid1.CancelCommand, AddressOf CancelCommand

                For i = 0 To attrColl("DTCount").Value - 1
                    myDataTable.Columns.Add(strCaption(i))
                Next
                Dim dsEditDetail As New DataTable()
                Dim strRow As DataRow
                Dim tblRow As DataRow
                dsEditDetail = objDb.GetDataTableSP("Usp_master_Detail_select")
                For Each strRow In dsEditDetail.Rows
                    tblRow = myDataTable.NewRow
                    For i = 0 To attrColl("DTCount").Value - 1
                        tblRow(strCaption(i)) = strRow.Item(i)
                    Next
                    myDataTable.Rows.Add(tblRow)
                Next
                If hdnString.Value = "" Then
                    DataGrid1.DataSource = myDataTable
                    DataGrid1.DataBind()
                    Session("myDataTable") = myDataTable
                    hdnString.Value = 1
                Else
                    myDataTable = Session("myDataTable")
                    DataGrid1.DataSource = myDataTable
                    DataGrid1.DataBind()
                    Session("myDataTable") = myDataTable
                End If
                controlcell.Controls.Add(DataGrid1)
                controlcell.ColSpan = 2
                row.Controls.Add(controlcell)
                MainForm.Controls.Add(row)
        End Select







 Private Class DataGridTemplate
        Implements ITemplate
        Dim templateType As ListItemType
        Dim columnName As String
        Dim controlName As String

        Sub New(ByVal type As ListItemType, ByVal ColName As String, ByVal conName As String)
            templateType = type
            columnName = ColName
            controlName = conName
        End Sub

        Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
            Dim lc As New Label()
            Select Case templateType
                Case ListItemType.Header
                    lc.Text = "<B>" & columnName & "</B>"
                    container.Controls.Add(lc)
                Case ListItemType.Item
                    lc.ID = "lbl" & columnName
                    AddHandler lc.DataBinding, AddressOf BindStringColumn
                    container.Controls.Add(lc)
                Case ListItemType.EditItem
                    Dim LabelEdit As New Label()
                    LabelEdit.ID = "lblE" & columnName
                    AddHandler LabelEdit.DataBinding, AddressOf BindStringColumn
                    LabelEdit.Visible = False
                    container.Controls.Add(LabelEdit)
                    If controlName = "TEXTBOX" Then
                        Dim tb As New TextBox()
                        tb.ID = "txtE" & columnName
                        container.Controls.Add(tb)
                        tb.Text = LabelEdit.Text
                    End If
                    If controlName = "RADIOBUTTON" Then
                        Dim rad As New TextBox()
                        rad.Text = ""
                        container.Controls.Add(rad)
                    End If
                    If controlName = "ComboBox" Then
                        Dim ddl As New DropDownList()
                        container.Controls.Add(ddl)
                    End If
                    If controlName = "DateTime" Then
                        Dim tb As New TextBox()
                        tb.Text = ""
                        container.Controls.Add(tb)
                    End If
                    If controlName = "TextArea" Then
                        Dim tb As New TextBox()
                        tb.Text = ""
                        tb.TextMode = TextBoxMode.MultiLine
                        tb.Width = Unit.Pixel(200)
                        tb.Height = Unit.Pixel(65)
                        container.Controls.Add(tb)
                    End If


                Case ListItemType.Footer
                    If controlName = "TEXTBOX" Then
                        Dim txtF As New TextBox()
                        txtF.ID = "txtF" & columnName
                        txtF.Text = ""
                        container.Controls.Add(txtF)
                    End If
                    If controlName = "RADIOBUTTON" Then
                        Dim rad As New RadioButton()
                        rad.Text = ""
                        container.Controls.Add(rad)
                    End If
                    If controlName = "ComboBox" Then
                        Dim ddl As New DropDownList()
                        container.Controls.Add(ddl)
                    End If
                    If controlName = "DateTime" Then
                        Dim txt As New TextBox()
                        Dim Alink As New LinkButton()
                        Dim imag As New System.Web.UI.WebControls.Image()

                        Alink.Attributes.Add("Onclick", "return open_popup('../calendar.aspx?formname=Form1." + columnName + "')")
                        Alink.Attributes.Add("href", "href='../calendar.aspx?formname=Form1." + columnName + "'")
                        imag.ImageUrl = "../images/calender.gif"

                        txt.ID = "txtD" & columnName
                        txt.MaxLength = 13
                        txt.ReadOnly = True
                        txt.Width = Unit.Pixel(130)
                        Alink.Controls.Add(imag)
                        container.Controls.Add(txt)
                        container.Controls.Add(Alink)
                    End If
                    If controlName = "TextArea" Then
                        Dim txt As New TextBox()
                        txt.ID = "txtArea" & columnName
                        txt.AutoPostBack = False
                        txt.TextMode = TextBoxMode.MultiLine
                        txt.Width = Unit.Pixel(200)
                        txt.Height = Unit.Pixel(65)
                        container.Controls.Add(txt)
                    End If
            End Select
        End Sub
        Sub BindStringColumn(ByVal sender As Object, ByVal e As EventArgs)

            Dim lbl2 As Label = CType(sender, Label)

            Dim Container As DataGridItem = CType(lbl2.NamingContainer, DataGridItem)

            lbl2.Text = DataBinder.Eval(Container.DataItem, columnName)

        End Sub

    End Class

    Private Class DataGridTemplateLink
        Implements ITemplate
        Dim templateType As ListItemType
        Dim columnName As String

        Sub New(ByVal type As ListItemType, ByVal ColName As String)
            templateType = type
            columnName = ColName
        End Sub

        Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
            Dim lc As New Label()
            Select Case templateType
                Case ListItemType.Header
                    lc.Text = ""
                    container.Controls.Add(lc)
                Case ListItemType.Item
                    Dim Edit As New LinkButton()
                    Edit.CommandName = "Edit"
                    Edit.ID = "lnkEdit"
                    Edit.Text = "Edit"
                    container.Controls.Add(Edit)
                Case ListItemType.EditItem
                    Dim Update As New LinkButton()
                    Update.CommandName = "Update"
                    Update.Text = "Update"
                    Update.ID = "lnkUpdate"
                    Dim Cancel As New LinkButton()
                    Cancel.CommandName = "Cancel"
                    Cancel.ID = "lnkCancel"
                    Cancel.Text = "Cancel"
                    Dim lbl As New Label()
                    lbl.Text = " "
                    container.Controls.Add(Update)
                    container.Controls.Add(lbl)
                    container.Controls.Add(Cancel)
                Case ListItemType.Footer
                    lc.Text = ""
                    container.Controls.Add(lc)
            End Select
        End Sub

    End Class












  Private Sub ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        If e.CommandName = "Update" Then
            Dim strAction As String = ""
            Dim strUser As String = ""
            Dim strUserList As String()
            myDataTable = Session("myDataTable")
            myRow = myDataTable.NewRow
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text

            myDataTable.Rows.Add(myRow)
            Session("myDataTable") = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
        End If

        If e.CommandName = "Add" Then
            Dim strAction As String = ""
            Dim strUser As String = ""
            Dim strUserList As String()
            myDataTable = Session("myDataTable")
            myRow = myDataTable.NewRow
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text

            myDataTable.Rows.Add(myRow)
            Session("myDataTable") = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
        End If
    End Sub


    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        Try
            Dim i As Integer
            If e.Item.ItemType = ListItemType.EditItem Then
                For i = 0 To strCaption.Length - 1
                    Select Case strControls(i).ToUpper
                        Case "TEXTBOX"
                            CType(e.Item.FindControl("txtE" & strCaption(i)), TextBox).Text = CType(e.Item.FindControl("lblE" & strCaption(i)), Label).Text


                    End Select
                Next
            End If
        Catch F As Exception
            Response.Write(F.Message)
        End Try
    End Sub

    Private Sub EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try


            myDataTable = Session("myDataTable")
            ViewState("EditItemIndex") = e.Item.ItemIndex
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = e.Item.ItemIndex
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
            Session("myDataTable") = myDataTable
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try
            Dim intcnt
            myDataTable = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = -1
            For Each myRow In myDataTable.Rows
                If intcnt = e.Item.ItemIndex Then
                    myDataTable.Rows.Remove(myRow)
                    myDataTable.AcceptChanges()
                    Exit For
                End If
                intcnt = intcnt + 1
            Next
            myDataTable.AcceptChanges()
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
            Session("myDataTable") = myDataTable
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try
            Dim strAction As String = ""
            Dim strUser As String = ""
            Dim strUserList As String()
            myDataTable = Session("myDataTable")
            Dim intcnt
            intcnt = 0
            Dim i As Integer
            For Each myRow In myDataTable.Rows
                If intcnt = e.Item.ItemIndex Then
                    For i = 0 To strCaption.Length - 1
                        Select Case strControls(i).ToUpper
                            Case "TEXTBOX"
                                myRow(strCaption(i)) = CType(e.Item.FindControl("txtE" & strCaption(i)), TextBox).Text


                        End Select
                    Next

                    myDataTable.AcceptChanges()
                    Session("myDataTable") = myDataTable
                    Exit For

                End If
                intcnt = intcnt + 1
            Next
            myDataTable.AcceptChanges()
            Session("myDataTable") = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = -1
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try
            myDataTable = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = -1
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
            Session("myDataTable") = myDataTable
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

 
Old January 3rd, 2005, 03:26 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why No Reply

 
Old January 6th, 2005, 11:41 AM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the reason the FindControl method is not working is that the controls are created dynamically and are not part of the form ViewState.
Fortunately there is a work around - use the Request.Form object to loop through the input fields.

one more problem I think you have is that the fields names are all the same. you should pass to the DataGridTemplateLink method a unique name for each field you create dynamically.

here is what I did (in c# but easy enough to understand):

        private void dgRecipient_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {

            BindRecipientDataGrid();
            DataSet ds = (DataSet)dgRecipient.DataSource;
            DataTable dt = ds.Tables["tblRecipient"];
            DataRow[] dr = dt.Select("RecipientID = " + dgRecipient.DataKeys[e.Item.ItemIndex]);
            if (dr.Length > 0)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    foreach (string key in Request.Form.AllKeys)
                    {
                        if (key.IndexOf("dgRecipient") != -1 && key.IndexOf(dc.ColumnName) != -1)
                        {
                            dr[0][dc.ColumnName] = Request.Form[key];
                            break;
                        }
                    }
                }

                DataProvider dp = new DataProvider();
                dp.UpdateRecipient(ds);

            }

            BindRecipientDataGrid();
        }


as you can see, I chose not the save my data in the session object, but that's ok. dgRecipient is the ID of my DataGrid control.

good luck,

Rimon


Rimon
 
Old January 7th, 2005, 08:18 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rimon


I got the problem



thanks for ur help





 
Old January 10th, 2005, 10:18 AM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

anoop.j -

Did the solution that Rimon posted correct your problem? I am also having difficultly getting the Cancel and Update events to fire correctly. It doesn't appear the solution that Rimon has posted would have corrected this issue? Thanks

Chris

 
Old January 21st, 2005, 11:21 PM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

anoop.j

Please let us know how you solved this issue. I am encountering the same issue with the update and the cancel events not firing.

Thanks

paulyrusso

 
Old February 4th, 2005, 12:07 PM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am having a similar problem but trying to perform a different task. I have tried it several different ways but to no avail. Here is the project. I am creating a course registration page in VB.NET The CodeBehind reads the current classes from a database and then binds the data to the datagrid. But it needs to be sorted by months.

So first I tried with a Nested Datagrid. I used a SELECT DISTINCT query from the database to display only months that currently had classes assigned to them. The parent datagrid fired....each time it created a row with the month, it fired a subroutine to create a nested datagrid through ITemplate inside that same cell, bound using a DataFilter, and only displayed classes for that month. The nested datagrid's first column was a checkbox, then the course name, date, etc....

This was all enclosed in a form (both datagrids) and a button also below the datagrids. When you click the button, I need to iterate through the checkboxes, find out which ones are checked, and add the data from that row to a DataTable, then pass that DataTable to a second page using Server.Transfer.

I first tested the script before using ITemplate. I created the datagrid on the page listing all the classes. I could check the boxes, and successfully pass the table to the second page and bind it to a new DataGrid. Everything worked great! But as soon as I dynamically created the inner table, it stopped working. FindControl wouldn't work to find the checkboxes. FindControl wouldn't even find the nested datagrid. So, I tried a new method...I completely recoded the page. I took out the Parent DataGrid and replaced it with a Panel control. I then generated my table displaying the months through code using a literal control. The code generates the beginning of the table using the Text property of a Literal, then adds it to the Panel Controls, next, it creates a DataGrid using RowFilter to only display those months, again using ITemplate. Then it generates another literal control closing off the table.

This code loops for each Month found in the database. The page is created fine....however, I still can't find the dynamically created DataGrid. Now I have a slew of innefficient code from recoding it a million times lol. I'll have some cleaning up to do once I finally do get this to work. But here is the entire code from my CodeBehind (including the class for the receiving page). The second page will actually load, but there is no data in the datatable and the datagrid now does not appear.

Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.OleDB
Imports System.Collections
Imports System.Web.UI.TemplateControl


Public Class myClassReg
 Inherits Page

'Place all ASP Objects from inherited page here 
    Protected WithEvents cmdRegister As Button
    Protected WithEvents phClasses As Panel
    Protected WithEvents tbTestBox As TextBox
'  Protected WithEvents lblClassName As Label
'  Protected WithEvents lblStartDate As Label
'  Protected WithEvents lblEndDate As Label
'  Protected WithEvents lblStartTime As Label
'  Protected WithEvents lblEndTime As Label
'  Protected WithEvents lblPrice As Label

'Global Variable Declarations Go Here
Dim strSQL As String
Dim objDataReader As OleDbDataReader        
        'Set up Database Connection Strings Globally 
           Dim pathtodb As String = Server.MapPath("~") & "\db\training.mdb"
        Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _
                                      "data source=" & pathtodb & ";"
        Dim objConnection As New OleDbConnection(strConnection)
'        Dim dtSelectedClasses As New DataTable
        Dim dtSelectedClasses As New DataTable                       
        Dim dsSchedule As New DataSet()
        Dim objClassDataReader As OleDbDataAdapter

Dim arrMonths As New ArrayList
    'i is the current arraylist index
    Dim i As Integer = 0
    'n is the current control number
    Dim n As Integer = 0

'======================================= 

'=======================================
    Public ReadOnly Property ClassIDs() As System.Object
        Get
          Return dtSelectedClasses
        End Get
    End Property

Sub Page_Load(s As Object, e as EventArgs)
'---------------------------------------------------------------------
'If Not Page.IsPostBack Then
        'Populate an array list with the distinct months of current classes
            Dim strMonthSQL As String = "SELECT DISTINCT tblScheduledClasses.Months " & _
                         "FROM tblInstructors INNER JOIN (tblClasses INNER JOIN tblScheduledClasses " & _
                         "ON tblClasses.ClassID = tblScheduledClasses.ClassID) " & _
                         "ON tblInstructors.InstructorID = tblScheduledClasses.InstructorID; "

            Dim myMonthCommand As New OleDbCommand(strMonthSQL, objConnection)

            Dim objMonthDataReader As OleDbDataReader
            objConnection.Open()
            objMonthDataReader = myMonthCommand.ExecuteReader(CommandBehavior.CloseConnection)
            Dim dbfield As String = "Months"

            While objMonthDataReader.Read()
                arrMonths.Add(objMonthDataReader(dbfield))
            End While
            objConnection.Close()
        'Populate a DataSet with the currently offered classes
            strSQL = "SELECT tblScheduledClasses.ScheduleID, tblScheduledClasses.StartDate, tblScheduledClasses.Months, tblScheduledClasses.EndDate, " & _
                     "tblScheduledClasses.StartTime, tblScheduledClasses.EndTime, tblScheduledClasses.Price, tblClasses.ClassName " & _
                     "FROM tblClasses INNER JOIN tblScheduledClasses ON tblClasses.ClassID = tblScheduledClasses.ClassID;"

    'Create Connection Object
        Dim myobjCommand as New OleDbCommand(strSQL, objConnection)
        objClassDataReader = New OleDbDataAdapter(strSQL, objConnection)

    'Create Data Reader to capture Data        
    objClassDataReader.Fill(dsSchedule, "Classes")
            'close connection and clean up
                objConnection.Close()
                pathtodb = nothing
                strConnection = nothing
                strSQL = nothing

        'Now that data is captured, Call function to create a table and datagrid for each month

For i = 0 To arrMonths.Count - 1
 BuildGrids()
Next


'End If

End Sub    

'=======================================
Public Sub BuildGrids()
Dim ltlTableStart as New Literal
    'create beginning of table to hold the newly created datagrid
    ltlTableStart.Text="<Table width = ""100%"">  <tr> " & _
                        "<td>" & arrMonths(i) & "</td></td>" & _
                        "<tr><td>"
    'Add new literal to Placeholder Controls
    phClasses.Controls.Add(ltlTableStart)
    'increment n so next control gets put into next slot
    n = n + 1

    'Now create the new datagrid from the ITemplate user control
    Dim dgSchedule as New DataGrid()
    dgSchedule.ShowHeader=false
    dgSchedule.AutoGenerateColumns = false
    dgSchedule.GridLines = false

            Dim ccColorConverter
            ccColorConverter = New System.Drawing.ColorConverter
            dgSchedule.BackColor = CType(ccColorConverter.ConvertFrom("#ffffff"), System.Drawing.Color)
            dgSchedule.AlternatingItemStyle.BackColor = CType(ccColorConverter.ConvertFrom("#D8DCDF"), System.Drawing.Color)

    Dim temp as ITemplate
    temp = Page.LoadTemplate("../UserControls/scheduledclasses.ascx")
    Dim tc as TemplateColumn
    tc = new TemplateColumn()
    tc.ItemTemplate = temp
    dgSchedule.Columns.Add(tc)

    'Next, use a Data Filter to only bind records for the current month
      Dim CurrentMonth = arrMonths(i)

      'Create a DataView that has only the applicable IDs
      Dim properClasses as DataView = dsSchedule.Tables("Classes").DefaultView
      properClasses.RowFilter = "Months='" & CurrentMonth & "'"

      'Bind the Data to the DataGrid
      dgSchedule.DataSource = properClasses
      dgSchedule.DataBind()

      'Add the datagrid to the next slot in the PlaceHolder controls
        phClasses.Controls.Add(dgSchedule)

      'increment n again to step to next open control number
          n = n + 1

    'Create new literal with closing table html and input the html
    Dim ltlTableEnd as New Literal
    ltlTableEnd.Text = "</td></tr></Table>"
    'Add new literal to PlaceHolder Controls and increment n for next control addition
    phClasses.Controls.Add(ltlTableEnd)

    'Table and DataGrid created, return to PageLoad for next iteration

End Sub

'=======================================
    Private Sub cmdRegister_Click(ByVal sender As _
                       System.Object, ByVal e As System.EventArgs) _
                       Handles cmdRegister.Click
    'Create DataTable and columns to store checked classes from Registration table
            Dim dcClassName as New DataColumn("ClassName", GetType(String))
            Dim dcClassDates as New DataColumn("ClassDates", GetType(String))
            Dim dcClassTimes as New DataColumn("ClassTimes", GetType(String))
            Dim dcClassPrices as New DataColumn("ClassPrices", GetType(String))
    'Add New Columns to New Datatable
        dtSelectedClasses.Columns.Add(dcClassName)
        dtSelectedClasses.Columns.Add(dcClassDates)
        dtSelectedClasses.Columns.Add(dcClassTimes)
        dtSelectedClasses.Columns.Add(dcClassPrices)                       

            Dim ccColorConverter
            ccColorConverter = New System.Drawing.ColorConverter                       

        Dim chkClassSelect As CheckBox
        Dim dgSchedule As DataGrid

        'Iterate through controls in PlaceHolder
        Dim strCombine As String
        Dim oDataGridItem As DataGridItem
        Dim c As Control
        Dim d As Control

        For Each c in phClasses.Controls
            tbTestBox.Text="For Each Loop Executed"

        dgSchedule = CType(phClasses.FindControl("dgSchedule"), DataGrid)
                'Once dgSchedule is found, iterate through checkboxes in dgSchedule
                For Each oDataGridItem in dgSchedule.Items
                dgSchedule.BackColor = CType(ccColorConverter.ConvertFrom("#D80000"), System.Drawing.Color)
                    chkClassSelect = CType(dgSchedule.FindControl("chkClassSelct"), CheckBox)
                        'If current chkbox is checked, add line to Data Table
                        If chkClassSelect.Checked Then
                            'Create New DataRow for each checked item and add to datatable
                             Dim drSelectedClasses As DataRow = dtSelectedClasses.NewRow()
                              drSelectedClasses("ClassName") = CType(dgSchedule.FindControl("lblClassName"), Label).Text
                                  'Concatenate Dates
                                strCombine = CType(dgSchedule.FindControl("lblStartDate"), Label).Text & " - " & CType(dgSchedule.FindControl("lblEndDate"), Label).Text

                            drSelectedClasses("ClassDates") = strCombine

                                  'Concatenate Times
                                strCombine = Ctype(dgSchedule.FindControl("lblStartTime"), Label).Text & " - " & Ctype(dgSchedule.FindControl("lblEndTime"), Label).Text
                            drSelectedClasses("ClassTimes") = strCombine

                          drSelectedClasses("ClassPrices") = CType(dgSchedule.FindControl("lblPrice"), Label).Text
                          'Add new Rows to DataTable
                          dtSelectedClasses.Rows.Add(drSelectedClasses)

                          tbTestBox.Text="a CheckBox was found to be Checked."

                          End If                            
                    Next
        Next
    'clean up and send Data Table to receiving page.    
    strCombine = nothing



    'Server.Transfer("studentinfo.aspx")    

    End Sub

'=======================================

'=======================================
End Class
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'This class accepts Class ID's from Class Selection Page and Registers students.


Public Class myStudentInfo
 Inherits Page

'Place all ASP Objects from inherited page here 

  Protected WithEvents dgStudentClasses As DataGrid
  Protected WithEvents tbFirstName As TextBox
  Protected WithEvents tbLastName As TextBox
  Protected WithEvents tbCompany As TextBox
  Protected WithEvents tbDayPhone As TextBox
  Protected WithEvents tbEmail As TextBox
  Protected WithEvents tbNightPhone As TextBox
'Global Variable Declarations Go Here
Dim objClassReg As myClassReg
Dim strSQL As String
Dim objDataReader as OleDbDataReader        
        'Set up Database Connection Strings Globally 
           Dim pathtodb As String = Server.MapPath("~") & "\db\training.mdb"
        Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _
                                      "data source=" & pathtodb & ";"
        Dim objConnection As New OleDbConnection(strConnection)


'======================================= 

'=======================================


Sub Page_Load(s As Object, e as EventArgs)
'---------------------------------------------------------------------
If Not Page.IsPostBack Then
objClassReg =  CType(Context.Handler, System.Object)


        dgStudentClasses.DataSource=objClassReg.ClassIDs
        dgStudentClasses.DataBind()

End If        
End Sub    

'=======================================

'=======================================

'=======================================
End Class
I have posted to numerous forums without reply....and I've been struggling with this problem for 2 weeks. I'm very new to VB .NET, but this is the first problem I haven't been able to figure out. Any help would be greatly appreciated.

Thanks,

Dustin Horne

Dustin Horne
Web Designer I
IntelliCom Computer Consulting, Inc.
http://www.intellicominc.com
[email protected]
 
Old February 10th, 2005, 04:59 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the problem with the datagrid is u r binding the datagrid before a add the datagrid to the place control

try doing the same thing after adding the control to place holder









Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic PDF Generation sandyg Classic ASP Components 12 June 15th, 2008 03:51 AM
Dynamic generation of ADO recordset Pravin kumar Classic ASP Professional 3 March 12th, 2006 07:50 PM
Dynamic ASP button generation Lloyd_Christmas Classic ASP Basics 1 May 17th, 2005 06:50 PM
Dynamic Button Generation LouMattera BOOK: Professional C#, 2nd and 3rd Editions 2 March 4th, 2005 04:26 PM
dynamic generation of crystal report abroc VB.NET 0 February 13th, 2005 06:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.