Forum Moderators: open

Message Too Old, No Replies

if else statements

ok - here's another question from me

         

hal12b

4:11 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



I am querying my database on the code behind page (aspx.vb) and displaying information on the aspx page like this, for example

<%#Utils.CheckForNull_Text2(Container.DataItem("USER"))%>

The above displays a name such as --> Bob
All is good here

But I want to add some logic to the page and in my head I know what I want to do with Classic ASP experience, but being new to ASP.net, some things are not coming along easily.

So on the .aspx page, I want to do something like

<%if #Utils.CheckForNull_Text2(Container.DataItem("USER")) = "Bob" then%>

lbl.text = "blah blah"

<%else%>
'nothing
<%end if%>

I am not sure how to have the proper syntax here --> <%if #Utils.CheckForNull_Text2(Container.DataItem("USER")) = "Bob"


Please help!

Thanks

marcel

4:17 pm on Jun 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should put all of it into the code behind:

if Utils.CheckForNull_Text2(Container.DataItem("USER")) = "Bob" then
lbl.text = "blah blah"
else
'nothing
end if

hal12b

5:19 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



hmm
Thanks!

hal12b

6:40 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'Container' is not declared.

Source Error:

Line 85: Try
Line 86:
Line 87: If Utils.CheckForNull_Text2(Container.DataItem("ROLE_LEVEL1")) = "YES" Then
Line 88: strQuery = "SELECT * FROM tblCalendar where ROLE_LEVEL1 = 'YES' AND DATE_DISPLAY > '" & strcurrmonth & " /01/" & strcurrentyear & "' AND DATE_DISPLAY < '" & strcurrmonth & "/" & strdaysinmonth & "/" & strcurrentyear & "' ORDER BY [DATE_DISPLAY] ASC"
Line 89:

hal12b

7:01 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



Sorry - So my question is how do I declare "Container"? What did I do wrong?

marcel

11:23 am on Jun 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I didn't look at your code properly, I see that you are binding against a List of Data, is that correct?

If you're using something like a DataList, Repeater or Datagrid you can use the RowDataBound event, here is an example for a GridView Control

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
' Check Value
If DataBinder.Eval(e.Row.DataItem, "USER") = "Bob" Then
Dim lbl As Label = e.Row.FindControl("lbl")
If Not lbl Is Nothing Then
lbl.Text = "blah blah"
End If
End If
End If
End Sub

hal12b

7:56 pm on Jun 22, 2010 (gmt 0)

10+ Year Member



I am getting errors. Do you have working code for the code behind page and aspx page I can examine and sub in my info?

hal12b

8:13 pm on Jun 22, 2010 (gmt 0)

10+ Year Member



OK, the code above seems to be OK for a Gridview, but what about a datalist? This part gets an error -->

Handles GridView1.RowDataBound

Particularly the "RowDataBound" word.

marcel

8:24 pm on Jun 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, the code above seems to be OK for a Gridview, but what about a datalist? This part gets an error -->

Handles GridView1.RowDataBound

Particularly the "RowDataBound" word.

Thats because a DataList control has a slightly different event, ItemDataBound, and also your DataList most likely does not have the Id 'GridView1'.

Try playing around with the ItemDataBound Event, it works a little like RowDataBound, so you will have access to the underlying Data.