Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UserControl之外搜索控件_C#_Asp.net_Controls - Fatal编程技术网

C# 在UserControl之外搜索控件

C# 在UserControl之外搜索控件,c#,asp.net,controls,C#,Asp.net,Controls,我在用户控件中有按钮。我想在用户控件内的按钮的单击事件发生时,找到ID为ShowPostsListView的asp:ListView。我该怎么做 <%--add post user control--%> <uc1:AddPostUserControl runat="server" id="AddPostUserControl" /> <%--show posts--%&g

我在
用户控件中有
按钮
。我想在
用户控件内的
按钮的单击事件发生时,找到ID为ShowPostsListView的
asp:ListView
。我该怎么做

              <%--add post user control--%>
                <uc1:AddPostUserControl runat="server" id="AddPostUserControl" />

                <%--show posts--%>
                <asp:ListView ID="ShowPostsListView" runat="server">
                    <ItemTemplate>
                        <div class="thisPost">
                            <div class="thisPostAuthor">
                                <%# this.GetCurrentUser(Eval("User")) %>
                            </div>
                            <div class="thisPostHeader">
                                <h3>
                                    <%# Eval("Title") %>
                                </h3>
                                <span class="postDateTime">
                                    <%# Eval("PostDate") %>
                                </span>
                            </div>
                            <div class="thisPostContent">
                                <p>
                                    <%# Eval("PostContent") %>
                                </p>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:ListView>



为什么要这样?
UserControl
应该是可重用的,而不是与包含它的页面硬链接。相反,您应该在UserControl中提供一个自定义事件(在单击按钮时引发),您可以在此页面中处理该事件。这里有对
列表视图的引用。页面是控制器而不是用户控件。
Page page = HttpContext.Current.Handler as Page
ListView lsv = page.FindControl("ShowPostsListView");