使用Jquery

时间:2015-05-21 16:11:01

标签: c# jquery asp.net listview listviewitem

我需要像这样的https://stackoverflow.com/a/7368719/1203098这样的解决方案,我需要选择的链接位于另一个ListView内的ListView中。

我不断得到的错误是" Object reference not set to an instance of an object"

<asp:ListView ID="lstViewSharedFolder" runat="server" OnItemDataBound="lstViewSharedFolder_ItemDataBound">
    <ItemTemplate>
        <tr class="alert-info">
            <td><strong>Folder ID</strong>:
                <%# Eval( "FolderID") %>
            </td>
            <td><strong>Folder Name</strong>:
                <%# Eval( "SharedFolderName") %>
            </td>
            <td><strong>Security Score</strong>:
                <%# Eval( "Score") %>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:ListView ID="lstViewUsers" runat="server" OnItemDataBound="lstViewUsers_ItemDataBound">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:HyperLink ID="lnkUsername" runat="server">
                                    <%# Eval( "UserName") %>
                                </asp:HyperLink>
                            </td>
                            <td>
                                <%# Eval( "ReadOnly") %>
                            </td>
                            <td>
                                <%# Eval( "Give") %>
                            </td>
                            <td>
                                <%# Eval( "CanAdminister") %>
                            </td>
                            <td>
                                <%# Eval( "GroupName") ?? "n/a" %>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:ListView>
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#<%= ((HyperLink)((ListView)lstViewSharedFolder.FindControl("lstViewUsers")).FindControl("lnkUsername")).ClientID %>').click(function() {
            var id = $(this).attr("id"); // Get the ID
            alert(id);
            console.log(id);
        });
    });
</script>

修改 我想我知道为什么。当绑定SharedFolder项时,我绑定了Users ListView控件,所以控件可能还没有存在。有没有更好的更有效的方法来做到这一点?我的目标是在点击链接时显示包含用户详细信息的弹出窗口。

var folderList = $('#<%= lstViewSharedFolder.ClientID %>');
<!-- This causes the error -->
var userList = $('#<%= ((ListView)lstViewSharedFolder.FindControl("lstViewUsers")).ClientID %>');

这是我的lstViewSharedFolder_ItemDataBound()。

protected void lstViewSharedFolder_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;

            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var folder = (SharedFolder)dataItem.DataItem;
                var users = folder.Users;

                var lstView = (ListView)e.Item.FindControl("lstViewUsers");
                lstView.DataSource = users;
                lstView.DataBind();
            }
        }

1 个答案:

答案 0 :(得分:0)

这里不需要复杂化。只需给LinkBut​​ton一个CssClass。

<asp:HyperLink ID="lnkUsername" runat="server" CssClass="my_link">
    <%# Eval( "UserName") %>
</asp:HyperLink>

并将其称为

var userList = $('.my_link');