In My VB.NET web page, I have this standard event. Note the "Handles" clause on teh event declaration.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
In my C# web app, I have this:
protected void Page_Load(object sender, System.EventArgs e)
{
Since C# doesn't have a "Handles" equivalent and from what I've seen, event handlers are wired up using delegate += syntax, I was looking for this, but I could not foind it in the aspx page, aspx.cs file or the aspx.designer.cs file.
In VB, I would have two drop down lists at the top of the Code Editor window and I could select any object on the web form or the web form itself and see the possible events for the object. Selecting the event would either take me to the event handler or if it didn't exists, it would create the stub for me.
I know that the Properties window in C# (and I think in VB, too) has an Event tab that shows the list of events for the selected object GUI object, but "Page" doesn't appear as an object that can be selected.
Where does C# define the hooking up of the event to the handler?
How do I generate a stub for the Page event handler routine? I know that the handle appears by default, but what if it is deleted or I want to add a Page_initialize code? Is there an easy way to get the stub or do I need to go to the Object Browser for the syntax?
In C# web forms, the #Page directive AutoEventWireup property on the markup code behind is defaulted to true, as opposed to false for VB. To see the #Page directive and all of its associated properties, right click on your web page in Solution Explorer and choose 'View Markup'
With AutoEventWireup=true, the runtime will automatically connect the event handlers it finds in your code that match the naming convention form of Page_EventName. You can however turn off this functionality and wire up the page event handlers manually using the standard C# += assignment. If you are using the AutoEventWireup=true, not only must your method name match, but obviously it must also have an appropriate method signature in order to be wired up automatically by the runtime.
See this KB for a good discussion of AutoEventWireup: http://support.microsoft.com/kb/324151
With respect to your second question, in C# there is no way to generate "stubs" for page events like there is in VB. As other have noted, including yourself -- there is similar functionality in C# for generating control object event stubs, via the property window. However, for page events you must know the event name and appropriate signature and code it yourself.
Where does C# define the hooking up of the event to the handler?
Page_Load is a special event that is automatically hooked up. It's a reserved name. So there's nothing you need to do for this event to be hooked up. Just declare it in the code behind.
namespace MyNamespace
{
public class Myclass : System.Web.UI.Page
{
override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, EventArgs e)
{
}
}
}
Reference: https://support.microsoft.com/en-us/help/324151/how-to-use-the-autoeventwireup-attribute-in-an-asp-net-web-form-by-usi
Related
What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?
Scenario:
I'm trying to build a custom control with a delete function, and I want to be able to delete the control that was clicked on a page that contains many of the same custom control.
The sender is the control that the action is for (say OnClick, it's the button).
The EventArgs are arguments that the implementor of this event may find useful. With OnClick it contains nothing good, but in some events, like say in a GridView 'SelectedIndexChanged', it will contain the new index, or some other useful data.
What Chris is saying is you can do this:
protected void someButton_Click (object sender, EventArgs ea)
{
Button someButton = sender as Button;
if(someButton != null)
{
someButton.Text = "I was clicked!";
}
}
sender refers to the object that invoked the event that fired the event handler. This is useful if you have many objects using the same event handler.
EventArgs is something of a dummy base class. In and of itself it's more or less useless, but if you derive from it, you can add whatever data you need to pass to your event handlers.
When you implement your own events, use an EventHandler or EventHandler<T> as their type. This guarantees that you'll have exactly these two parameters for all your events (which is a good thing).
Manually cast the sender to the type of your custom control, and then use it to delete or disable etc. Eg, something like this:
private void myCustomControl_Click(object sender, EventArgs e)
{
((MyCustomControl)sender).DoWhatever();
}
The 'sender' is just the object that was actioned (eg clicked).
The event args is subclassed for more complex controls, eg a treeview, so that you can know more details about the event, eg exactly where they clicked.
'sender' is called object which has some action perform on some
control
'event' its having some information about control which has
some behavoiur and identity perform
by some user.when action will
generate by occuring for event add
it keep within array is called event
agrs
FYI, sender and e are not specific to ASP.NET or to C#. See Events (C# Programming Guide) and Events in Visual Basic.
I have Page_Load event on an .aspx markup page and a.aspx.cs code-behind file.
When running the application only the markup Page_Load event will fire, rather than both the markup and the code-behind methods. Why does the markup version fire rather than the code-behind version?
Example markdown:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
</script>
Example code behind:
public partial class WebForms : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
There's two aspects to this question:
Why does any event launch without any manual event registration?
Why does only the ASPX event launch, and not the code-behind?
The first is basically a backwards compatibility thing related to the way the old ASP works. To make sure it's not too hard to convert existing ASP applications to ASP.NET, the ASP.NET runtime examines all public and protected methods on the class representing the page for a known naming pattern - in this case, it knows that Page_Load should be automatically wired to the Page.Load event.
The second has to do with how inheritance works, and how ASP.NET exploits inheritance. The usual ASP.NET page you make consists of two files - aspx and aspx.cs (for C#). The C# file is a C# source file as any other - it simply defines a class that inherits from Page (by default). The ASPX file, on the other hand, serves as a template to auto-generate another C# file - which defines a class that inherits from the code-behind class.
Thus, in your sample, you have a method called Page_Load in the code-behind class, and another in the ASPX class. Since the method isn't virtual, the ASPX one simply hides the other method in the code-behind. When the ASP.NET runtime comes looking for event handlers to auto-wire, it only sees one Page_Load - the one in the ASPX.
One way to go around this would be to use this code in code-behind:
protected virtual Page_Load(object sender, EventArgs e)
{
Response.Write("I'm in code-behind!");
}
And this in ASPX:
protected override Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
Response.Write("I'm in ASPX!");
}
Again, the runtime will wire-up just one Page_Load method - but thanks to the use of base.Page_Load, we ensure the overriden method gets called as well (note that you don't even need the method to be virtual for this, but there's a lot of good reasons to do that anyway).
The Page_Load subroutine runs EVERY time the page is loaded. If you want to execute the code in the Page_Load subroutine only the FIRST time the page is loaded, you can use the Page.IsPostBack property. If the Page.IsPostBack property is false, the page is loaded for the first time, if it is true, the page is posted back to the server (i.e. from a button click on a form)
When I double click on a button (myButton) in Design view of a .aspx web form, an event handler is automatically generated in the code behind: protected void myButton_Click(object sender, EventArgs e)
Now if I understand correctly, in order to associate that method with the Button.Click event, somewhere there has to be something like: myButton.Click += new EventHandler(this.myButton_Click);
However, I can't seem to find that anywhere. I've used Ctrl+F for the entire solution and I've checked the mywebform.aspx.designer.cs.
At first I thought it was because the .aspx page's AutoEventWireup was set to true. However, even after making AutoEventWireup false, the button still responds to being clicked by running the code in protected void myButton_Click(object sender, EventArgs e)
I understand that you shouldn't mess with generated code, and I don't intend to, I just want to know more about how this is working under the hood.
The assignment of the event handler is actually done in the asp markup. Here's a link to a bunch of different properties that can be declaratively assigned to your button.
Here's another MSDN link about using the OnClick attribute.
I'm guessing that your ASP markup for the button has the following property assigned:
OnClick="myButton_Click"
As for how it gets translated into an assignment, the page gets compiled at runtime upon the first time it is requested (ASP.NET Compilation Overview).
Im designing a dialerPad form using Windows form, there is a textbox which should only take numbers and not text, my problem is that when i add the code
private void txtDialedNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//Blaah Blaah Code;
}
but its not getting registered in the other DialPad.Designer.CS page. For example the fallowing code registers TextChangedEvent
this.txtDailedNumber.TextChanged += new System.EventHandler(this.txtDailedNumber_TextChanged);
Can anybody help me on this?
You should never change *.designer.cs files manually.
What you should be doing is opening the design view of your form, selecting the object, and then setting the event handler in the objects properties:
Alternatively, if you want to register event handler manually (instead of using the designer), just put it under the InitializeComponent() call in the constructor for your form.
I would try to simplify Greg's answer.
Select the text box > go to properties > events tab> KeyPress Event > select the method this.txtDailedNumber.KeyPress
You are done.
I'm having a problem setting up an event on a form. Here's the setup:
I was given a page with two controls, two different versions of a form for the end-user to fill out- standard and advanced. The two forms have different code and javascript, so the original dev put them in separate web user controls. Aside from the controls is a LinkButton that switches to Advanced mode.
<uc1:Standard runat="server" ID="StandardForm" />
<uc1:Advanced runat="server" ID="AdvancedForm" />
<asp:LinkButton runat="server" ID="lnkAdvanced" Text="Go To Advanced" OnClick="lnkAdvanced_Click" />
lnkAdvanced_Click just takes all the info currently entered to the advanced and flips the Visible.
My problem is that one of the bosses wants the 'Go to Advanced' button inside the standard form, but the .Visible code is on the page. So I thought it could be done using an event, but it doesn't seem to be working.
I tried to set up the event like this:
public event EventHandler AdvanceClick;
protected void lnkAdvanced_Click(object sender, EventArgs e) {
AdvanceClick(sender, e);
}
And when that didn't work, I tried to set up a delegate:
public delegate void AdvancedEventHandler(object sender, EventArgs e);
public event AdvancedEventHandler AdvanceClick;
When I moved the button to the standard form, I expected to be able to type something like:
StandardForm.AdvanceClick += new AdvancedEventHandler(GoToAdvanced);
But it doesn't seem to recognize any events within the control! I get an error: "Standard does not contain a definition for 'AdvanceClick' and no extension method 'AdvanceClick accepting a first argument of type 'Standard' could be found" It finds the other properties just fine, am I going about this the wrong way?
// in your Standard user control
public event EventHandler AdvancedClick;
private void lbtnAdvanced_Click(object sender, EventArgs e)
{
OnAdvancedClick(e);
}
protected void OnAdvancedClick(EventArgs e)
{
if (AdvancedClick != null)
AdvancedClick(this, e);
}
// on your page
StandardForm.AdvancedClick += new EventHandler(StandardForm_AdvancedClick);
private void StandardForm_AdvancedClick(object sender, EventArgs e)
{
// toggle logic here
}
If the standard form has the "Switch to advanced" button. Then, clearly, it has to know about the Advanced form and thus they seem to be pretty tightly coupled. If this is the case, it seems to me that you might as well just have the advanced form as a child of the standard form then... or better yet, merge them into one control.
If you don't like these options you might want to create a third controls which hosts the button and the two forms, along with the logic to move data between them and toggle their visibility.
I personally recommend the single control option. Having tighly coupled controls usually just leads to confusion down the road. You could loosen up the dependency in various ways, but think hard about it before you do so.
In the legacy project I currently work on we have a bunch of examples such as serach forms and search results being split up into multiple controls, but then in the end needing each others instances to function properly. As I said earlier, I wont reccomend this path.
You shouldn't need the delegate because you've created a standard event.
Try in your form load or thereabouts:
StandardForm.AdvanceClick += new EventHandler(GoToAdvanced);
Then somewhere on the page that hosts the 2 user controls
protected void GoToAdvanced(object sender, EventArgs e)
{
//Code that was previously in lnkAdvanced_Click on page.
}
Edit:
It does sound like the setup is wrong.
Can you post the markup for the Host page (at this point we are assuming it is simply the 2 user controls).
Then we are also assuming that the AdvanceClick event is declared in the Standard UC but the error message would indicate that it doesn't.. and the lnkAdvanced_Click method is in the Standard UC?
Then we are assuming the code that is attempting to attach to the custom event is declared in the Host page.
If you could confirm or deny the assumptions i'm sure we could get this cleared up.