如何在 ASP.NET 页面上另一个用户控件的事件内查找 ASP.NET 页面的用户控件编辑:不同的内容占位符?
我有一个 ASP.NET 页面,其中注册了 2 个用户控件。第一个只有一个按钮。第二个是简单文本,默认情况下隐藏。我想要的是当单击第一个按钮中的按钮(即按钮单击事件)时使第二个按钮可见。
ASP.NET 页面:
<%@ 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:
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;
}
我做错了什么?我在代码的最后一行得到了众所周知的对象引用未设置为对象的实例。
错误。
编辑:
我第一次发布此内容时忘记提及的一件事。用户控件位于不同的
控件中(我编辑了上面的示例)。如果我把它们放在同一个占位符代码中就可以了。如果我将它们放在单独的内容占位符中,我将无法使用 findcontrol 以任何方式找到它们。这是为什么?我怎样才能找到它们?
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 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:
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;
}
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.
EDIT:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请检查以下内容:
please check below:
FindControl 方法不会对控件进行深入搜索。它直接在您为所请求的控件指定的位置中查找。
在您的情况下,您需要做的是:
您还可以在这里看到我的问题: 使用yield return的IEnumerable和递归演示了按类型查找深层控件的方法。
The FindControl method does not do a deep search for controls. It looks directly in the location you specify for the control you're requesting.
In your case, what you'll need to do is something like:
You can also see my question here: IEnumerable and Recursion using yield return that demonstrates a method of finding deep controls by type.
好吧,我找到了解决方案,直到更好的解决方案出现。问题是,正如 Jamie Dixon 指出的那样(谢谢 Jamie):
FindControl 方法不会对控件进行深入搜索。它直接在您为所请求的控件指定的位置中查找。
因此,因为我在不同的内容占位符中拥有用户控件,所以我必须首先找到目标占位符(用户控件所在的位置),然后我可以在其中搜索用户控件:
真正让我烦恼和困惑的是 this.Parent.Parent 部分,因为我知道这不是最好的解决方案(如果我稍微更改层次结构,此代码就会中断) 。这部分代码实际上所做的是,它在页面层次结构中上升两级(即两个用户控件所在的页面)。我不知道与
this.Page
有什么区别,因为对我来说它意味着相同,但对我不起作用。长期解决方案类似于服务器端“类 jQuery 选择器”(它可以找到元素,无论它们位于层次结构中的哪个位置)。有人有更好的解决方案吗?
Ok I found solution until better one comes my way. The problem is, as Jamie Dixon pointed out (thank you Jamie):
The FindControl method does not do a deep search for controls. It looks directly in the location you specify for the control you're requesting.
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:
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 withthis.Page
is because for me it means the same but is not working for me.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?
使用用户控件的id,然后将控件(例如文本框)放入面板中然后
尝试主页
示例中的此代码:
用于使用 udpatepanel 进行更新:
TextBox txt = (TextBox)showbutton1.FindControl("Textbox1");
txt.Text="你好世界!";
use the id of user control,then put the controls (e.g Textbox) inside panel then
try this code from the main page
example:
for updating with udpatepanel:
TextBox txt = (TextBox)showbutton1.FindControl("Textbox1");
txt.Text="Hello World!";