Sentius
Sentius

Reputation: 7

ASP.net(C#): Is it possible to access controls on one webform from another?

I am currently building a web app that will become the new data center (intranet website) for my company. I have it hooked into active directory and have a user group set up to allow page editing. I am creating a usercontrol to give our QA manager more editing power so that he can manage the site without me having to re-code anything.

Here's my issue: As I am using a usercontrol as the new edit control; I need to be able to access the controls on whatever page the edit control is being used on. For the life of me I cant figure out how.

Here is a simple example of what I'm trying to do:

 WebPartManager wpm = (WebPartManager)WebPartManager.GetCurrentWebPartManager(this.Page);


    TextBox testBox = new TextBox
    {
        ForeColor = System.Drawing.Color.Blue,
        ID = "testID",
        Width = 500,
        Height = 200
    };


    GenericWebPart testGWP = wpm.CreateWebPart(testBox);

    wpm.AddWebPart(testGWP, WebPartZone4, 1); //heres where I get my error. WebPartZone4 is a webPartZone in one of my pages.

If I run this code on the page that has said WebPartZone the desired effect is achieved so I know the code works. My usercontrol just cant see the WebPartZone on another page. Any help at all would be great. Thank you.

Upvotes: 0

Views: 1190

Answers (1)

Sentius
Sentius

Reputation: 7

I feel slightly stupid for how easy this ended up being, but here it is for anyone having the same issue.

Here's the design for a simple user control I used to test this. Its just a button. xD

<%@ Control  Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl3.ascx.cs" Inherits="AcmeCompany_DATA_CENTER_v2.WebUserControl3" %>
<asp:Button ID="Button1" runat="server" OnClientClick="Button1_Click" Text="Button" OnClick="ButtonCreate_Click" PostBackUrl="~/WebUserControl3.ascx" />

heres the code behind for the user control

 protected void ButtonCreate_Click(object sender, EventArgs e)
    {
        WebPartManager wpm = (WebPartManager)WebPartManager.GetCurrentWebPartManager(this.Page);

        TextBox testBox = new TextBox
        {
            ForeColor = System.Drawing.Color.Blue,
            ID = "testID",
            Width = 500,
            Height = 200
        };          

        GenericWebPart testGWP = wpm.CreateWebPart(testBox);

        wpm.AddWebPart(testGWP, wpm.Zones["WebPartZone4"], 1);
    }

I was unaware of the Zones class inside of webpartmanager, and I came across it just scrolling through looking for something that may work. Tada.....It's always the little things.

P.S. @Maciej S. Thank you, While you didn't give me the answer, our conversations helped my brain think to look for certain things.

Upvotes: 1

Related Questions