Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2745
  • Last Modified:

ASP.NET: If Statement Code Block in ASPX Page (not in codebehind)

All,

I have a ASP:Table control that I wish to hide rows in if certain conditions are met. The trick is that it's in a repeater so I have to do it has the repeater is binding the data to the table, written out in english it'd be:

If the value of DataBinder.Eval(Container.DataItem, "TestParamDesc5") is NOT string.empty then write this row of HTML to the page..  programmatically I was thinking like:


<%# If Not DataBinder.Eval(Container.DataItem, "TestParamDesc5") = string.empty %>
                                <asp:TableRow ID="TableRow6" Runat="server">
                                    <asp:TableCell ID="TableCell11" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc5") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell12" Runat="server">  
                                    <asp:TextBox ID="txtTestParam5" Runat="server" width="120px" text='<%# DataBinder.Eval(Container.DataItem, "TestParam5") %>'></asp:TextBox>                                  
                                    </asp:TableCell>                              
                                </asp:TableRow>
<%# end if %>

however, I don't know the right syntax.

OR, is there a way to do this as the repeater is binding? I could use an OnItemDatabound event, but how do I pull the column name? like: If current column being bound is named Blah, then if blah is string.empty then table row(currentindex).visible = false ..

help :)
0
Mitchell_
Asked:
Mitchell_
  • 9
  • 8
1 Solution
 
raterusCommented:
This is not ASP anymore, you can't write "if" statements in the aspx page, that is not asp.net.  You'll have to do this in itemdatabound, and make the "TableRow6" visible=false.
0
 
Type25Commented:
In item_databound event:

if (e.Item.Cells[1].Text == "")
{
       e.Item.Visible = false;
}

[vb]

If e.Item.Cells(1).Text = "" Then
       e.Item.Visible = False
End If
0
 
Mitchell_Author Commented:
Neither of these solutons will work.. you can't reference a table object withina  repeater by name.. and Type's solution refers to a datagrid.

Thanks though..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
raterusCommented:
never say "can't"...Type25, if you want to handle this one be my guest, I've had bout enough of these types of questions...
0
 
Mitchell_Author Commented:
"types of questions?" there's no need to take that tone.

Sorry.. I don't mean "can't".. you're right, most things can be done somehow.. but my response wasn't rude, it was just a satement.

and I did say thanks :P

0
 
raterusCommented:
Oh I wasn't meaning to be rude, sorry if I came off that way.  But like both of us said, you have going to have to do this in ItemDataBound (That's where I got "types of questions"), and this can be rather confusing if you've never done it before.
0
 
raterusCommented:
show me your whole repeater source and I may be able to show you just a little of what you need...
0
 
Mitchell_Author Commented:
Heh.. it's so hard to communicate over the internet sometimes..

I appreciate your help and would welcome a chance to work on this with you.

I have used ItemDataBound many many times in my apps so I'm not foreign to it, I just can't see how I can reference a tablerow(x) within the repeater there..

I need to see if the database value is an empty string for that particular table row and if so, mark it visible=false.

0
 
Mitchell_Author Commented:
alright:

<asp:Repeater ID="rptTestB" Runat="server" OnItemDataBound="rptTestB_ItemDataBound">
                    <ItemTemplate>
                        <td valign="top">
                        <asp:Table ID="tblParamsB" Runat="server" CellSpacing="4" Width="100%">
                                <asp:TableRow ID="TableRow1" Runat="server">
                                    <asp:TableCell ID="TableCell1" text="Test" Runat="server" Font-Bold="True" HorizontalAlign="Right">                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell2" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestDesc") %>'>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="TableRow2" Runat="server">
                                    <asp:TableCell ID="TableCell3" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc1") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell4" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestParam1") %>'>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="TableRow3" Runat="server">
                                    <asp:TableCell ID="TableCell5" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc2") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell6" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestParam2") %>'>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="TableRow4" Runat="server">
                                    <asp:TableCell ID="TableCell7" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc3") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell8" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestParam3") %>'>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="TableRow5" Runat="server">
                                    <asp:TableCell ID="TableCell9" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc4") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell10" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestParam4") %>'>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="TableRow6" Runat="server">
                                    <asp:TableCell ID="TableCell11" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc5") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell12" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestParam5") %>'>  
                                </asp:TableCell>                              
                                </asp:TableRow>                              
                            </asp:Table>
                        </td>
                    </ItemTemplate>
                    <SeparatorTemplate>
                        </tr>
                        <tr>
                    </SeparatorTemplate>
                </asp:Repeater>
0
 
Mitchell_Author Commented:
So, for example, I want to check:

<%# DataBinder.Eval(Container.DataItem, "TestParamDesc2") %>

and if it's string.empty then I want:

<asp:TableRow ID="TableRow3" Runat="server">
                                    <asp:TableCell ID="TableCell5" Runat="server" Font-Bold="True" HorizontalAlign="Right" text='<%# DataBinder.Eval(Container.DataItem, "TestParamDesc2") %>'>                                    
                                    </asp:TableCell>
                                    <asp:TableCell ID="TableCell6" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestParam2") %>'>
                                    </asp:TableCell>
                                </asp:TableRow>


to be invisible.

the repeater works fine otherwise as it is right now.
0
 
raterusCommented:
This is the basics...Put this in your script block...

    Private Sub rptTestB_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Then
            If e.Item.DataItem("TestParamDesc2") = String.Empty Then
                Dim tr As TableRow = e.Item.FindControl("TableRow3")
                tr.Visible = False
            End If
        End If
    End Sub
0
 
Mitchell_Author Commented:
alright, that's pretty close..

the check passes as long as there's a value in the field, but if the value is an empty string in the database, something is tripping it up..

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

If e.Item.DataItem("TestParamDesc3") = String.Empty Then
Line 371:                Dim tr As TableRow = e.Item.FindControl("TableRow4")
Line 372:                tr.Visible = False
Line 373:            End If

but the one before it:

If e.Item.DataItem("TestParamDesc2") = String.Empty Then
   Dim tr As TableRow = e.Item.FindControl("TableRow3")
  tr.Visible = False
 End If

works fine because there's a value in the database.

is there some typecasting that needs to be done on the data item to make it into a string?

I tried doing a cstr on it but it didn't like that too much, also tried a ".tostring" at the end of the data item check but that didn't fly either.

0
 
raterusCommented:
what line is the error on?  I'm thinking it's being caused because it can't find the tablerow, not what your datasource has in it...
0
 
Mitchell_Author Commented:
372 - the tr.visible line


I checked the aspx page and that table row is indeed there..
0
 
raterusCommented:
uh huh..did you keep the e.item.itemtype check in there?  That is very important, if you start trying to find these controls in the header/footer/separator it is going to break real quick :-)
0
 
Mitchell_Author Commented:
heh.

yes.. here's the entire sub, and  yeah  need the first one because it splits my repeater into 3 columns :)

Private Sub rptTestB_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Separator Then
            If Not ((System.Threading.Interlocked.Increment(counterB) Mod columnCount) = 0) Then
                e.Item.Visible = False
            End If
        End If

        If e.Item.ItemType = ListItemType.Item Then
            If e.Item.DataItem("TestParamDesc1").ToString = String.Empty Then
                Dim tr As TableRow = e.Item.FindControl("TableRow2")
                tr.Visible = False
            End If
        End If

        If e.Item.ItemType = ListItemType.Item Then
            If e.Item.DataItem("TestParamDesc2").ToString = String.Empty Then
                Dim tr As TableRow = e.Item.FindControl("TableRow3")
                tr.Visible = False
            End If
        End If

        If e.Item.ItemType = ListItemType.Item Then
            If e.Item.DataItem("TestParamDesc3").ToString = String.Empty Then
                Dim tr As TableRow = e.Item.FindControl("TableRow4")
                tr.Visible = False
            End If
        End If

        If e.Item.ItemType = ListItemType.Item Then
            If e.Item.DataItem("TestParamDesc4") = String.Empty Then
                Dim tr As TableRow = e.Item.FindControl("TableRow5")
                tr.Visible = False
            End If
        End If

        If e.Item.ItemType = ListItemType.Item Then
            If e.Item.DataItem("TestParamDesc5") = String.Empty Then
                Dim tr As TableRow = e.Item.FindControl("TableRow6")
                tr.Visible = False
            End If
        End If
    End Sub
0
 
raterusCommented:
What the world are you doing with the threading class...(sorry I have to ask..curiosity).  Not to make it more difficult on you, but have you ever thought to use a UserControl for each "record", it makes it much easier to read/manage in code, also, have you ever looked at the DataList control, it's in the middle of a repeater and a datagrid, it makes it much easier to create html tables than the repeater, especially using the "RepeatColumns" functionality...anyway, did you get it to work, or are you still having problems?, you said "heh" like you had figured something out...
0
 
Mitchell_Author Commented:
for some reason the only way I could get the incrementing thing going to split my columns up was to access that class.

I haven't really lookat any of the other controls you've mentioned, but I will now :)

Though, if you have an idea how to get this problem working as it is right now, that'd be cool.. but if you're at a standstill on it, I will just work with what I have until I have a chance to explore those other controls :)

(so no, my "heh", wasn't a lightbulb coming on ;) )

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now