Search code examples
javascriptc#jqueryasp.netpopover

How can I display a Bootstrap popover with an html content from a GridView?


I have been at this for like two hours now trying various stuff. I can get the popover to display as just a basic title/content, but I haven't found a way to get it to work with HTML and Eval() as the content yet. This is what I'm working with:

JS:

$(function () {
    $("#btnViewRefDrDetails").popover({
        html: true,
        content: function () {
            return $("#popover-content-wrapper").html();
        },
        title: function () {
            return $("#popover-title").html();
        },
        container: 'body'
    });
})

And the HTML:

<asp:GridView ID="gridTOC" runat="server"
                <Columns>
                    <asp:TemplateField HeaderText="Ref Dr" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="15%">
                        <ItemTemplate>
                            <asp:LinkButton ID="btnViewRefDrDetails" runat="server" Text='<%#Eval("ReferringName") %>' data-toggle="popover"
                                title="Popover Title" ClientIDMode="Static" OnClientClick="return false" role="button" rel="popover">
                            </asp:LinkButton>
                            <%--this should be the template for the popover--%>
                            <div id="popover-content-wrapper" style="display: none;">
                                <table>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Provider Name:</b>&nbsp;</td>
                                        <td><%# Eval("ReferringName")%></td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Provider Specialty:</b>&nbsp;</td>
                                        <td><%# Eval("ProviderSpecialty")%> </td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Practice Name:</b>&nbsp;</td>
                                        <td><%# Eval("PracticeName")%></td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Practice Address:</b>&nbsp;</td>
                                        <td><%# Eval("PracticeAddress")%></td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Practice City:</b>&nbsp;</td>
                                        <td><%# Eval("PracticeCity")%> </td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Practice State:</b>&nbsp;</td>
                                        <td><%# Eval("PracticeState")%> </td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Practice ZIP:</b>&nbsp;</td>
                                        <td><%# Eval("PracticeZip")%> </td>
                                    </tr>
                                    <tr>
                                        <td style="white-space: nowrap;"><b>Practice Phone:</b>&nbsp;</td>
                                        <td><%# Eval("PracticePhone")%> </td>
                                    </tr>
                                </table>
                            </div>
                            <div id="popover-title" style="display: none">
                                <%# Eval("ReferringName")%>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

When I click the LinkButton, nothing happens at all. No errors, just nothing. When I just hard code the popover's title and content it works fine. What do?


Solution

  • I'm not sure if the problem is that there are more than one $("#popover-content-wrapper"), because if it is in a GridView, I think is normal to have more than one row.

    I have an example of how I implment this to show a larger <img> in a popover

    <asp:TemplateField>
        <ItemTemplate>
                <asp:Image ID="imgSignature" runat="server"  
                    data-img='<%# Eval("ImgBase64")  %>' 
                    data-toggle="popover"
                    ImageUrl='ViewLarger.jpg' />
        </ItemTemplate>
    </asp:TemplateField>
    
    $('img[data-toggle=popover]').popover({
        html: true,
        trigger: 'hover',
        content: function () {
            return '<img width="250px" src="' + $(this).data('img') + '" />';
        }
    });
    

    As you can see, I save the information I want to pass to the popover in a data-X attribute in the trigger element, and in the JS function just place it when appropiate. In your case should be

    <asp:TemplateField>
        <ItemTemplate>
                <asp:LinkButton ID="btnViewRefDrDetails" 
                                data-toggle="popover"
                                data-ReferringName='<%# Eval("ReferringName")%>' 
                                data-ProviderSpecialty='<%# Eval("ProviderSpecialty")%>' >
        </ItemTemplate>
    </asp:TemplateField>
    
    $('a[data-toggle=popover]').popover({
        html: true,
        trigger: 'hover',
        content: function () {
            return '<table><tr><td>Provider Name' + $(this).data('ReferringName') + '</td></tr> etc...';
        }
    });