M
Miguel Dias Moura
Hello,
I have a GridView in my page which is created in runtime. It works fine.
My page has 2 Asp Buttons:
- The HIDE button makes GridView.Visible = False;
- The SHOW button makes GridView.Visible = True.
I press HIDE and the GridView disappears as expected.
After it I press SHOW and the GridView doesn't show.
In fact it shows because I can see the GridLines separating the right
number of rows.
However, I can't see any row content, header or footer.
My GridView is composed by:
- An asp image control in the header row and in the footer row;
- An asp label and image in a template field.
I am trying to figure this out for 1 week.
I tried everything I could think of.
It doesn't make any sense to me that tha grid controls don't show up
when I press SHOW after I hidded the grid.
Can someone please help me out?
Thank You,
Miguel
I am creating my View Grid using the ItemTemplate contructor as in MSDN
Library.
I add the function which creates the GridView and the Class which i use
to create the ItemTemplate:
-- Function that creates the GridView ---
Private Sub gvProfessor_Build()
' Set gvProfessor properties
With gvProfessor
.AllowPaging = False
.AutoGenerateColumns = False
.BorderColor = Drawing.ColorTranslator.FromHtml("#FFFFFF")
.BorderStyle = BorderStyle.Solid
.BorderWidth = Unit.Pixel(1)
.CellPadding = 1
.CssClass = "gvProfessor"
.GridLines = GridLines.Horizontal
.ShowFooter = True
.ShowHeader = True
.Width = Unit.Pixel(320)
End With
' Add description template column to gvProfessor
Dim tfDescription As New TemplateField
tfDescription.ItemTemplate = New
gvtDescricao(DataControlRowType.DataRow, "description")
gvProfessor.Columns.Add(tfDescription)
' Add icon template column to gvProfessor
Dim tfIcon As New TemplateField
tfIcon.ItemTemplate = New gvtDescricao(DataControlRowType.DataRow,
"icon")
gvProfessor.Columns.Add(tfIcon)
' Set gvProfessor icon column width
gvProfessor.Columns(1).ItemStyle.Width = Unit.Pixel(22)
' Create dtProfessor
Dim dtProfessor As New DataTable
dtProfessor.Columns.Add("description", GetType(String))
dtProfessor.Columns.Add("icon", GetType(Boolean))
' Create dtProfessor rows
Dim rows As Object(,) = {{"Disciplinas", "Acesso a todas as
disciplinas", True}, _
{"Níveis de Ensino", "Acesso a todos os
níveis de ensino", True}, _
{"Documentos", "Consulta, download e
impressão", True}, _
{"Foruns", "Acesso aos fóruns de dúvidas e
de discussão", True}, _
{"Artigos", "Consulta de artigos", True}, _
{"Newsletter", "Subscrição da newsletter
BonsAlunos.com", True}}
' Add rows to dtProfessor
Dim iRow As Integer
For iRow = 0 To rows.GetLength(0) - 1
dtProfessor.Rows.Add(New Object() {"<h1 class='gvProfessor'>" &
rows(iRow, 0) & "</h1>" & _
"<p class='gvProfessor'>" &
rows(iRow, 1) & "</p>", rows(iRow, 2)})
Next iRow
' Create dsProfessor
Dim dsProfessor As New DataSet
dsProfessor.Tables.Add(dtProfessor)
' Bind gvProfessor
gvProfessor.DataSource = dsProfessor
gvProfessor.DataBind()
' Create gvProfessor header
Dim iHeader As New Image
With iHeader
.AlternateText =
Me.GetLocalResourceObject("iProfessor_GvHeader.AlternateText")
.CssClass = "iProfessor_GvHeader"
.ID = "iProfessor_GvHeader"
.ImageUrl = "~/Assets/Design/Images/Professor_GvHeader.jpg"
.ToolTip =
Me.GetLocalResourceObject("iProfessor_GvHeader.ToolTip")
End With
gvProfessor.HeaderRow.Cells(0).Controls.Add(iHeader)
' Create gvProfessor footer
Dim iFooter As New Image
With iFooter
.AlternateText =
Me.GetLocalResourceObject("iProfessor_GvFooter.AlternateText")
.CssClass = "iProfessor_GvFooter"
.ID = "iProfessor_GvFooter"
.ImageUrl = "~/Assets/Design/Images/Professor_GvFooter.jpg"
.ToolTip =
Me.GetLocalResourceObject("iProfessor_GvFooter.ToolTip")
End With
gvProfessor.FooterRow.Cells(0).Controls.Add(iFooter)
End Sub
-- Class which creates the Item Template --
' gvtDescricao
Public Class gvtDescricao
Implements ITemplate
Private templateType As DataControlRowType
Private dataColumn As String
Sub New(ByVal type As DataControlRowType, ByVal column As String)
templateType = type
dataColumn = column
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements
ITemplate.InstantiateIn
' Create the content for the different row types.
Select Case templateType
Case DataControlRowType.Header
' ---
Case DataControlRowType.DataRow
Select Case dataColumn
Case "description"
' Create the control to put in a data row section.
Dim lDescription As New Literal
' Set the control properties.
With lDescription
End With
' Register the event-handling methods to perform the data
binding.
AddHandler lDescription.DataBinding, AddressOf
Description_DataBinding
' Add the control to the controls collection of the
container.
container.Controls.Add(lDescription)
Case "icon"
' Create the control to put in a data row section.
Dim iIcon As New Image
' Set the control properties.
With iIcon
End With
' Register the event-handling methods to perform the data
binding.
AddHandler iIcon.DataBinding, AddressOf Icon_DataBinding
' Add the control to the controls collection of the
container.
container.Controls.Add(iIcon)
End Select
Case Else
' Insert code to handle unexpected values.
End Select
End Sub
Private Sub Description_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
' Bind the lDescription label to GridView data source
Dim lDescription As Literal = CType(sender, Literal)
' Get the GridViewRow object that contains lDescription.
Dim row As GridViewRow = CType(lDescription.NamingContainer,
GridViewRow)
' Get the field value from the GridViewRow object and assign it to
the Text property of the Literal control.
lDescription.Text = DataBinder.Eval(row.DataItem,
"description").ToString()
End Sub
Private Sub Icon_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
' Bind the iIcon image to GridView data source
Dim iIcon As Image = CType(sender, Image)
' Get the GridViewRow object that contains lDescription.
Dim row As GridViewRow = CType(iIcon.NamingContainer, GridViewRow)
' Get the field value from the GridViewRow object and assign it to
the Text property of the Literal control.
With iIcon
.AlternateText = "Disponível"
.ImageUrl = "~/Assets/Design/Images/Verified_20x20_Icon.jpg"
.ToolTip = "Disponível"
End With
End Sub
End Class
I have a GridView in my page which is created in runtime. It works fine.
My page has 2 Asp Buttons:
- The HIDE button makes GridView.Visible = False;
- The SHOW button makes GridView.Visible = True.
I press HIDE and the GridView disappears as expected.
After it I press SHOW and the GridView doesn't show.
In fact it shows because I can see the GridLines separating the right
number of rows.
However, I can't see any row content, header or footer.
My GridView is composed by:
- An asp image control in the header row and in the footer row;
- An asp label and image in a template field.
I am trying to figure this out for 1 week.
I tried everything I could think of.
It doesn't make any sense to me that tha grid controls don't show up
when I press SHOW after I hidded the grid.
Can someone please help me out?
Thank You,
Miguel
I am creating my View Grid using the ItemTemplate contructor as in MSDN
Library.
I add the function which creates the GridView and the Class which i use
to create the ItemTemplate:
-- Function that creates the GridView ---
Private Sub gvProfessor_Build()
' Set gvProfessor properties
With gvProfessor
.AllowPaging = False
.AutoGenerateColumns = False
.BorderColor = Drawing.ColorTranslator.FromHtml("#FFFFFF")
.BorderStyle = BorderStyle.Solid
.BorderWidth = Unit.Pixel(1)
.CellPadding = 1
.CssClass = "gvProfessor"
.GridLines = GridLines.Horizontal
.ShowFooter = True
.ShowHeader = True
.Width = Unit.Pixel(320)
End With
' Add description template column to gvProfessor
Dim tfDescription As New TemplateField
tfDescription.ItemTemplate = New
gvtDescricao(DataControlRowType.DataRow, "description")
gvProfessor.Columns.Add(tfDescription)
' Add icon template column to gvProfessor
Dim tfIcon As New TemplateField
tfIcon.ItemTemplate = New gvtDescricao(DataControlRowType.DataRow,
"icon")
gvProfessor.Columns.Add(tfIcon)
' Set gvProfessor icon column width
gvProfessor.Columns(1).ItemStyle.Width = Unit.Pixel(22)
' Create dtProfessor
Dim dtProfessor As New DataTable
dtProfessor.Columns.Add("description", GetType(String))
dtProfessor.Columns.Add("icon", GetType(Boolean))
' Create dtProfessor rows
Dim rows As Object(,) = {{"Disciplinas", "Acesso a todas as
disciplinas", True}, _
{"Níveis de Ensino", "Acesso a todos os
níveis de ensino", True}, _
{"Documentos", "Consulta, download e
impressão", True}, _
{"Foruns", "Acesso aos fóruns de dúvidas e
de discussão", True}, _
{"Artigos", "Consulta de artigos", True}, _
{"Newsletter", "Subscrição da newsletter
BonsAlunos.com", True}}
' Add rows to dtProfessor
Dim iRow As Integer
For iRow = 0 To rows.GetLength(0) - 1
dtProfessor.Rows.Add(New Object() {"<h1 class='gvProfessor'>" &
rows(iRow, 0) & "</h1>" & _
"<p class='gvProfessor'>" &
rows(iRow, 1) & "</p>", rows(iRow, 2)})
Next iRow
' Create dsProfessor
Dim dsProfessor As New DataSet
dsProfessor.Tables.Add(dtProfessor)
' Bind gvProfessor
gvProfessor.DataSource = dsProfessor
gvProfessor.DataBind()
' Create gvProfessor header
Dim iHeader As New Image
With iHeader
.AlternateText =
Me.GetLocalResourceObject("iProfessor_GvHeader.AlternateText")
.CssClass = "iProfessor_GvHeader"
.ID = "iProfessor_GvHeader"
.ImageUrl = "~/Assets/Design/Images/Professor_GvHeader.jpg"
.ToolTip =
Me.GetLocalResourceObject("iProfessor_GvHeader.ToolTip")
End With
gvProfessor.HeaderRow.Cells(0).Controls.Add(iHeader)
' Create gvProfessor footer
Dim iFooter As New Image
With iFooter
.AlternateText =
Me.GetLocalResourceObject("iProfessor_GvFooter.AlternateText")
.CssClass = "iProfessor_GvFooter"
.ID = "iProfessor_GvFooter"
.ImageUrl = "~/Assets/Design/Images/Professor_GvFooter.jpg"
.ToolTip =
Me.GetLocalResourceObject("iProfessor_GvFooter.ToolTip")
End With
gvProfessor.FooterRow.Cells(0).Controls.Add(iFooter)
End Sub
-- Class which creates the Item Template --
' gvtDescricao
Public Class gvtDescricao
Implements ITemplate
Private templateType As DataControlRowType
Private dataColumn As String
Sub New(ByVal type As DataControlRowType, ByVal column As String)
templateType = type
dataColumn = column
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements
ITemplate.InstantiateIn
' Create the content for the different row types.
Select Case templateType
Case DataControlRowType.Header
' ---
Case DataControlRowType.DataRow
Select Case dataColumn
Case "description"
' Create the control to put in a data row section.
Dim lDescription As New Literal
' Set the control properties.
With lDescription
End With
' Register the event-handling methods to perform the data
binding.
AddHandler lDescription.DataBinding, AddressOf
Description_DataBinding
' Add the control to the controls collection of the
container.
container.Controls.Add(lDescription)
Case "icon"
' Create the control to put in a data row section.
Dim iIcon As New Image
' Set the control properties.
With iIcon
End With
' Register the event-handling methods to perform the data
binding.
AddHandler iIcon.DataBinding, AddressOf Icon_DataBinding
' Add the control to the controls collection of the
container.
container.Controls.Add(iIcon)
End Select
Case Else
' Insert code to handle unexpected values.
End Select
End Sub
Private Sub Description_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
' Bind the lDescription label to GridView data source
Dim lDescription As Literal = CType(sender, Literal)
' Get the GridViewRow object that contains lDescription.
Dim row As GridViewRow = CType(lDescription.NamingContainer,
GridViewRow)
' Get the field value from the GridViewRow object and assign it to
the Text property of the Literal control.
lDescription.Text = DataBinder.Eval(row.DataItem,
"description").ToString()
End Sub
Private Sub Icon_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
' Bind the iIcon image to GridView data source
Dim iIcon As Image = CType(sender, Image)
' Get the GridViewRow object that contains lDescription.
Dim row As GridViewRow = CType(iIcon.NamingContainer, GridViewRow)
' Get the field value from the GridViewRow object and assign it to
the Text property of the Literal control.
With iIcon
.AlternateText = "Disponível"
.ImageUrl = "~/Assets/Design/Images/Verified_20x20_Icon.jpg"
.ToolTip = "Disponível"
End With
End Sub
End Class