Results 1 to 6 of 6

Thread: [RESOLVED] [2008] Better way to write this code?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Resolved [RESOLVED] [2008] Better way to write this code?

    Hi!

    This piece of code looks really bad. Basically I want to remove the option to press a button and instead show a message, if the value in the datasource is <> -1

    This code looks bad, can someone find a better and nicer way to write it?

    Code:
     Dim book As HiLibrary.BooksRow
    
            If (e.Row.DataItem IsNot Nothing) Then
                book = CType(CType(e.Row.DataItem, System.Data.DataRowView).Row, HiLibrary.BooksRow)
    
                ' Om boken är utlånad till denna användaren, då kan han inte låna den igen
                If (book.IS_LOANED <> -1) Then
                    ' Dölj knappen
                    CType(e.Row.FindControl("Button1"), Button).Visible = False
                    Dim alert As New Literal
                    alert.ID = "litAlert"
                    alert.Text = "Book is already loaned"
                    e.Row.Cells(5).Controls.Add(alert)
                End If
            End If

    kind regards
    Henrik

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Better way to write this code?

    vb Code:
    1. Dim book As HiLibrary.BooksRow
    2.    If e.Row.RowType = DataControlRowType.DataRow Then
    3.       book = CType(e.Row.DataItem, HiLibrary.BooksRow)
    4.             If (book.IS_LOANED > -1) Then
    5.                 ' Dölj knappen
    6.                 CType(e.Row.FindControl("Button1"), Button).Visible = False
    7.                 Dim alert As New Label
    8.                 alert.ID = "AlertMessage"
    9.                 alert.Text = "Book has already been loaned"
    10.                 e.Row.Cells(5).Controls.Add(alert)
    11.             End If
    12. '.............

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: [2008] Better way to write this code?

    Thanks.

    So it is ok to just hide the button, I was thinking about removing it completly from the tree?

    Also, am I using the correct pattern for adding a new control dynamically. I just set the ID and the text. Is that enough?

    Also, should I use the indexers when referring to a cell? It looks very "ASP.NET 1.1" if you know what I mean.

    kind regards
    Henrik

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Better way to write this code?

    There isn't a point to removing it because it's "there" already. By hiding it, you're ensuring that it's not being sent to the browser.


    Also, am I using the correct pattern for adding a new control dynamically. I just set the ID and the text. Is that enough?
    Yes it's enough. As an alternative, it seems like there are only a few options for each cell. You could actually have the 'not available' message present in the cell but make it invisible in the databound event. This means that you perform your check and then based upon whether or not it's available you make either the button visible or make the message visible. This way you can avoid the "very asp.net 1.1" feel, but to be honest this is how it'd be done in ASP.NET 1.1 as well.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: [2008] Better way to write this code?

    Quote Originally Posted by mendhak
    This way you can avoid the "very asp.net 1.1" feel, but to be honest this is how it'd be done in ASP.NET 1.1 as well.

    You mean ASP.NET 2.0?

    Kind regards
    Henrik

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Better way to write this code?

    I mean ASP.NET 2.0, yes. Across all ASP.NET versions, that's how you'd do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width