Using asp.net 4.0. I have a page with a listview. I am trying to display a certain pre-defined Panel within the listview itemtemplate based on the current ItemBound value but not on the itemBound event....

For example, if the dataItem.Item("DataGapDesc") value is equal to "A", display Panel pnlPanelA, which will have 2 textboxes. If the dataItem.Item("DataGapDesc") value is equal to "B", display Panel pnlPanelB, which will have 3 textboxes and a checkbox, and so on.

Here's my current listview in the aspx:

     <asp:ListView ID="EmployeesGroupedByDataField" runat="server" DataSourceID="sqlDataGapsForSelectedemployees" OnItemBound="employeesGroupedByDataField_ItemDataBound">
                <LayoutTemplate>
                    <table id="Table1" runat="server" class="table1">
                        <tr id="Tr1" runat="server">
                            <td id="Td1" runat="server">
                                <table ID="itemPlaceholderContainer" runat="server" >
                                    <tr id="Tr2" runat="server">
                                        <th id="Th1" runat="server" style="text-align:left"><u>
                                            employee</u></th>
                                        <th id="Th2" runat="server" style="width:5%;text-align:center"><u>
                                            # Items Missing</u></th>
                                        <th id="Th3" runat="server"><u>
                                            employee DOB</u></th>
                                        <th id="Th4" runat="server"><u>
                                            Primary Physican</u></th>
                                        <th id="Th6" runat="server" style="width:10%;text-align:center;border-right: thin solid #000000"><u>
                                            Missing Data Item</u></th>
                                        <th id="Th5" runat="server" style="text-align: center;"><u>
                                            Last Known Visit/Service</u></th>                                
                                        <th id="Th7" runat="server" style="text-align: center;"><u>
                                            Data Entry</u></th>      
                                    </tr>
                                    <tr ID="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr id="Tr3" runat="server">
                            <td id="Td2" runat="server" style="">
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <%# AddGroupingRowIfemployeeHasChanged()%>               
                            <td style="text-align: right;border-right: thin solid #000000"><%# Eval("DataGapDesc")%>&nbsp</td>
                            <td style="text-align: left;">
                                <%#Eval("ServiceDate")%> 
                                &nbsp-&nbsp
                                <%# Eval("PlaceOfService")%>
                            </td>  
                            <td>
                                <%# DisplaySpecificPanel()%>                                   
                            </td>                                                                                                                                                        
                        </tr>
                </ItemTemplate>
            </asp:ListView>

Im calling the function DisplaySpecificPanel() and this is where I was "trying" to perform this behavior. That function in VB:

Public Function DisplaySpecificPanel() As String

    Dim currentEmployeeNameValue As String = Trim(Eval("Employee").ToString().Substring(0, (Eval("Employee").ToString.Length) - 10).ToString())

    If currentEmployeeNameValue = "DOE, JOHN" Then
        'Panel1.Visible = True
        Return String.Format("<asp:Button ID=""Button1"" runat=""server"" Text=""Button"" />")
    Else
        Return String.Format("<asp:TextBox ID=""TextBox1"" runat=""server""></asp:TextBox>")

    End If

End Function

Right now, I'm just trying this functionality out by adding either a textbox or button based on the value but longer term, I wish to add the panels... Well, my function is being called properly but the controls are not being placed on the rendered listview.

Any ideas? I'm I going about this correct? Many thanks...

有帮助吗?

解决方案

Found this from another source. Makes sense but I wasn't completely versed in the lifecycle or behavior of the aspx page.

You will not be able to render server side controls using string representation of the control. That approach works for plain HTML controls. But, server side controls need to go through a rendering mechanism which will not be invoked when you just set text property of a control. Better alternative would be keeping just one panel in the item template and adding controls dynamically in the itemdatabound event based on conditions. Then, You can check if some condition is true and then add textbox or a button accordingly. If there are too many controls, you can also use UserControls. But, dynamic controls will not be retained across page postbacks and you would need to add them back with the same ID. If you do not want to handle all this creation mechanism, you could just keep two independent panels and update the visibility based on some conditions.

within Listview :

<td>                                                                       
    <asp:Panel runat="server" ID="pnlOne" Visible='<%# CanShowFirstPanel()%>'>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </asp:Panel>
    <asp:Panel runat="server" ID="pnlTwo" Visible='<%# CanShowSecondPanel()%>'>
        <asp:TextBox runat="server" ID="TextBox1" />
    </asp:Panel>
</td>

Code-behind:

Public Function CanShowFirstPanel() As Boolean
       'condition to check 
       Return True
   End Function
   Public Function CanShowSecondPanel() As Boolean
       'condition to check 
       Return False
   End Function
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top