Error using 'this' in a cs page trying to access control - c#

I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks

You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.

Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).

Related

Access the parent Telerik.Web.UI.RadWindow from the contained System.Web.UI.Page

The question is in the title :
I have a System.Web.UI.Page contained in a Telerik.Web.UI.RadWindow and I can't find out a way to access the RadWindow from the Page code-behind.
I tried to explore the Page properties, and I guess that there's a casting to do on a control at some point, but I didn't find anything.
EDIT : At runtime, I have no clue on where the RadWindow comes from (it is generated dynamically from about anywhere in the application), so I can't access it to modifiy the RadWindow definition. That's why I need to access it at runtime, to alter it programmatically.
Can you help me ?
Thanks !
You can't access the RadWindow in the code-behind of the content page. That would be equivalent to accessing Default.aspx from SomeOtherPage.aspx in the code-behind. There simply isn't a way to do that.
You can, however, use JavaScript. If the pages are all yours (i.e. same application, same domain) you can get a reference to the RadWindow and use its Client-side API. See here:
http://www.telerik.com/help/aspnet-ajax/window-programming-radwindow-methods.html
and
http://www.telerik.com/help/aspnet-ajax/window-programming-calling-functions.html - the GetRadWindow() will return the reference
and here
http://www.telerik.com/help/aspnet-ajax/window-application-is-page-in-radwindow.html
You need to assign an xname to your RadWindow.
<telerik:RadWindow x:Name="myWindow">
...
</telerik:RadWindow>
Then you can access myWindow in the code-behind of whatever xaml this is in.

Changing displayed data from a static method in ASP.NET

I have two user controls that sit on a page which determines which should be displayed. The user controls generate some html that is passed into an asp:literal and subsequently manipulated via javascript. It is done this way due to the lack of a suitable control that I am allowed to use on the project.
When a user clicks a view change button, a WebMethod is called on the main page (the one that holds the controls) from the control's javascript. From here, a static method on the control is called. The control then needs to regenerate the html and place it into the asp:literal for the view change to be complete.
My problem is that I am in a static method on the control's page, and have no access to the non-static genorateHtml function. I have tried a singleton pattern with no success (this could have been due to improper implementation). Any ideas on how to make this call? THANKS!
I used to hit similar issues at with one of the projects i worked on. The solution we ended up adopting was implementation of System.Web.UI.ICallbackEventHandler with partial rendering to return just the needed content depending on arguments. ICallbackEventHandler runs in the page lifecycle.
The only trouble we had then was performance issues relative to implementation which posts back the whole form instead of just the arguments you want.
Maybe the best way for you would be through this method in which they render the control from a static method. That would probably suit your needs.
Hope this helps!

Accessing a UserControl from different Usercontrol

I am trying to use the already written code which accesses a control from an other control in the following code.
Controls_SearchFeaturedMerchants UCMerchant = (Controls_SearchFeaturedMerchants)this.Parent.FindControl("UCSearchFeaturedMerchants1");
I am wondering what this statement says. Can someone please give me some idea :)
Thanks in advance!
If I understood your question correctly here is the brief:
On a page (ContentPage) is hosting two user controls (UsrCtrl1, UsrCtrl2).
Now "UsrCtrl1" wanted to access some data in the "UsrCtrl2".
For that I'll write code like - "UsrCtrl1" parent is the "ContentPage" that page hosts "UsrCtrl2", so I'll first get the "UsrCtrl2" from the parent page with the following code:
this.Parent.FindControl("UsrCtrl2Name") -> this means current user control which is "UsrCtrl1" and Parent means is the "ContentPage" (it may be parent control or page) in that you are trying to find a contrl with Id "UsrCtrl2Name" (this is the id of the 2nd user control in the content page).
When you use FindContrl method it always returns base type UserControl and you need to cast it to your usercontrol in this case it is of type "UsrCtrl2".
I hope this is clear now.
in plain text it is saying:
Get the Parent Control of the current control and then find UCSearchFeaturedMerchants1 on said control. Cast the result into a Controls_SearchFeaturedMerchants.
Hope that sums it up for you

Controls in a telerik control not accessible from .cs file

I am trying to use this solution to access items inside a telerik menu item:
ascx code:
<asp:Label ID="DivLeave" runat="server"></asp:Label>
In the ascx.cs file I run this code to disable the asp label
RadMenuItem expenses = RadMenu1.FindItemByText("Expenses");
Label DivLeave = (Label)expenses.FindControl("DivLeave");
DivLeave.Visible = false;
but I get this error when I try to run the code:
{"Object reference not set to an instance of an object."}
Can anyone tell me how to fix this problem. I really need to run this server side as code surrounding the above code does some work server side and it will all fit in neatly...
Kind regards
This is because the name of your label is not "DivLeave" when the HTML for your form is rendered. Since it is inside a user control it will be a combination of the user control name on the page and then "DivLeave". You should be able to see the name by looking at the code behind. Also why can't you just reference DivLeave.Visible without using the FindControl? Its an ASP.NET control with the runat server attribute so it should be available to you.
Can you do a quickwatch for 'expenses' object in Visual Studio and see if 'DivLeave' is available? It may so happen that:
The label control is available but at a different level in the object.
The label control itself is not getting added to the parent 'expenses'.
Also, it would be a good idea to do a null check for expenses and DivLeave objects before accessing them.

asp.net - variable pass between ascx and page

Have any ways to keep a variables (object) in a page scope? I need pass some object from master page to all ascx files. Normally, i passing by using a method or structure with parameters. But now im asking another way to do this. Store it in session is a bad idea because my object should exists in a page scope only.
One way is to set up properties in your user controls. You have access to read and set these from all pages that implement them.
Another alternative is to store the shared object(s) in the HttpContext.Items collection.
You could expose your variables a public properties of the master page:
public string MyVariable { get; set; }
then access them from the user controls by referencing the master page, and casting to its specific type:
((MyMasterPageType)Page.Master).MyVariable
So you have masterpage, user controls and the page itself.
The page is the container for the masterpage and the user controls, hence it's the only one that 'knows' the 2 parties. So I'll suggest (if you haven't) you have the page instance to facilitate the object/variable passing amongst them.
If your variable's value is going to be changed on per page basis then i would recommend you to write that code in base page (or user control) and inherit all the page (or usercontrol),if the values are going to be similar for all pages(or user control) you can use cache object as well.
In a more better approach if you feel you can even create one helper class and call it from your base page (or user control), so you can separate variable assignment code from your page.

Categories

Resources