how can I treat a button handler like a method call? - c#

I have a VERY large asp.net application, it has many different .cs files in the project. I would like to trigger one button handler's method from another project. The reason is that in the .cs file that the button handler is it, there are many variables and methods being used that are private and I dont want to change the accessibility of all of them to public in order to access them.
SIMPLY put. how can I call the method " protected void try_validate(object sender, EventArgs e)" from another method in a different .cs file?
I am trying to access resetPage from a different .cs file in the project. I HAVE placed the code for resetPage into a new method called WriteToDB but even that I cant call from the other .cs file

Is it possible if you place the logic within the handler into another public method and then you invoke it from where ever you need to?

Related

Create a void method for each button

Hi im hoping someone can assist im still new to programming and this is a noob question but i have created a Visual studio - C# (Windows Form Application) and now the question reads to Create a void method for each of my buttons i created in the form and telling me even what to name the method.
but on my research The void keyword is used in method signatures to declare a method that does not return a value.
LinkToAddresses () will be my void method for address the (button), so my question is do i just put in this void method and its going to do nothing?
im just going to link the full question maybe im just really not understanding this>?
''
The below form will represent the main form from which the user will navigate to the other forms. Meaning each button should be linked to the appropriate form. E.g. If button Manage Addresses is clicked the form managed addresses should be displayed. The Exit button should successfully terminate the program.
Create a void method for each button and name them as follow: LinkToAddresses (), LinkToCustomers (), LinkToDrivers (), LinkToStatus (), and LinkToFreight (). The methods should be called under the appropriate button. For the exit button create a void method named AppExit () this should terminate the program.
''
I would appreciate any help or guidance, thank you in advance.
Visual studio usually handles the button actions easily. Just place the buttons on your form, then rename the buttons to LinkToAddresses, LinkToCustomers, LinkToDrivers, LinkToStatus, LinkToFreight and AppExit. Then simply just double click on the each button and visual studio will create a void method for their click event.
using System;
using System.Windows.Forms;
namespace YourApp
{
public partial class FormMain : Form
{
private FormManagedAddresses formManagedAddresses = null;
public FormMain()
{
InitializeComponent();
}
private void LinkToAddresses_Click(object sender, EventArgs e)
{
if (formManagedAddresses != null)
{
formManagedAddresses.Close();
}
formManagedAddresses = new FormNews();
formManagedAddresses.Show();
}
private void AppExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
The closest thing to a buttons function, is the Click Event Handler. While specific names vary based on Display technology (WinForms, WPF/UWP, ASP.Net), that is the general pattern for Graphical User Interfaces. It is called event driven programming. Even things that have a different programm flow like Games and Web Applications usually try to imitate it.
The signature of a event is given during its definition and must be strictly followed. Usually void NameOfTheEvent(object sender, SampleEventArgs e).
A return type of void is extremely common with events. If there is to be any output, that usually is handeled via a property in the Event Args or by directly doing stuff with the other GUI Elements.
If you want a button to do nothing, you just never give it a event handler. Every single button you ever used, was given a implicit or explicit event handler to do exactly what it did. If you want it to conditionally do nothing, either disable the Button so it can not be clicked, or put a proper if-statement into the event Handler.
A advanced topic would be the command pattern, where there is a bunch of commands in code behind. And each button, menu item and key combination is meerely a way to trigger said command - a representation for hte user to call the command.
You can share a single event across any number of Elements. AS you can see above, the pattern for events includes object sender as argument. This means you can check if it is a specific Button instance that called the event. Or even "unpack" the specific button, do look at stuff like Display String, Tag to get data from it. However, as a general rule retrieving data from the GUI is a bit frowned - ideally the GUI should only represent the data in the backend.

Trigger event on aspx page from .cs file without stopping current execution

I have an aspx page which posts a form back to itself, processes the form in a .cs page (in the DLL, not code behind) and then the code takes one of many branches based on that outcome.
This is a rough outline of what happens:
protected void Page_Load(object sender, EventArgs e)
{
LoanApplication oLoanApplication = new LoanApplication();
string sStep = Request.Form["step"];
oLoanApplication.Process(Request.Form); //Sets the value of the object
}
Then, in the Process method that is in the DLL code, I want it to trigger an event at this point:
public void Process(NameValueCollection pFormData)
{
SendApplicationToWebService();
//Trigger event here on aspx page
}
Basically I am trying to pass a variable returned from the SendApplicationToWebService(); into Google Tag manager.
There are many different branches the code could take, and I do not think it is a good solution to do it at the end - it is best to do it at the point the variable is assigned its value - that way I know all code is passing through this point.
Some of the branches redirect to different pages, so I cannot do this after the process method is called on the Page_Load because the code may never be executed.
Any ideas?
Why don't you pass a callback function to oLoanApplication.Process ?
take a look on this sample

How can you make a method call whenever GridView Sorts?

So what I want to do is allow AutoSorting that's built into GridView, but whenever autosort happens, for it to call a method (recolor();) to recolor the results appropriately. Probably an easy question, but I don't see any easy way to do it.
It is a .net form (a .dll I'm using with another application), I have not attempted any code yet, as I don't want to break what I already have set up.
EDIT ANSWER
So, I figured it out in the designer I should have put:
this.dataGridView1.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridView1_ColumnHeaderMouseClick);
And then in the form this will work :
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
recolor();
}
Call recolor() in the event GridView.OnSorting. See: this article on MSDN.

Object not loading when I add `Load_Event`

The object I'm using is <asp:Chart/>. I built it using Visual Web Developper 2010's binding tool:
I changed the look and behavior with the properties pannel and a CSS sheet. Everything was coming out fine until I accidently double-clicked on the chart. This added a myChart_Load method to my code. Now if I boot my page with the empty chart load method, my chart disappears from the screen as if it was only looking in the chart_load to see what to do, see nothing then don't show anything, forgetting the binding tool and properties/CSS.
Then if I delete
protected void myChart_Load(object sender, EventArgs e) {
}
the empty load above, the compiler crashes and says it is expecting a load method. Any idea why it's behaving like this?
I don't know about why adding a Load handler it doesn't come back, but it's erroring now because in the markup, it added the event handler reference there OnLoad="myChart_Load", so it looks for this, can't find it, and throws an exception.
you can remove the method from code behind by just deleting it. you'll also have to remove the reference of the method found in the .designer.cs file.

Where is the Component Initialize Method in Asp.NET 3.5

I want to create my own naming convention for page events rather than AutoEventWireUp but I couldn't find Component Initialize methods any where ? Should I override it ? But in which class it is defined ?
Thanks...
Edit :
For example : I don't want to use Page_Load but LoadThisPage naming. So It should be like
Load += new LoadThisPage(sender,e);
I was expecting a InitializeComponent method where I can initialize page,controls etc. events handlers...But it turned out to be Constructor function :)
So what confused me is I thought there should have been a method like InitializeComponent which does things for me already created by Designer itself so I thought I could define my own event handler names within this method by overriding it in the say Default.aspx.cs .
But the answer was simple :) Thanks...
Your question isn't really clear as to what you're trying to do.
Page events are defined in the System.Web.UI.Page class, some of which are inherited from System.Web.UI.Control. You don't need to use AutoEventWireUp if you don't want to, and you're free to override all of the Page methods that would normally raise the lifecycle events (OnInit, OnLoad, OnPreRender, etc.) and then not call the base methods, effectively squelching the events from being raised.
You can see some limited discussion around this on this blog post.
As Hogan noted, this sounds like a bad idea. Could you expand on what you're trying to accomplish?
This sounds like a bad idea to me.
However, you should be able to find auto-created code by selecting the project display options, I believe it is right click in project explorer, and selecting display hidden files or display all files. Then you will see additional .vb files created by the system. You might also have click on the little plus sign.
additonal added notes
Most of the events (btw) are defined in the base class(es) in System.Web.UI.Page and not in the code created for a specific instance.

Categories

Resources