Not sure what I'm missing...

Discussion in 'ASP .Net' started by Rob Meade, May 24, 2004.

  1. Rob Meade

    Rob Meade Guest

    Hi all,

    I'm trying to dynamically create the navigation for my web page - I have
    achieved this at work and whilst trying to reproduce the code this evening
    at home I'm obviously missing something, I just cant see what...

    Here's the error:

    Class 'System.Web.UI.WebControls.TableRow' cannot be indexed because it has
    no default property.

    Here's the code for my left navigation web user control.


    Imports System.IO

    Public Class LeftBorder

    Inherits System.Web.UI.UserControl

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    ' Declare variables
    Dim strCurrentPage As String
    Dim tblNavigation As Table
    Dim addNavigation As TableRow

    tblNavigation = New Table
    addNavigation = New TableRow

    strCurrentPage = Path.GetFileName(Request.Path)

    ' Create navigation
    tblNavigation.Rows.Add(addNavigation(strCurrentPage, "Home",
    "Default.aspx"))

    End Sub

    End Class

    Public Class LeftNavigation

    Function addNavigation(ByVal CurrentPage As String, ByVal PageName As
    String, ByVal PageLink As String) As TableRow

    ' Declare variables
    Dim Row As TableRow
    Dim Cell As TableCell
    Dim Link As HyperLink

    Link = New HyperLink

    If CurrentPage.ToLower = PageName.ToLower Then
    Cell.Text = PageName
    ElseIf CurrentPage.ToLower <> PageName.ToLower Then
    ' Set the properties of our link
    Link.NavigateUrl = PageLink
    Link.CssClass = "normalText"
    Link.ToolTip = PageName
    Link.Target = "_top"
    Cell.Controls.Add(Link)
    End If

    Row.Cells.Add(Cell)


    Return (Row)

    End Function

    End Class



    I think I probably need to change the inherits lines around so that the
    second class has the Inherits System.Web.UI.UserControl
    and then I derived my main class from that using inherits leftNavigation
    etc...

    The line I get the error on is the first class where I am calling
    addNavigation...

    Any help would be appreciated - quite new to this so be gently :)

    Regards

    Rob
     
    Rob Meade, May 24, 2004
    #1
    1. Advertisements

  2. Rob Meade

    Raterus Guest

    I don't know what you are trying to do, but this is your problem

    Dim addNavigation As TableRow

    tblNavigation = New Table
    addNavigation = New TableRow

    strCurrentPage = Path.GetFileName(Request.Path)

    ' Create navigation
    tblNavigation.Rows.Add(addNavigation(strCurrentPage, "Home",
    "Default.aspx"))

    ...

    addNavigation is your tablerow object, so what are you trying to do with this??

    addNavigation(strCurrentPage, "Home", "Default.aspx")
     
    Raterus, May 24, 2004
    #2
    1. Advertisements

  3. Rob Meade

    Rob Meade Guest

    ...
    ' Create navigation

    Hi Raterus, thanks for the reply..

    I spotted the Dim'ing of the tblNavigation and removed it just now - now it
    references the table that I have in the design view (<asp:table
    id="tblNavigation" runat="server"></asp:table> etc)

    What I am trying to do is call the function, passing it the current page, a
    page name, and a page URL - if it turns out that the current page is the
    page url then it only writes a piece of text in the cell, if it isnt then
    it'll write a link to the page...

    I have managed to get away from any squiggly lines in VS, but still nothing
    is happening....

    Code as of now is (still not producing anything to the page though):

    Imports System.IO

    Public Class LeftBorder
    Inherits LeftNavigation

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    ' Declare variables
    Dim strCurrentPage As String

    strCurrentPage = Path.GetFileName(Request.Path)

    tblNavigation.Rows.Add(addNavigation(strCurrentPage, "Home",
    "Default.aspx"))

    End Sub

    End Class

    Public Class LeftNavigation
    Inherits System.Web.UI.UserControl
    Public Function addNavigation(ByVal CurrentPage As String, ByVal
    PageName As String, ByVal PageLink As String) As TableRow
    'addNavigation(strCurrentPage, "Home", "Default.aspx"
    ' Declare variables
    Dim Row As TableRow
    Dim Cell As TableCell
    Dim Link As HyperLink

    Row = New TableRow
    Cell = New TableCell

    If CurrentPage.ToLower = PageName.ToLower Then

    Cell.Text = PageName

    ElseIf CurrentPage.ToLower <> PageName.ToLower Then

    Link = New HyperLink
    ' Set the properties of our link
    Link.NavigateUrl = PageLink
    Link.CssClass = "normalText"
    Link.ToolTip = PageName
    Link.Target = "_top"

    Cell.Controls.Add(Link)

    End If

    Row.Cells.Add(Cell)

    Return (Row)

    End Function

    End Class
     
    Rob Meade, May 24, 2004
    #3
  4. Rob Meade

    Raterus Guest

    Are you trying to add this usercontrol dynamically? They are a bit more complicated to add than a regular server/custom control, because they have that added implementation in the .ascx file.

    Try this:

    Dim addNavigation As LeftNavigation = CType(LoadControl("../path/to/LeftNavigation.ascx"), LeftNavigation)
    addNavigation.addNavigation(strCurrentPage, "Home", "Default.aspx") 'these really should be properties of the usercontrol if you wanted to do it right...
    td.Controls.Add(addNavigation)
     
    Raterus, May 24, 2004
    #4
  5. Rob Meade

    Natty Gur Guest

    Hi,

    You want to access function in other class. You have two options:

    1) Create instance of the class and use it to access class function:

    Dim oLeft as LeftNavigation = new LeftNavigation()
    tblNavigation.Rows.Add(oLeft.addNavigation(....))

    You also need to set your addNavigation function as public.

    2) Make addNavigation shared function and access it by LeftNavigation
    class:

    tblNavigation.Rows.Add(LeftNavigation .addNavigation(....))
    . . .

    Public shared Function addNavigation(ByVal CurrentPage As String, ByVal
    PageName As
    String, ByVal PageLink As String) As TableRow

    The main cause for all this confusion is that the TableRow and the
    function got the same name. Try next time to use name convention. Using
    name conventions make your code more readable and reduce such problems.

    Natty Gur[MVP]

    blog : http://weblogs.asp.net/ngur
    Mobile: +972-(0)58-888377
     
    Natty Gur, May 25, 2004
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.