0

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.

11
  • Is this not due to the fact that each page is contained and you need to post/get the data from another page? I'm pretty sure this is a protections issue outlined here Commented Dec 5, 2018 at 23:19
  • @CaptainWibble I had already read that post. It is referring to using multiple classes or namespaces in the same webform. I am attempting to have my usercontrol identify controls of another page. I have seen something to this effect done with one usercontrol changing a property of another usercontrol using session state. I was hoping there might be a way to do something similar. Commented Dec 5, 2018 at 23:28
  • Well one method is disabling ValidateRequests in the @page and running any access requests asynchronously using an ASHX handler with AJAX given this is on intranet (note this is a method that is used to check script injection). Commented Dec 5, 2018 at 23:34
  • You may have just given me an idea. I'll test my theory and post the answer if it works. Thanks. Commented Dec 5, 2018 at 23:47
  • Yeah... I had similiar problem when developed websites in ASP.NET Webforms. It's old technology so You probably need old solutions... Try singleton. It's obsolete pattern but still usefull in Webforms, Winforms and old Windows Mobile apps. Try this site for more nformation, understanding, types and samples: link Commented Dec 6, 2018 at 0:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. :) Glad to hear that I could help even if it wasn't full solution. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.