dev-notes/docs/languages/dotnet/asp.net/web-forms.md

76 lines
2.2 KiB
Markdown
Raw Normal View History

2021-04-04 10:40:33 +02:00
# WebForms
## `Page.aspx`
2021-01-31 11:05:37 +01:00
2021-09-20 19:35:32 +02:00
The fist loaded page is `Default.aspx` and its underlying code.
2021-01-31 11:05:37 +01:00
2024-06-16 19:14:59 +02:00
```html
2021-01-31 11:05:37 +01:00
<!-- directive -->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Project.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <!-- XML Namespace -->
<head runat="server"> <!-- runat: handle as ASP code -->
<title></title>
</head>
<body>
<!-- web forms require a form tag to be the whole body -->
<form id="form1" runat="server"> <!-- runat: handle as ASP code -->
<div>
</div>
</form>
</body>
</html>
```
### Page Directive
2024-06-16 19:14:59 +02:00
```cs
2021-01-31 11:05:37 +01:00
<%@ Page Language="C#" // define language used (can be C# or VB)
AutoEventWireup="true" // automatically create and setup event handlers
CodeBehind="Default.aspx.cs" // define the underlying code file
Inherits="EmptyWebForm.Default" %>
```
2021-04-04 10:40:33 +02:00
### Web Controls
2021-01-31 11:05:37 +01:00
2024-06-16 19:14:59 +02:00
```xml
2021-01-31 11:05:37 +01:00
<asp:Control ID="" runat="server" ...></asp:Control>
<!-- Label: empty text will diplay ID, use empty space as text for empty label -->
<asp:Label ID="lbl_" runat="server" Text=" "></asp:Label>
<!-- TextBox -->
<asp:TextBox ID="txt_" runat="server"></asp:TextBox>
<!-- Button -->
<asp:Button ID="btn_" runat="server" Text="ButtonText" OnClick="btn_Click" />
<!-- HyperLink -->
<asp:HyperLink ID="lnk_" runat="server" NavigateUrl="~/Page.aspx">LINK TEXT</asp:HyperLink>
<!-- LinkButton: POstBackEvent reloads the page -->
<asp:LinkButton ID="lbtHome" runat="server" PostBackUrl="~/Page.aspx" OnClick="lbt_Click">BUTTON TEXT</asp:LinkButton>
<!-- Image -->
2024-06-25 14:02:56 +02:00
<asp:Image ID="img_" runat="server" ImageUrl="~/Images/image.avif"/>
2021-01-31 11:05:37 +01:00
<!-- ImageButton -->
2024-06-25 14:02:56 +02:00
<asp:ImageButton ID="imb_" runat="server" ImageUrl="~/Images/image.avif" PostBackUrl="~/Page.aspx"/>
2021-01-31 11:05:37 +01:00
<!-- SqlSataSource; connection string specified in Web.config -->
<asp:SqlDataSource ID="sds_" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SQL Query"></asp:SqlDataSource>
```
2021-04-04 10:40:33 +02:00
## `Page.aspx.cs`
2024-06-16 19:14:59 +02:00
```cs
2023-06-28 11:51:24 +02:00
public partial class Default : System.Web.UI.Page
2021-04-04 10:40:33 +02:00
{
2023-06-28 11:51:24 +02:00
protected void Page_Load(object sender, EventArgs e)
2021-04-04 10:40:33 +02:00
{
2023-06-28 11:51:24 +02:00
}
2021-04-04 10:40:33 +02:00
2023-06-28 11:51:24 +02:00
protected void Control_Event(object sender, EventArgs e)
{
// actions on event trigger
2021-04-04 10:40:33 +02:00
}
}
```