如何在子控件中查找控件

时间:2014-06-29 19:57:04

标签: webforms

<asp:ListView ID="lvSharingList" runat="server" OnItemDataBound="lvSharingList_ItemDataBound">
        <LayoutTemplate>
            <table id="tblList" runat="server" style="width: 100%;">
                <tr runat="server" id="itemPlaceHolder" style="border: 1px solid rgb(208, 208, 208);"></tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr runat="server" id="tableRow" class="trShareItem" style="border: 1px solid rgb(208, 208, 208);">
                <td runat="server" id="tableCell">
                    <div style="width: 10%; float: left; padding: 5px 5px; margin: 5px 5px; border: 1px solid rgb(208, 208, 208);">
                        <asp:Label Visible="false" ID="lblGroupId" Text='<%# Eval("GroupId") %>' runat="server"></asp:Label>
                        <asp:Label Visible="false" ID="lblShareId" Text='<%# Eval("ShareId") %>' runat="server"></asp:Label>
                        <asp:Image ID="imageForFriend" runat="server" CssClass="fromUsername" AlternateText='<%# Eval("Username") %>' />
                        <br />

                        <a href="#" id="aUsername2" runat="server" title='<%# Eval("Username") %>'></a>
                    </div>
                    <div style="width: 45%; float: left; padding: 5px 5px; margin: 5px 5px; border: 1px solid rgb(208, 208, 208);">
                        <asp:Label ID="lblPost" Text='<%# Eval("Post")%>' runat="server" CssClass="sharedPost"></asp:Label><br />
                        <abbr id="abbrId" class="timeago" title='<%# DataBinder.Eval(Container.DataItem, "PostedDate", "{0:M/d/yyyy hh:mm:ss tt}") %>' runat="server"></abbr>
                        <asp:Label runat="server" ID="lblImgFlag" Text='<%# Eval("imgFlag") %>' Visible="false"></asp:Label>
                        <asp:Image ID="imagePhoto" runat="server" CssClass="sharePhotoFile" />
                        <asp:Literal ID="literal" runat="server" Text='<%# Eval("video") %>'></asp:Literal>
                    </div>
                    <div style="width: 38%; float: left; height: 100%;">
                        <div>
                            <fieldset>
                                <legend>Comments</legend>
                                <asp:UpdatePanel ID="UpdatePanelToResetComment" runat="server">
                                    <ContentTemplate>
                                        <a href="#" runat="server" id="aUsernameForComment"></a>
                                        <asp:TextBox ID="txtShareComment" runat="server" CssClass="txtShareCommentClass"></asp:TextBox>
                                        <asp:Button ID="btnComment" UseSubmitBehavior="false" runat="server" Text="Comment" CssClass="btn btn-small btn-primary" OnClick="btnComment_Click" />
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                    <ContentTemplate>
                                        <asp:ListView ID="listViewShareComments" runat="server">
                                            <LayoutTemplate>
                                                <ul id="itemPlaceHolder" runat="server">
                                                </ul>
                                            </LayoutTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="lblUsernameID" runat="server" Text='<%# Eval("Username") %>' CssClass="txtShareCommentClass"></asp:Label>&nbsp;:&nbsp;
                                                <asp:Label ID="txtShareCommentID" runat="server" Text='<%# Eval("Comment") %>' CssClass="txtShareCommentClass"></asp:Label>
                                            </ItemTemplate>
                                            <ItemSeparatorTemplate>
                                                <br />
                                            </ItemSeparatorTemplate>
                                        </asp:ListView>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="btnComment" EventName="Click" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </fieldset>
                        </div>
                    </div>
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

我有上面的例子,我想知道在子控件中定位控件的最佳方法。在上面的例子中,我有一个ListView控件,它有一个tableRow,后跟tableCell。我在tableCell中有一个listview。为什么我无法直接从页面控件找到子列表视图?为什么我必须找到每个runat = server控件然后找到子控件?

以下方式运行正常!我想知道有没有更好的方法来找到子列表视图控件?

HtmlTableCell tCell = (HtmlTableCell)lblShaId.Parent;
                UpdatePanel updatePanel1 = (UpdatePanel)tCell.FindControl("UpdatePanel1");                
                ListView listViewShareComments1 = (ListView)updatePanel1.FindControl("listViewShareComments");
                listViewShareComments1.DataSource = userMethods.GetAdminGroupShareComments(Convert.ToInt32(lblShaId.Text));
                listViewShareComments1.DataBind(); 

1 个答案:

答案 0 :(得分:0)

这就是在ASP.Net中实现FindControl的方式 - c.f. Better way to find control in ASP.NET更详细地讨论了以这种方式实现它的一些可能原因以及如何以递归方式搜索控件的一些技术。