如何找到在该ASP.NET页面编辑其他用户控件的事件内的ASP.NET页面的用户控件:不同的内容占位符? [英] How to find user control of an ASP.NET page inside of event of another user control on that ASP.NET page EDIT: different content placeholders?

查看:251
本文介绍了如何找到在该ASP.NET页面编辑其他用户控件的事件内的ASP.NET页面的用户控件:不同的内容占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2注册用户控件的ASP.NET页面。第一个只有一个在它的按钮。第二个是简单的文字和隐藏的默认。我要的是让第二个可见的第一个按钮被点击时(即在按钮的单击事件)。

I have a ASP.NET page with 2 user controls registered. The first one has only one button in it. The second one is simple text and hidden on default. What I want is to make the second one visible when the button in the first one is clicked (that is on button click event).

ASP.NET页面:

ASP.NET page:

<%@ Page Title="" Language="C#" CodeFile="test.aspx.cs" Inherits="test" %>
<%@ Register Src="~/UC_button.ascx" TagName="button" TagPrefix="UC" %>
<%@ Register Src="~/UC_text.ascx" TagName="text" TagPrefix="UC" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MyTestContent" Runat="Server">
    <UC:button ID="showbutton1" runat="server" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MyTestContent2" Runat="Server">
    <UC:text runat="server" Visible="false" ID="text1" />
</asp:Content>

UC_Button.ascx.cs:

UC_Button.ascx.cs:

protected void button1_Click(object sender, EventArgs e)
{
    Button btnSender = (Button)sender;
    Page parentPage = btnSender.Page;
    UserControl UC_text = (UserControl)parentPage.FindControl("text1");
    UC_text.Visible = true;
}

我是什么做错了吗?我得到众所周知对象引用未设置到对象的实例。在code的最后一行的错误。

What am I doing wrong? I get well known Object reference not set to an instance of an object. error on that last line of the code.

编辑:

有一件事我忘了提,当第一次发布此。用户控件是在不同的&LT; ASP:内容&GT;&LT; / ASP:内容&GT; 控件(我编辑上方的例子)。如果我把它们放在同一个占位符code的作品就好。如果我把它们放在单独的内容占位符,我不能与任何的FindControl办法找到他们。这是为什么,我如何才能找到他们?

One thing I forgot to mention when first posting this. User controls are in different <asp:Content></asp:Content> controls (I edited upper example). If I put them in the same placeholder code works just fine. If I put them in the separate content placeholders I can't find them in any way with findcontrol. Why is that and how can I find them?

推荐答案

好吧,我发现解决方案,直到更好的来我的方式。问题是,杰米迪克森指出(谢谢你,杰米):

Ok I found solution until better one comes my way. The problem is, as Jamie Dixon pointed out (thank you Jamie):

FindControl方法并不做控制了深刻的搜索。它直接看在你指定你的要求控制的位置。

所以,因为我有不同的contentplaceholders用户控件我必须先找到有针对性的占位符(其中用户控件所在),然后我可以搜索里面的用户控件:

So because I have user controls in different contentplaceholders I must first find targeted placeholder (where the user control reside) and then I can search for user control inside it:

protected void Dodaj_Feed_Panel_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder MySecondContent = (ContentPlaceHolder)this.Parent.Parent.FindControl("MyTestContent2");

        UserControl UC_text = (UserControl)MySecondContent.FindControl("text1");
        UC_text.Visible = true;
    }

真正苦恼和困惑我的是 this.Parent.Parent 部分,因为我知道这是不是最好的解决办法(如果我更改层级有点这个code将打破)。什么code,这部分实际上做的是,它在页面层次结构变为向上两级(即页面,无论用户控件)。我不知道与 this.Page 的区别是什么,因为对我来说意味着相同的,但不是为我工作。

what really annoys and confuses me is the this.Parent.Parent part because I know it's not the best solution (in case I change the hierarchy a bit this code will break). What this part of code actually does is that it goes two levels up in page hierarchy (that is page where both user controls are). I don't know what the difference with this.Page is because for me it means the same but is not working for me.

长期的解决办法是这样的服务器端的jQuery样选择器(它可以找到的元素,无论他们在哪里层次结构)。任何人有一个更好的解决方案?

Long term solution would be something like server side "jQuery-like selectors" (it can found elements no matter where they are in hierarchy). Anyone has a better solution?

这篇关于如何找到在该ASP.NET页面编辑其他用户控件的事件内的ASP.NET页面的用户控件:不同的内容占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆