views:

44

answers:

2

Within my Website project, I have some aspx pages, javascript files, and a custom C# class I called MyCustomReport. I placed a Image box with an ID of Image1 inside SelectionReport.aspx. I need to get access to that Image1 inside MyCustomReport.cs so I can turn it on and off based on conditions. What code do I need to do this? Thanks everyone

+2  A: 

You'll need to pass the instance of Image control to MyCustomReport. From there you'll be able to set it's Visible property to true or false.

Probably something like this

public partial class SelectionReport : Page
{
    // your code here

    protected void Page_Load( object sender, EventArgs e ){
        MyCustomReport myCustomReport = new MyCustomReport();
        myCustomReport.MyReport( Image1 );
    }
}

public class MyCustomReport
{
    public void MyReport( Image arg ){
        // some more code
        arg.Visible = false; // or true
    }
}

EDIT derek is right, you won't need the entire page, just the image.

R0MANARMY
Hi Romanarmy- Does the code you posted above need to go into something like SelectionReport.aspx.cs? I don't have a cs class for that yet but can create one.
Josh
@Josh: The first class yes, the second class no. I just put the two together to illustrate a point. And how do you not have a class for that, doesn't visual studio generate one for you when you add a new page to the project?
R0MANARMY
Trying to implement your code but having a little issue (I'm learning, sorry). I created a file called SelectionReport.aspx.cs and added a reference to the appropriate assembly and then declared it at the top of my class. Next I added your page_load and in the first line the namespace in MyCustomReport is CustomReports so I declared CustomReports.MyCustomReport RP = new CustomReports.MyCustomReport(); In the second line I tried RP. but intellisense din't show MyCustomReport or am I looking for the wrong thing here. What did I miss?
Josh
@Josh: You should read this, it goes into more detail about how code behind files work and whatnot. http://msdn.microsoft.com/en-us/library/015103yb.aspx
R0MANARMY
A: 

it sounds a bit odd to do it that way. You could pass the control to the class method using the ref keyword, then the class could modify it:

doSomething(data, MyUserControl);

I think a better implementation would be for your class to have a method or property that the page could query to turn the control on or off.

derek
@derek: Why would you need to pass it by reference? He just needs to change the property on the object, not change the object itself.
R0MANARMY
To clear things up, MyUserControl would be the Image1 control, not the entire page. That will give him access to the object to change its property. Not much different than passing the SelectionReport instance, just narrowing down the reference that is sent.
derek
agreed. Essentially your suggestion basically implements my second point of just making it a property or method that the page could query. I would even simplify it further by having: Image1.Visible = MyCustomReport.MyReport(), and have that return a bool.
derek
correct, I think I had my keywords mixed up, out would definitely be the better solution in my example.
derek