?
Solved

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

Posted on 2005-03-15
18
Medium Priority
?
2,703 Views
Last Modified: 2008-02-01
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
Comment
Question by:Mitchell_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 13544663
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
 
LVL 9

Expert Comment

by:Type25
ID: 13545172
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
 

Author Comment

by:Mitchell_
ID: 13546310
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:raterus
ID: 13546355
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
 

Author Comment

by:Mitchell_
ID: 13546373
"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
 
LVL 33

Expert Comment

by:raterus
ID: 13546427
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
 
LVL 33

Expert Comment

by:raterus
ID: 13546444
show me your whole repeater source and I may be able to show you just a little of what you need...
0
 

Author Comment

by:Mitchell_
ID: 13546453
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
 

Author Comment

by:Mitchell_
ID: 13546463
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
 

Author Comment

by:Mitchell_
ID: 13546477
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
 
LVL 33

Expert Comment

by:raterus
ID: 13546516
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
 

Author Comment

by:Mitchell_
ID: 13546726
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
 
LVL 33

Expert Comment

by:raterus
ID: 13546758
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
 

Author Comment

by:Mitchell_
ID: 13546783
372 - the tr.visible line


I checked the aspx page and that table row is indeed there..
0
 
LVL 33

Expert Comment

by:raterus
ID: 13546826
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
 

Author Comment

by:Mitchell_
ID: 13546878
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
 
LVL 33

Accepted Solution

by:
raterus earned 2000 total points
ID: 13546956
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
 

Author Comment

by:Mitchell_
ID: 13547037
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question