Search code examples
asp.netvbalistviewtextitemtemplate

ASP.Net Passing a Variable from VBA on HTML Listview Item template when loading a web page


I have the below code on my aspx page, where a listview is being formed on HTML

´´´

 <asp:ListView runat="server"
                ID="VistaVehiculos"
                DataSourceID="ADSParticipantes"
                DataKeyNames="IDInscrito" ItemPlaceholderID="itemPlaceHolder"
                OnItemDataBound="VistaVehiculos_ItemDataBound">
                <ItemTemplate>
                    <td>
                        <asp:Label ID="lNum" runat="server" Height="80px" Width="83px" BackColor="Red"
                            Style="text-align:center; margin-left:5px; font-size:40px; padding-top:3px; font-family:'Doppio One', sans-serif; font-style:italic"
                            Text='<%# Eval("Num")%>'/>                          
                        <asp:Label ID="lTS" runat="server" Height="80px" BorderStyle="None"
                            Style="font-size:11px; display:block; margin-top:-80px; margin-left:8px"
                            text='<%# If(Eval("SEO1") Is DBNull.Value, "", "Correct"))%>'/>  
                    </td>
                </ItemTemplate>
                <GroupTemplate>
                    <tr>
                        <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                    </tr>
                </GroupTemplate>
                <GroupSeparatorTemplate>
                    <tr id="Tr1" runat="server">
                        <td style="height:15px"></td>
                    </tr>
                </GroupSeparatorTemplate>
                <LayoutTemplate>
                    <table border="0">
                        <asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
                    </table>
                </LayoutTemplate>
            </asp:ListView>
´´´

In label id=lTS, I evaluate a data field "SEO1"

What I need to do is to set such data field with different values depending on different requirements, to use this page as a template, using load vba procedure.

This case refers to Safety Engine O1 - SEO1, if I need to change by Safety Engine O2, I would evaluate SEO2 data field.

I cannot find control to change text value.

Any help on this?


Solution

  • Ok, first up. Your tags VBA are incorrect VBA is Visual Basic for Applications (that the code system used in Word, Excel, and ms-access.

    you are using vb.net - you should change your tags.

    As for messing around with that expression? You don't mention if the if() now works?

    However, beyond ANY simple Eval() expression, I find it becomes worse then a trip to the dentist to try and burn up a pot of coffee to inject/use/have complex expressions. They are also rather painfull to extend and write and change.

    So, I suggest you consider setting the value(s), and writing your code logic in codebehind.

    so, I would change this control to this:

       <asp:Label ID="lTS" runat="server" Height="80px" BorderStyle="None"
          Style="font-size:11px; display:block; margin-top:-80px; margin-left:8px"
          text = "" />
    

    Note how it has nothing now. Because we want to apply "more" complex logic to that control, then moving our code to the item data bound event is the place to be!

    So, we now will have this:

    Protected Sub VistaVehiculos_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles VistaVehiculos.ItemDataBound
    
        If e.Item.ItemType = ListViewItemType.DataItem Then
    
            ' get the control we are to format
    
            Dim SEOlbl As Label = e.Item.FindControl("ITS")
    
            Dim dRow As DataRowView = e.Item.DataItem
            ' NOTE IN ABOVE - NOT list view item!!! - but DataRowItem!!!!!!!!!
    
    
            Dim strResult As String = ""
            If dRow("SafteyCase") = "Safety Engine O1" Then
                strResult = dRow("SE1").ToString
            Else
                strResult = dRow("SE2").ToString
            End If
            If strResult = "" Then
                strResult = "NO VALUE"
            End If
    
            SEOlbl.Text = strResult
    
        End If
    
    End Sub
    

    note how we are free to get any data column here INCLUDING ones not in the lv layout, but of course they MUST be in the data source.

    So, you can conditional test anything you want, and then SHOVE into the label the results of that code - that code can be anything you want.

    so, remove your expression from the label, and use code in the data bound event of lv.

    Needless to say, you can also use this event to set color of the row, or even text boxes, so for conditonal formatting tricks, you can use the above.