Calling a Web Method from ASPX Code Behind - c#

I'm trying to figure out a way that I can call a Web Method (located as a public static method in the code behind of an aspx page) from another ASPX Code Behind page.
This is the code behind of Page A.aspx
public partial class PageA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// other code not relevant is here
}
[WebMethod(true)]
public static string GetStringInfo()
{
// do some stuff here to build my string
// return string
}
}
On page B, I need to be able to call GetStringInfo() during page load or some other event to get the information. the GetStringInfo() is fairly complex and for reasons outside of my control, can't be moved elsewhere or rebuilt presently.
How can I consume the web method above from another page's code behind?
I've tried instantiating a copy of the other page (PageB), such as:
public partial class PageB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PageA page = new PageA();
page.GetStringInfo();
}
}
The problem here is since it's dynamically compiled, I don't have an easy namespace to reference and access. I've tried adding one, and it ignores it.
This project is on .net 3.5, C#, and is a web site project (not a web application).
Any help is appreciated!

If the GetStringInfo method is static you don't need an instance of PageA to invoke it:
public partial class PageB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string info = PageA.GetStringInfo();
}
}

Related

Global objects in C# ASP.Net - always null when read back

In an ASP.Net project, using C#, I have a class (PlcComms.cs) for talking to a Controller (a PLC).
I want the one class object to be globally available, for each web page in my project.
I see from reading various forum posts that I should be able to create a public static object of my class type, i.e. PlcComms, in Global.asax.cs, or in a class in the App_Code folder. I've tried both and I write to the object ok, but when I go to read from it (from a timer in an update panel on the home web page) then it always read back as null.
I'm at a loss to know what to do at this point. can anyone help?
Currently, this is a class I have in the App_Code folder...
namespace SpearheadWeb
{
public static class AppGlobal
{
public static SpearheadWeb.PlcComms PlcCommsObject { get; set; }
}
}
this I have on my web page - it seems to create the object OK...
namespace SpearheadWeb
{
public partial class _Default : Page
{
private PlcComms CurrentPLC;
//some other here including ComPorts
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
//some settings reading and setup here
CurrentPLC = new PlcComms(Global.CommsSettings.CpuType1,Global.CommsSettings.Network1,ComPorts[0], Global.CommsSettings.IPAddress1, Global.CommsSettings.Path1,UpdatePanel.Controls[0].Controls, 1, Global.CommsSettings.MsUpdate1);
AppGlobal.PlcCommsObject = CurrentPLC;
but in my timer (the timer within an updatepanel) PLCComms1 is always null here...
protected void TimerUpdate_Tick(object sender, EventArgs e)
{
PlcComms PLCComms1 = AppGlobal.PlcCommsObject;

How to code reuse apsx?

I know in C# I can make a factory but I don't not know how to reuse code in aspx. My code was originally purposed for ARList only, but now has IcnList. I thought about making a switch statement but isn't there something better to code?
The Function
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ARExtractionController arController = new ARExtractionController();
Dictionary<int, string> ARDictionary = arController.GetTickets();
List<int> sortedARList = new List<int>();
foreach (KeyValuePair<int, string> kv in ARDictionary)
{
sortedARList.Add(kv.Key);
}
sortedARList.Sort();
sortedARList.Reverse();
ARList.DataSource = sortedARList;
ARList.DataBind();
ARList.Items.Insert(0, " ");
ARList.AutoPostBack = true;
}
}
If you just want to be able to reuse the code, add a class to your project for utility methods and dump it in there for both pages to use.
If you're trying to reuse code with and associated UI, look into User Controls (ascx files) which allow you to do just that.
It isn't clear which is right for you from the question.
In TicketExtractionWeb, create a BasePage class.
BasePage.cs:
public class BasePage : System.Web.UI.Page
{
// Add methods that are used on all your pages in here.
protected DateTime GetCurrentDate()
{
return DateTime.Now;
}
}
And a page (we'll call it MyPage):
public class MyPage : BasePage
{
protected Page_Load(object sender, EventArgs e)
{
var currentDate = this.GetCurrentDate();
}
}
So when you create another aspx page, it will default to:
public class MyNewPage : System.Web.UI.Page
{
// ...
}
Just change : System.Web.UI.Page to : BasePage
You should be able to inherit from a single page to reuse the code.
Top of aspx file:
<%# Page Language="C#" AutoEventWireup="true" Inherits="MyNamespace.CommonCode" %>
If you want to reuse html , please use a user control file (.ascx) , which can be reused muliple times
when do you need .ascx files and how would you use them?

Web service and global parameter

I start building a new web service for my app and i want to make a global int for example that every time someone call this web service this int will increase in one.
this is what i create in visual studio:
public partial class Form : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
and i try to put static int in the class but it not work.
Put your variable in Application state.
Note you need to make it thread safe.
from http://msdn.microsoft.com/en-us/library/94xkskdf(VS.80).aspx
Application.Lock();
Application["PageRequestCount"] =
((int)Application["PageRequestCount"])+1;
Application.UnLock();

C# - Easy way to execute code in all pages in the Page_Load method

I'm looking for a way to add some code to execute in all Page_Load events on all pages of my web application, without have to write it in all the pages.
The code must execute before the Page_Load methods on the pages.
Thanks for the attention.
You can create one Class let say BasePage.cs, and here you will have one virtual method Page_Load:
public class BasePage: System.Web.UI.Page
{
protected virtual void Page_Load(object sender, EventArgs e)
{
//Some logic here that you want to execute for all pages
}
}
Then, in every page where you want to execute this code on PageLoad, make that page to inherit from BasePage and override the PageLoad method, like this:
in file somePage.aspx.cs do this:
public partial class somePage : BasePage
{
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e); //This line will execute page load from BasePage class
//The rest of code you want to execute on this page load
}
}
You could create a master page, set all the pages you want the code to be executed as "childs" of your master page, and them put the code you want to be executed on the Page_Load event of your master page.
To see how master pages work: http://msdn.microsoft.com/en-us/library/ie/wtxbf3hh.aspx

Where is the other half of the _Default partial class that is created when you create a webform?

When you add a webform, you get a file called Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Where is the other half of it?
The other half is the Default.aspx.designer.cs file which includes all the controls you've added to the ASP.Net page.

Categories