As mentioned on the "An Introduction" page of this series, ASP.NET doesn't re-invent the wheel when it comes to
web development. Web pages are still constructed using HTML tags and styled using CSS. ASP.NET simply introduces
.NET conventions and the C# programming language as an alternate to something like JavaScript, for being able to
dynamically create, modify, or remove web content, based off of user actions. Which, up to this point, we've
referred to as
Events during our Windows Form project executions. This will continue
to be true for these web projects.
We first need to indicate that we intend on using ASP.NET at all, as well as how we would like to use it. We do
this through a series of
directives, typically defined right at the top of our source code. One
such directive would be to indicate the language we're using (C#).
<%@ Application Language="C#" %>
Let's take a look at how we might code a HTML button that clears a form meant to capture credit card information.
<button id="ClearCC_Button" onclick="Clear_CCForm()">Clear</buton>
"id" is an attribute we use to help uniquely identify containers within a webpage. "onclick" is an
EventHandler functionally identical to our
Click from Windows Form projects. "Clear"
is the expected label to be displayed for this button. Clicking it would trigger the JavaScript method (that takes
no arguments) called "Clear_CCForm", which no doubt would include a series of instructions that look like this:
document.getElementById("CCForm_firstName").value = "";
For each input field of the form. The ASP.NET implementation of this is similar.
<asp:Button id="ClearCC_Button" runat="server" onclick="Clear_CCForm" Text="Clear" />
(Note the missing parentheses of the
EventHandler specified) Any ASP.NET container we use
will typically include the
runat="server" attribute. Here's how one of the
TextBox containers would be used:
<asp:TextBox id="CCForm_firstName" runat="server" />
And this is how the
EventHandler would be coded:
public void Clear_CCForm(object sender, EventArgs e)
{
CCForm_firstName.Text = "";
// Other such Text attribute assignments
}
Looks familiar, eh?
From here, you might see how you can translate your Windows Form projects to ASP.NET webpages, although the loss of a
convenient Designer view of VisualStudios may need to be compensated for.