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?

房东的猫 提交于 2019-12-23 18:29:00

问题


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?


回答1:


please check below:

UserControl UC_text = (UserControl)this.NamingContainer.FindControl("text1");



回答2:


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:

UserControl UC_text = (UserControl)Content1.FindControl("text1");

You can also see my question here: IEnumerable and Recursion using yield return that demonstrates a method of finding deep controls by type.




回答3:


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:

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;
    }

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.

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?




回答4:


use the id of user control,then put the controls (e.g Textbox) inside panel then try this code from the main page

example:

TextBox txt = (TextBox)showbutton1.FindControl("Textbox1");

for updating with udpatepanel: TextBox txt = (TextBox)showbutton1.FindControl("Textbox1"); txt.Text="Hello World!";

((UpdatePanel)showbutton1.FindControl("UpdatePanel1")).Update();


来源:https://stackoverflow.com/questions/6828124/how-to-find-user-control-of-an-asp-net-page-inside-of-event-of-another-user-cont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!