How to Add Event Handler and Logical Code in ASP.NET
Description

Learn how to add Event Handler and Logical Code in ASP.NET.
Transcript
Now then, let us just think about the vertical alignment for this table row just like we did before. I want to specify vertical alignment top. So, let us select the “tr” element on here. Select tag and then in the properties window, we can hopefully set the VAlign to be top. Okay, what is the next? Next step, we want to add a Result Label in here. So, let us select the table row and then once you select the table row, we can merge the cells so that we have one big cell that expands five columns and then in there, I am going to drag a label control in asp label and we will set some properties in there. Let us set the ID to be “Result Label.” Let us also set the text. We will set the text to be “Result will be displayed here”.
Okay. Now if I wanted to, I could right click on that control and I could set the styles so I’ll just click that. And this is the Style Builder dialogue box that we saw earlier when we look at HTML controls and I could use the same approach if I wanted to here, to set the visual appearance for my web control and that is fine. I am not going to do that. I will cancel the window and instead we will come back to the properties window in the bottom corner and you can see that we have higher level controls that you can set individually, so this gives you a richer programming model. You do not have to set styles at the HTML level. What we do in here is we are setting properties on that .net class and it will in turn render the appropriate style attribute in the HTML when it gets return back to the browser. So for example, let us set the background color to be a web color and we will go for, let me see dark orange appeals to me and we will set the font properties. We will set the foreground color for the font to be, how about maroon? Could it work? And finally on the font item, we will set the font names to be, well let us choose some fonts. We can choose Arial because I preferred that font and then I will press enter and then we could choose Verdana as our next preferred font and then finally we could choose San-Serif as the full back front. I will click Ok.
So, that is the different way for setting attributes on the asp label. We use high level properties instead of setting the low level styles like we did with the HTML control, very good!
Last thing we need to do is to add the button so, let us add the button onto our web page and drag it there. We will set the ID of the button to be “Submit Button”. And we will set the text to be “Submit” okay and we are done. Now then, let us think about how we are going to handle the click event on this button. If you recall how we handled it on the HTML button, we had to manually go into the Source View and then we have to add in here the On Server Click event. And we have to manually then go into the code; type in a method with the correct signature and that was a quite a time consuming error-prone process. We do not need to do any of that when we are using web controls because it is a web control, it has a high level event that I can access via the property window. So, let us just do that. Let us go back to the design view, click our button and then over in the property window, here is our lightning bolt which shows the events for the control.
Now, you can handle any of these events if you want to. Most often, you will just handle a click event. If you double click on the event name, it will add a method with a default name such as Submitbutton_Click. So, let us do that. I am going to double click an event name and it has added the event handler method here. So, all we need to do now is to write some code to gather the users data and display it in the Result label. Okay, so let us do that. I am going to declare a string variable called “Name” and that is going to capture the name entered in the NameTextbox. And we will also capture the user’s age. So, this is by similar to before but if you remember, when we are dealing with HTML controls, the property that we are accessing was called value but now that we are using asp.net text boxes, the value of the property that we are
Related Articles
How to Add Event Handler and Logical Code in ASP.NET
In this tutorial we will show a simple way to create localization (globalization) for web application using APS.NET MVC framework. It should work fine with MVC 1 and 2 and we\'re currently using .NET 3.5 SP1, but .NET 4.0 will work as well. All code is in C# and for language translations we use XML files....
ASP.NET is basically a combination of Active Server Pages along with .net framework. It actually provides the best and the easiest way to build dynamic web applications along with great scalable features. Additionally, very little code is needed with classic ASP....
You can develop reports through Visual Studio. If you are adept in using the asp.net, you can learn more about reporting for Visual Studio in this article....
Microsoft released Security Bulletin MS10-070 out-of-band today–a couple weeks ahead of the regularly scheduled Patch Tuesday for October. The update resolves a zero-day issue with ASP.NET that could allow an attacker to compromise information on all supported versions of Windows....
In this article, we will discuss how to upgrade an ASP.NET web app to ASP.NET 4.0. Please read all the article to understand more....
ASP.Net Programming is the fastest growing web development platform in the world today. Asp.net web applications are faster and provide better performance to your web applications....
Microsoft''s Asp.Net technology is a technology proposed to connect. What to connect? Well the whole thing that can be linked through software, people, structures, computers and extra things as well a......
A website is the face of any organization in today''s digital age. Your website is the first point of contact for your customers who are looking for information or want to place an order with you. An ......
ASP stands for Active Server Pages. ASP.NET is the next generation ASP, but it’s not an upgraded version of ASP. ASP.NET is an entirely new technology for server-side scripting that enables scripts (embedded in web pages) to be executed by an Internet server. It is a part of the Microsoft .NET framework, and a powerful tool for creating dynamic and interactive web pages. ASP.Net benefits over ot...
ASP.NET is a web development technology that is built into the .NET Framework and uses a full featured programming language such as C# (pronounced C-Sharp) or VB.NET to build web applications easily. It is a web application & development technology commercialized by Microsoft that programmers can use to build dynamic websites, web applications & web services ASP.NET leverages the power of...