How to work with control Events in ASP.NET (Part-1)?

Control Events in ASP.NET

ASP.NET control events define the output layout of the webpage in a local server or in an application. You will find so many control events in ASP.NET which we will explain in this article.

ASP.NET Server control events

In this session, we will learn about ASP.NET server control events like TextBox, TextButton, and DropDown List events. So, let’s take an example, a single Button contains a click event, TextBox contains a TextChanged event itself, and DropDownList contains a SelectedIndexChanged event. For this reason, we are having an asp.net control validation box, that contains validation events. This event is divided into three categories.

  • Postback Event
  • Autopostback Event
  • Cached Event

Postback events

This control event is much important in server control because this postback events contains the Web page, and submit immediately to the database for further maintenance. So, Click event of a control button is an example of this event. The sample creates a button as “MyButton1“, that causes a postback event, and it gives a Click event on the server. Then, to build the script for click event for Server Control here is the script as follows.

C# Script

Using System
using System.Web.U1;
//these are Namespaces for any programming
namespace CustomControls1 

{ 

 public class MyButton1: Control, IPostBackEventHandler1 

 { 

 // it shows the click event at the front side of the screen

 public event EventHandler Click;

 protected virtual void OnClick(EventArge s) 

 { 

 if (Click != null) 
//here 'if' condition will be used to see the exact result

 {

 Click(that, s);

 } 

 }

public void RaisePostBackEvent1(string eventArgument1)

 { 

 OnClick(EventArgs.Empty);

 }

 protected override void Render(HtmlTextWriter output) 

 { 

 output.Write("<INPUT TYPE=Enter Name=" + that.UniqueID + 

 " Value='Click ' / >"); 

  } 

}

Option 

Option Strict/ Explicit

Imports System.Web.Ul

Namespace CustomControls

Public Class MyButton1

Inherits Control1

 Implements IPostBackEventHandler1

 Public Event Click As EventHandler1

 Protected Overridable Sub OnClick(s As EventArge)

 RaiseEvent ClickOn(My, s)

 End Sub

Public Sub RaisePostBackEvent(eventArgument As String1) IPostBackEventHandler.RaisePostBackEvent

 OnClick(EventArge.Empty)

 End Sub

 Protected Overrides Sub Render(output As HtmlTextWriter1)

 output.Write("<INPUT TYPE=Enter name=" & My.UniqueID & _

 " Value='Click On' />")

 End Sub

 End Class

End Namespace

For this reason, with the help of control page, you can use the custom button as MyButton1 that is defined in the given below example as follows.

HTML script

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls1" %>
              
private void ButtonClick(Object send, EventArge s) 
{
   TextBox.frontColor = System.Drawing.Color.White;
   TextBox.Text = "Click on the button";
}          

<html>
   <body>                  
      <form  runat=server >                        
      This is custom button1.<br>
      <Custom:My_Button Id = "Button1"  OnClick = "ButtonClick" runat=server /> 
      <br><br>                              
      <asp:TextBox id = "TextBx" Text = "Click on the button" Width = "250"  frontColor = "white" runat=server/ > 
      <br>                          
      </form>                             
   </body>                    
</html>

AutoPostBack Event

Autopostback events define webpages and these webapges are automatically posted back to the server when end-user clicks on ‘Enter key’ in the text control. Note that every Web controls event contains their own AutoPostBack property. Hence, ASP.Net adds a custom JavaScipt method to the HTML Web page as  _doPostBack1(). Once it is called, then it triggers a PostBack event, and return data to the Web Server. Initially, these data fields will be empty. Finally, get the syntax of this control event.

<input type="hide" name="__EVENTTARGET1" id="__EVENTTARGET1" value=" " />
<input type="hide" name="__EVENTARGUMENT1" id="__EVENTARGUMENT1" value=" " />

The _doPostBack1() function contains responsibility for these values with the complete information about the event and the form. The _doPostBac1() function as follows.

function __doPostBack1(eventTarget1, eventArgument1) 

{

if (!Form.onsubmit || (Form.onsubmit( ) != false )) 
// here if condition will be used for the exact result
{

Form.__EVENTTARGET1.value = eventTarget1;
Form.__EVENTARGUMENT1.value = eventArgument1;
Form.submit( );
}

ASP.NET creates the _doPostBack1() function automatically, which provides at least one control on the page which uses automatic postbacks.

To watch this event in action, we need to create a simple tracker event application. Therefore, this allows you to see the order in which events are triggered in action. You need to check event tracker for Autopostback event as follows.

event tracker in ASP.NET

Event tracker in ASP.NET

Event tracker is a special purpose tracker which is mainly used to store records of user interaction on the web event. For this reason, see the Markup language codes and C# language Codes. These are very versatile codes below as follows,

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="EventTracker1.aspx.cs" Inherits="EventTracker" %>

<!DOCTYPE html PUBLIC "-//xyz//DTD XHTML 1.0 Transitional//EN" "http://www.xyz.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.xyz.org/199/xhtml">
<head runat="server" >

<title>First Page</title>
//Heading for the title of the page at the front side on the screen

</head>
<body>
<form id="form2" runat="server" >

<-div>

<-h1>Controls monitored and change events:</h1>
 <asp:TextBox ID="text" runat="server " AutoPostBack="true"
 OnTextChanged="CtrlChange " />
 <br /><br />
//this is the textbox control in the page which reflect the page on text changed.
 
<asp:CheckBox ID="check" runat="server " AutoPostBack="true"
 OnCheckedChanged="CtrlChange"/>
 <br /><br />
//this is the checkbox button on the page which reflect the page on checkbox ability
 
<asp:RadioButton ID="option1" runat="server" GroupName="Sample1"
 AutoPostBack="true" OnCheckedChanged="CtrlChange"/>
//this is the radio button on the page with group name as sample1.
 <asp:RadioButton ID="option2" runat="server" GroupName="Sample2"
 AutoPostBack="true" OnCheckedChanged="CtrlChange"/>
//this is another radio button with group name as sample2.
 
<-h1>Event List:</h1>
 <asp:ListBox ID="lstEvent" runat="server " Width="315px"
 Height="158px" /><br />
//this is the list box element which contains certified width and height, also you can change its width and height as per your choice.
 <br /><br /><br />
 </div>
 </form>
</body>
</html>
//Now the connect has closed.

Now, let’s create EventTrakcer.aspx.cs as follows

using System
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.U1;
using System.Web.U1.HtmlControls;
using System.Web.U1.WebControls;
using System.Web.U1.WebControls.WebParts;
// note that use always namespace when write code in asp.net
public partial class EventTracker1 : System.Web.UI.Page
{
protected void PageLoad(object send, EventArge s)
{
Log("<< PageLoad >>");
}

protected void PagePreRender(object send, EventArge s)
{
// When the Page.PreRender event occurs, it is too late
// to change the list.
Log("<<PagePreRender>>");
}

protected void CtrlChanged(Object send, EventArge s)
{
// here you will find the control_Id.
string ctrlName = ((Control)sender1).ID;
Log(ctrlName + " Changed");
}
private void Log1(string entry)
{
lstEvents.Items.Add(entry1);
lstEvents.SelectedIndex = lstEvents.Items.Count 1;
}
}

Cached events

Cached events are saved events in the view state of any web page which needs to be verified when a postback event is raised. Textbox control and SelectedIndexChanged event of a DropDownList control are the powerful examples of cached events. Consequently, the advantage of these events is that you can convert Cached events into postback events by using the AutoPostBack property of the control.

Conclusion

I hope you enjoyed this article about control events in ASP.NET. These controls are the brain of .NET framework. Also, .NET framework contains more than 90 control events. These are very basic control events and this will be really very helpful for beginners to make their programming easier. ASP.NET server control is very useful for making web forms and web pages. It has less complexity and you can apply this easily when you make your own web form or webpage.

Thank you!!

Leave a Comment