static void Client script - c#

I have a method in an aspx web page which is accessed from another non webpage .cs class. This method contains a ClientScript to create a popup.
I get this error below when trying to make it a static.
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get'
So I am wondering if there is a way to access this method from another .cs class or a solution.
Basically when an error trips on the .cs class I want to display a popup that says the error.
public static void message(string popupinformation)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "popupinformation", true);
}
In .cs
AddUI.message("alert('Error can not add information');");

tl;dr - You need to add "System.web" reference in that library.
I understand that, you have a separated cs file in some another library that invokes a function sitting in asp.net webform. If that is true, then how you are calling that function because it will be inside a page class which inherits from system.web.ui.page.
Lets assume you have make it work by placing that in App_Code. Then used that class say Web_work.cs in some other library say lib.cs.
To use ClientScript... in the library with lib.cs you need to add "System.web" reference in that library.
Best practice is to do all validation in library, but use such kind of display method in webform only. Take an example, lets say you want to use windows form now, which uses MessageBox.Show, in that case your library needs change with current approach.

Related

Accessing another page's functions in Windows Phone 8

Basically I have a page in my app where there's an async function that receives data via bluetooth. I want to execute functions on my main page based on the data I receive via bluetooth on the other page. I searched around a bit and mostly people suggest creating a base class and inheriting both pages from the same class, but that isn't what I'm trying to do: I don't want to execute a generic instance of the main page's function, I want to execute the specific main page function that belongs to the main page that my app is displaying.
I thought of two possible solutions to this:
Update a static variable in the main page class based on the received data, and use a property to execute the required functions every time the variable changes value, but the problem is that the functions I want to execute are non-static.
If I could know the object name of the main page class which my app instantiates, I could simply make the functions public and access them using syntax like MainPageObjectName.myFunction(), but I don't know the name of the object instantiated from the MainPage class by my app.
Any advice? Thanks in advance.
Ali, I assume here that you need to update the contents of the MainPage.xaml while some background (async) task has finished.
The best way to do that is have a DataModel (MVVM) in your application. More info here
and here.
By doing this when the async task finishes a function will be called to update the DataStructure that holds your data. Since that Model implements the INotifyPropertyChanged class the control on your UI (even though they are on another page) will be updated automatically.

ext.net direct method, call from external class method

I have a method in a class like below:
public class ActionHelper
{
[DirectMethod]
public string DeleteComment()
{
...
return "Delete";
}
}
and I want to call it from grid command like this:
<Command Handler="Ext.net.DirectMethod.request({url: '/Classes/ActionHelper/DeleteComment', cleanRequest: true}});" />
but it's not working! how can i do that??? I use ext.net 2.2 and .netframework 4.5
look at this example
http://examples.ext.net/#/Events/DirectMethods/ID_Mode/
it can be helpful
Put a [DirectMethod] in your code behind wich calls that class and use
App.direct.<Method>(); instead
You can't, you either define the method inside the Page, User Control or any Custom Control, or you define a static method inside the Page class.
Here is a quote defining direct methods from a post in the Ext.net forums:
DirectMethod (none static, must be public): server side handler is
raised when you call special javascript method (basically, proxy
method is generated by Ext.Net toolkit). None static direct method can
be defined inside Page, User Control or any Custom Control. Please
note, if direct method is defined inside user control (master page
placeholders are user controls also) or custom control then ClientID
of that control will be added to proxy method 1
Ext.net.DirectMethods.ClientIDOfTheControl.DirectMethodName(); You can
use DirectMethodProxyID attribute for the class to define own alias or
completely remove ClientID prefix Really, none static direct method is
direct event. Single difference, that direct method has no relation
with any widget (and its events) and can be raised by developer from
javascript (as javascript method)
Static DirectMethod (must be public): similar ASP.NET PageMethods, can be defined inside Page class only. With static page method the
Page life cyle is not execued therefore access to ASP.NET control is
not possible but response time much better (depends from your method
logic only)

referencing one aspx.cs class in another aspx.cs class?

Does anyone know how to reuse the code from one aspx.cs class in another aspx page?
It all depends on what you're ultimately doing, but one option would be to build a re-usable class in your App_Code folder and use it across your entire site.
Ideally you should put your reusable methods in a seperate class in a seperate cs file.
But what you are asking for can also be easily done, here is an example:
Page1.aspx.cs
public partial class MyPage1: System.Web.UI.Page
{
public static void MyTestFunction()
{
//Code Here
}
public void MyTestFunction2()
{
//Code Here
}
}
Page2.aspx.cs
public partial class MyPage2: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyPage1.MyTestFunction(); // static function call
//or
MyPage1 page1 = new MyPage1();
page1.MyTestFunction2();
}
}
There are several options for reuse in ASP.NET:
Master pages
user controls
composite controls
create a custom class which inherits from the Page class and have your pages inherit from that custom class
Create a helper class which has reusable methods which you can use in your different webforms
Reusing code in multiple pages should be done in a separate class as mentioned or by using another mechanism such as master pages, etc. Trying to call code from multiple aspx pages without using backend classes is risky going forward with upgrades as it makes your app more brittle and harder to maintain over time. frontend pages such as ASPX pages should contain their own independent codebehind only. They should not reference another ASPX page. A better model is to set up an object model prior to the GUI layer, then call into the object model from various pages. That way, the page code is responsible only for it's own local page elements and any business logic needed across pages is housed elsewhere. Again, if you need UI logic to be shared, use something like old-style include files, master pages, or user controls which can be very useful at sharing as well as separating out the concerns of each piece (that separation of concerns principle is very important and when done right can help your app remain clean and easy to upgrade later!)
see here How many classes can you inherit from in C#?

ASP.NET and IsNew on the page level

Never seen this before in ASP.NET development.
I'm trying to refactor out 40 single-page ASP.NET pages to code-behind style.
What does this code do?
// Validate required parameters (if "new", then nothing is required)
if (!this.IsNew())
{
if (string.IsNullOrEmpty(_billId))
{
responseErrorNo = 4;
Utils.SendError(respErrNum);
}
}
Its on a single-page design ASP.NET page in the block in the Page_Load method.
On a code-behind page this code ( .IsNew) is not recognized. What am I missing here?
Is there an MSDN page on IsNew of the "page"?
update
Ok. This is my dumbkoff move of the day.
There was a little method hidding at the bottom of the server-side
was protected bool IsNew()
see comments about the inheritance point.
http://msdn.microsoft.com/en-us/library/015103yb.aspx
Have you done a search through all the source files for IsNew?
Some possibilities
1. This is a method inherited from a base class, if you have one of course
2. IsNew might be an extension method. http://msdn.microsoft.com/en-us/library/bb383977.aspx
3. IsNew is a member of the class
If your code-behind file inherits from a custom page class, as in a class like PageBase instead of the standard System.Web.UI.page, the IsNew could be in there, and maybe your page needs to implement that... ALtneratively, it could be an extension method for the page class, and your missing the namespace reference to include it...
HTH.
This is puzzling as the System.Web.UI.Page class definitely has no IsNew() method. The only way you would get that is if the page is inheriting from a base page, or perhaps if it there is an extension method that extends Page.
Can you right-click the method in Visual Studio and find the definition?

Common inheritance for pages, web services and web controls

I have a set of functions I want to be available to my web pages and user controls in my c# .net 3.5 web project. My standard approach for pages is to create a "base page" that extends the System.Web.UI.Page class and then have my pages inherit from this, rather than directly from the Page class.
I now want to expose some of these functions to my web controls (ascx) and web services. I can think of a number of ways to do this, but they seem a little clumsy and I think I'm missing a trick.
Is there an easy way to provide some common functions to both my pages, web services and controls using inheritance, or do I need to wrap these functions in a class that all of them can access?
An example to clarify:
I have a singleton that handles most functionality for my web application.
At the start of each request I want to check that the class exists in the web cache and initialise it if not.
Initially this was handled in a page base that the pages all used. Now I need to be able to access my singleton safely from services and controls, with the same checks. I have therefore extracted the checking and initialisation logic into another class, that then each of my base page, control and web service, all instantiate. Even with this model I have the same code repeated in 3 places (each of my base classes for controls, ws and pages), albeit not much code, this seems wrong too!
It works, but it seems clumsy...I look forward to you guys humbling me with your wisdom!
Sounds to mee like a case of aspect-oriented programming. .NET is ill equipped for this. I'm afraid that your solution is one of the best.
Alternatively perhaps you can move all or some of those functions to a static class/singleton and then use that class from your aspx/ascx/asmx? Not much in the way of inheritance, but at least less code duplication.
My solution to this is to put all the methods and functions I want to share in my base master page class. I then put an equivalent for each method and function in the user control base class as follows:
//Property in masterpage base
public string QsSearchTerm
{
get
{
if (!String.IsNullOrEmpty(Request.QueryString["q"]))
{
return Helpers.SanitiseString(Server.UrlDecode(Request.QueryString["q"]));
}
return String.Empty;
}
}
//Property in usercontrol base
public string QsSearchTerm
{
get
{
if (Page.Master is BaseMasterPage)
{
return ((BaseMasterPage)Page.Master).QsSearchTerm;
}
return string.Empty;
}
}
What this doesn't help with, is your code repetition with web service base classes. I would think that refactoring the above into a class with a constructor that accepts an HttpContext instance would be the way forward. You can then expose a singleton instance of this class in your base web service, master page, user control, page etc.
Hope this helps, but I too would be interested in hearing if there's a better way.
In your Singleton you could provide a Strategy interface to allow variations of the code depending on the configured environment. This would allow you to switch between web/windows/wcf...and so on.
I think using a BasePage is the right approach.
I have multiple base pages and custom user controls that load differently depending on which basepage is used by the current page.
In your custom user control you can use something like:
if (this.Page is BasePageName)
{
BasePageName bp = (BasePageName)this.Page;
bp.BasePageFunction();
}
No you can get ride of the repetitive code in the custom user control and just call it from the base page.
You can also have a hierarchy of inherited base pages depending on page functionality and needs. ie.) BasePageName2 : BasePageName

Categories

Resources