I am deveoping a web part for deployment on a MOSS 2007 intranet using VB.net. I have a dataset containing ID,name. My template field needs to display a photo (photo filename is the ID number),name and ID number. I created a reapeater control and can successfully display my templated data. I now need to display my data in a grid format. -
photoGrid = New Repeater()
-
photoGrid.ItemTemplate = New MyTemplate()
-
Me.Controls.Add(photoGrid)
-
This code is from CreateChildControls() Sub. -
writer.RenderBeginTag("div")
-
photoGrid.RenderControl(writer)
-
writer.RenderEndTag()
-
This code from RenderContents Sub -
photoGrid.DataSource = ds.Tables("classlist")
-
photoGrid.DataBind()
-
This code from the button click event that runs the sql to fill dataset. -
Public Class MyTemplate
-
Implements ITemplate
-
-
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
-
-
Dim ph As New PlaceHolder()
-
Dim photo As New System.Web.UI.WebControls.Image
-
Dim name As New Label()
-
Dim number As New Label()
-
-
photo.ID = "studphoto"
-
photo.Height = 100
-
photo.Width = 75
-
name.ID = "studname"
-
number.ID = "studnum"
-
-
-
ph.Controls.Add(New LiteralControl("<div>"))
-
ph.Controls.Add(photo)
-
ph.Controls.Add(New LiteralControl("<br />"))
-
ph.Controls.Add(name)
-
ph.Controls.Add(New LiteralControl("<br />"))
-
ph.Controls.Add(number)
-
ph.Controls.Add(New LiteralControl("</div>"))
-
AddHandler ph.DataBinding, New EventHandler(AddressOf Item_DataBinding)
-
-
container.Controls.Add(ph)
-
-
End Sub
-
Code from the template class -
Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
-
-
Dim ph As PlaceHolder = CType(sender, PlaceHolder)
-
Dim ri As RepeaterItem = CType(ph.NamingContainer, RepeaterItem)
-
Dim nameVal As String = Convert.ToString(DataBinder.Eval(ri.DataItem, "Surname"))
-
Dim numVal As Integer = Convert.ToInt32(DataBinder.Eval(ri.DataItem, "Student Code Number"))
-
Dim pathToPhoto As String = "/Student Photos/_w/" & numVal & "_JPG.jpg"
-
Dim currentSite As SPWeb = SPContext.Current.Web
-
' Now test if the photo exists in the current sharepoint site.
-
Try
-
Dim fi As SPFile = currentSite.GetFile(pathToPhoto)
-
-
If fi.Exists Then
-
' do nothing
-
-
Else
-
pathToPhoto = "/Student Photos/_w/face2_JPG.jpg"
-
End If
-
Catch ex As Exception
-
-
End Try
-
-
CType(ph.FindControl("studphoto"), System.Web.UI.WebControls.Image).ImageUrl = pathToPhoto
-
CType(ph.FindControl("studname"), Label).Text = nameVal.ToString()
-
CType(ph.FindControl("studnum"), Label).Text = numVal.ToString()
-
I have followed these and many other articles but have not solved my problem. <links removed>
Any help or suggestions would be much appreciated.
Jack
7 3166
It looks like everything should work.
What are you having problems with?
Displaying the photos?
Creating a table instead of using <div>s?
-Frinny
Hi Frinny,
Thanks for the reply and apologies for the delayed response, been offline for a coupla days.
When I convert my code to use a grid view instead of the repeater control, no picture or data is displayed, checking the page source shows nothing in the img src attribute.
The changes I made to try and convert this to bind with a grid view control are as follows; -
pGrid = New GridView
-
pGrid.AutoGenerateColumns = False
-
-
Dim gvi As DataGridItem = CType(ph.NamingContainer, DataGridItem)
-
-
Dim nameVal As String = Convert.ToString(DataBinder.Eval(gvi.DataItem, "Surname"))
-
-
Dim numVal As Integer = Convert.ToInt32(DataBinder.Eval(gvi.DataItem, "Student Code Number"))
-
I didn't think I had changed enough to introduce any errors lol!
Cheers
Jack
Could you post your GridView markup (in your "code view" of your aspx page) so that I can see what's going on?
-Frinny
All my markup is generated programmatically. The web parts that I am creating will be added to MOSS 2007 pages by the end user.
The web part requires 2 peices of data from the user who then clicks a button.
I am posting the full web part code, hope it helps,
Jack -
Option Explicit On
-
Option Strict On
-
-
Imports System
-
Imports System.Collections
-
Imports System.Data
-
Imports System.Data.SqlClient
-
Imports System.Drawing
-
Imports System.IO
-
Imports System.String
-
Imports System.Text
-
Imports System.Runtime.InteropServices
-
Imports System.Web.UI
-
Imports System.Web.UI.WebControls
-
Imports System.Web.UI.WebControls.WebParts
-
Imports System.Xml.Serialization
-
-
Imports Microsoft.SharePoint
-
Imports Microsoft.SharePoint.WebControls
-
Imports Microsoft.SharePoint.WebPartPages
-
-
Namespace viewGroup_v1
-
-
<Guid("3a4f174b-1726-439a-9c51-127d5984f934")> _
-
Public Class viewGroupV1
-
Inherits System.Web.UI.WebControls.WebParts.WebPart
-
-
'Define local variables to contain property values
-
Private _connectionString As String = ""
-
Private subjectCode, groupNum As TextBox ' Subject code/ group number entry box.
-
Private ds As DataSet ' Dataset to hold db results.
-
Private pGrid As GridView
-
'Private photoGrid As Repeater
-
Private studentPhoto As System.Web.UI.WebControls.Image ' Need to be explicit here.
-
Private subjectLbl, groupLbl As Label
-
Private show As Button
-
Public showError, debug As Literal
-
Private INTstudentCode As Integer ' The students unique id.
-
-
'Create property to hold SQL connection string
-
' NB The conn string has been hard coded for testing purposes.
-
<Personalizable(PersonalizationScope.Shared), WebBrowsable(), WebDisplayName("Connection String:"), _
-
WebDescription("Connection string to use when connecting to SQL source.")> _
-
Property ConnectionString() As String
-
Get
-
Return _connectionString
-
End Get
-
-
Set(ByVal Value As String)
-
_connectionString = Value
-
-
End Set
-
End Property
-
-
Public Sub New()
-
' Add Handlers.
-
AddHandler Me.Init, AddressOf viewGroupV1_Init
-
AddHandler Me.Load, AddressOf viewGroupV1_Load
-
AddHandler Me.PreRender, AddressOf viewGroupV1_PreRender
-
End Sub
-
-
Protected Overrides Sub CreateChildControls()
-
-
'MyBase.CreateChildControls()
-
Try
-
-
' Create the various controls needed for the web part and set required attributes.
-
-
subjectLbl = New Label()
-
subjectLbl.Attributes.Item("for") = "subCode"
-
subjectLbl.Text = "Enter Subject Code"
-
-
subjectCode = New TextBox()
-
subjectCode.Style("padding") = "4px"
-
subjectCode.ID = "subCode"
-
subjectCode.MaxLength = 3
-
subjectCode.Width = 50
-
subjectCode.BackColor = Color.Cornsilk
-
subjectCode.BorderColor = Color.DarkGoldenrod
-
subjectCode.BorderWidth = 1
-
-
groupLbl = New Label()
-
groupLbl.Attributes.Item("for") = "grpNum"
-
groupLbl.Text = "Enter Group Number"
-
-
groupNum = New TextBox()
-
groupNum.Style("padding") = "4px"
-
groupNum.ID = "grpNum"
-
groupNum.MaxLength = 2
-
groupNum.Width = 50
-
groupNum.BackColor = Color.Cornsilk
-
groupNum.BorderColor = Color.DarkGoldenrod
-
groupNum.BorderWidth = 1
-
-
show = New Button()
-
show.Width = 130
-
show.BackColor = Color.Cornsilk
-
show.BorderColor = Color.DarkGoldenrod
-
show.BorderWidth = 1
-
show.Style("padding") = "0px"
-
show.Font.Italic = True
-
show.Text = " Show Class "
-
AddHandler show.Click, AddressOf show_Click
-
-
showError = New Literal()
-
debug = New Literal()
-
-
'photoGrid = New Repeater()
-
'photoGrid.ItemTemplate = New MyTemplate()
-
-
pGrid = New GridView
-
pGrid.AutoGenerateColumns = False
-
-
' Add all the controls to the controls collection.
-
Me.Controls.Add(subjectLbl)
-
Me.Controls.Add(subjectCode)
-
Me.Controls.Add(groupLbl)
-
Me.Controls.Add(groupNum)
-
Me.Controls.Add(show)
-
Me.Controls.Add(showError)
-
Me.Controls.Add(debug)
-
'Me.Controls.Add(photoGrid)
-
Me.Controls.Add(pGrid)
-
-
Catch ex As Exception
-
showError.Text = ex.ToString()
-
End Try
-
-
-
End Sub
-
-
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
-
-
Try
-
-
' Render the web part to the page.
-
-
writer.RenderBeginTag("div")
-
writer.RenderBeginTag("table")
-
writer.RenderBeginTag("tr")
-
writer.RenderBeginTag("td")
-
subjectLbl.RenderControl(writer)
-
writer.RenderEndTag() ' end td
-
writer.RenderBeginTag("td")
-
subjectCode.RenderControl(writer)
-
writer.RenderEndTag() ' end td
-
writer.RenderEndTag() ' end tr
-
-
writer.RenderBeginTag("tr")
-
writer.RenderBeginTag("td")
-
groupLbl.RenderControl(writer)
-
writer.RenderEndTag() ' end td
-
writer.RenderBeginTag("td")
-
groupNum.RenderControl(writer)
-
writer.RenderEndTag() ' end td
-
writer.RenderEndTag() ' end tr
-
writer.RenderBeginTag("tr")
-
writer.RenderBeginTag("td")
-
show.RenderControl(writer)
-
writer.RenderEndTag() ' end td
-
writer.RenderEndTag() ' end tr
-
-
writer.RenderBeginTag("tr")
-
writer.RenderBeginTag("td")
-
writer.RenderEndTag() ' end td
-
writer.RenderBeginTag("td") '
-
-
writer.RenderEndTag() ' end td
-
writer.RenderEndTag() ' end tr
-
writer.RenderEndTag() ' end table
-
debug.RenderControl(writer)
-
showError.RenderControl(writer)
-
writer.RenderEndTag() ' end div
-
-
writer.RenderBeginTag("div")
-
pGrid.RenderControl(writer)
-
writer.RenderEndTag()
-
-
writer.RenderBeginTag("div")
-
photoGrid.RenderControl(writer)
-
writer.RenderEndTag()
-
-
Catch ex As Exception
-
showError.Text = ex.ToString() ' DEBUG
-
End Try
-
-
End Sub
-
-
Public Sub viewGroupV1_Init(ByVal sender As Object, ByVal e As EventArgs)
-
-
' NB Hard coded conn string placed here for testing only.
-
ConnectionString = "DATA SOURCE=testDB1;Initial Catalog=full_test_db;Failover Partner=testDB2;Trusted_Connection=True"
-
-
End Sub
-
-
Public Sub viewGroupV1_Load(ByVal sender As Object, ByVal e As EventArgs)
-
-
' Set web part title and description here.
-
-
End Sub
-
-
Public Sub viewGroupV1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
-
-
' Nothing to pre render on this occasion.
-
-
End Sub
-
-
Protected Sub show_Click(ByVal sender As Object, ByVal e As EventArgs)
-
-
' Check the user supplied parameters and remove any illegal chars.
-
Dim subtemp As String = stripIllegal(subjectCode.Text.Trim)
-
Dim grptemp As String = stripIllegal(groupNum.Text.Trim)
-
-
If subtemp = "" Or grptemp = "" Then
-
showError.Text = "Please enter a valid subject code and group number."
-
Else
-
-
Dim con As New SqlClient.SqlConnection(ConnectionString)
-
Dim sqlAdapter As New SqlClient.SqlDataAdapter("getClassList", con)
-
-
sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
-
sqlAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Subject", SqlDbType.VarChar, 3))
-
sqlAdapter.SelectCommand.Parameters("@Subject").Value = subtemp
-
-
sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
-
sqlAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Group", SqlDbType.Int))
-
sqlAdapter.SelectCommand.Parameters("@Group").Value = grptemp
-
-
ds = New DataSet()
-
-
Try
-
con.Open()
-
sqlAdapter.Fill(ds, "classlist")
-
Catch ex As Exception
-
showError.Text += "<p style=""color:#f00;"">DEBUG -><br><br>" & ex.ToString & "</p>"
-
Finally
-
con.Close()
-
sqlAdapter = Nothing
-
sb = Nothing
-
End Try
-
-
If ds.Tables("classlist") IsNot Nothing Then
-
-
If ds.Tables("classlist").Rows.Count > 0 Then
-
-
Try
-
-
pGrid.ShowHeader = False
-
pGrid.ShowFooter = False
-
-
' Add a template for each row of the data set.
-
For y As Integer = 0 To ds.Tables("classlist").Rows.Count - 1
-
-
Dim val As String = ds.Tables("classlist").Rows(y).Item("Surname").ToString ' DEBUG
-
debug.Text += "Adding template field " & y + 1 & " - " & val & "<br>" ' DEBUG
-
Dim tf As New TemplateField()
-
tf.ItemTemplate = New MyTemplate()
-
pGrid.Columns.Add(tf)
-
-
Next
-
-
pGrid.DataSource = ds.Tables("classlist")
-
pGrid.DataBind()
-
pGrid.Visible = True
-
-
Catch ex As Exception
-
showError.Text = "<p style=""color:#f00;"">DEBUG -> <br><br>" & ex.ToString & "</p>"
-
End Try
-
-
'photoGrid.DataSource = ds.Tables("classlist")
-
'photoGrid.DataBind()
-
showError.Text = ""
-
-
Else
-
-
showError.Text = "<p style=""color:#f00;"">No results found for this subject and group combination.</p>"
-
-
End If
-
-
End If
-
-
End If
-
-
End Sub
-
-
End Class
-
-
Public Class MyTemplate
-
Implements ITemplate
-
-
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
-
-
Dim ph As New PlaceHolder()
-
Dim photo As New System.Web.UI.WebControls.Image
-
Dim name As New Label()
-
Dim number As New Label()
-
-
photo.ID = "studphoto"
-
photo.Height = 100
-
photo.Width = 75
-
name.ID = "studname"
-
number.ID = "studnum"
-
-
ph.Controls.Add(New LiteralControl("<div>"))
-
ph.Controls.Add(photo)
-
ph.Controls.Add(New LiteralControl("<br />"))
-
ph.Controls.Add(name)
-
ph.Controls.Add(New LiteralControl("<br />"))
-
ph.Controls.Add(number)
-
ph.Controls.Add(New LiteralControl("</div>"))
-
AddHandler ph.DataBinding, New EventHandler(AddressOf Item_DataBinding)
-
-
container.Controls.Add(ph)
-
-
End Sub
-
-
Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
-
-
Dim ph As PlaceHolder = CType(sender, PlaceHolder)
-
-
'Dim ri As RepeaterItem = CType(ph.NamingContainer, RepeaterItem)
-
-
Dim gvi As DataGridItem = CType(ph.NamingContainer, DataGridItem)
-
-
'Dim nameVal2 As String = Convert.ToString(DataBinder.Eval(ri.DataItem, "Surname"))
-
'Dim numVal2 As Integer = Convert.ToInt32(DataBinder.Eval(ri.DataItem, "Student Code Number"))
-
-
Dim nameVal As String = Convert.ToString(DataBinder.Eval(gvi.DataItem, "Surname"))
-
Dim numVal As Integer = Convert.ToInt32(DataBinder.Eval(gvi.DataItem, "Student Code Number"))
-
Dim pathToPhoto As String = "/Student Photos/_w/" & numVal2 & "_JPG.jpg"
-
-
Dim currentSite As SPWeb = SPContext.Current.Web
-
' Now test if the photo exists in the current sharepoint site.
-
Try
-
Dim fi As SPFile = currentSite.GetFile(pathToPhoto)
-
-
If fi.Exists Then
-
' do nothing
-
Else
-
pathToPhoto = "/Student Photos/_w/face2_JPG.jpg"
-
End If
-
Catch ex As Exception
-
-
End Try
-
-
CType(ph.FindControl("studphoto"), System.Web.UI.WebControls.Image).ImageUrl = pathToPhoto
-
CType(ph.FindControl("studname"), Label).Text = nameVal2.ToString()
-
CType(ph.FindControl("studnum"), Label).Text = numVal2.ToString()
-
-
End Sub
-
-
End Class
-
-
End Namespace
-
-
Apologies, lines 347,348 should read, -
CType(ph.FindControl("studname"), Label).Text = nameVal.ToString()
-
CType(ph.FindControl("studnum"), Label).Text = numVal.ToString()
-
Somehow I didn't realize that you were working with a web-part. I've never worked with them before but I'll see if I can still help you get through this.
Give me a bit to figure out what you have posted here.
-Frinny
Much appreciated Frinny, cheers
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Oleg |
last post by:
I am using asp:repeater control. I would like to create a conditional row <tr> when the data in
DataBinder.Eval(Container.DataItem, "Activity") returns '1'.
I need something like this: <%if(...
|
by: ratnakarp |
last post by:
Hi,
I have a search text box. The user enters the value in the text box and
click on enter button. In code behind on button click i'm writing the
code to get the values from the database and...
|
by: Steve Lutz |
last post by:
I have written a web service to provide some back-end functionality to a Web
Site. The web service returns lists of items.
If I use the webservice via a browser, it works fine and returns the...
|
by: Mike Cain |
last post by:
Hi,
The Repeater control seems like exactly what I want to output rows of data
from my database. However I need to do some manipulation to the data prior
to it being output and I'm not...
|
by: erin.sebastian |
last post by:
Hello All,
I have 2 tables in my sql database, a table that holds different years
(ex, 2003,2004,2005,2006) and then another table that holds newsletter
information and the year is a foreign key...
|
by: Eugene Anthony |
last post by:
The problem with my coding is that despite removing the records stored
in the array list, the rptPages repeater
control is still visible. The rptPages repeater control displayes the
navigation...
|
by: Jeff |
last post by:
Hey
ASP.NET 2.0
GridView have AllowPaging & PageSize for the letting the GridView span
multiple pages.
But is the same allowed on a Repeater control? I didn't see any properties
like...
|
by: Doogie |
last post by:
Hi, I have been trying to get a checkbox added to a repeater control
of mine and then try to access events of the repeater control when a
user clicks the checkbox. At first, since the control is...
|
by: Emma Middlebrook |
last post by:
Hi there,
I've been trying to implement a repeater control in an ASP.NET 2 page
but I can't seem to get the layout exactly how I want and I'm not sure
if it's something that I am doing wrong or...
|
by: =?Utf-8?B?Um9iZXJ0IFNtaXRo?= |
last post by:
Hi,
I wish to create a repeater control in c#.net that allows for the
selection of rows, any idea how this can be done.
Regards
Robert
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |