|
-
May 16th, 2008, 07:34 AM
#1
Thread Starter
Frenzied Member
[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
-
May 16th, 2008, 02:32 PM
#2
Re: [2008] Better way to write this code?
vb Code:
Dim book As HiLibrary.BooksRow If e.Row.RowType = DataControlRowType.DataRow Then book = CType(e.Row.DataItem, HiLibrary.BooksRow) If (book.IS_LOANED > -1) Then ' Dölj knappen CType(e.Row.FindControl("Button1"), Button).Visible = False Dim alert As New Label alert.ID = "AlertMessage" alert.Text = "Book has already been loaned" e.Row.Cells(5).Controls.Add(alert) End If '.............
-
May 19th, 2008, 03:08 AM
#3
Thread Starter
Frenzied Member
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
-
May 19th, 2008, 12:19 PM
#4
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.
-
May 19th, 2008, 03:32 PM
#5
Thread Starter
Frenzied Member
Re: [2008] Better way to write this code?
 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
-
May 20th, 2008, 01:25 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|