Introduction:
ASP.NET is a web application framework developed by Microsoft that allows developers to build dynamic websites and applications. One of the key components of ASP.NET are server controls which help in building user interfaces for web pages. In this article, we will explore some popular server controls available in ASP.NET and how they can be used with C#.
1、Button Control:
The Button control is one of the most commonly used server controls in ASP.NET. It enables users to perform actions such as submitting forms or navigating between pages. The Button control has various properties like Text, CommandName, CommandArgument etc., which allow you to customize its appearance and behavior.
图片来源于网络,如有侵权联系删除
Example code snippet:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Button Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server"/> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void btnSubmit_Click(object sender, EventArgs e) { // Handle button click event here Response.Write("Button clicked!"); } }
2、Label Control:
The Label control displays static text on your webpage. You can set its Text property to display any desired message using C#. Additionally, there are other properties like Font-Bold, ForeColor etc., allowing you to modify font styling options.
Example code snippet:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Label Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMessage" Text="Hello, World!" Font-Bold="True" ForeColor="Blue" runat="server"/> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Set label text dynamically if required lblMessage.Text = "Welcome to my website!"; } }
3、DropDownList Control:
The DropDownList control provides a list of items from which users can select an option while interacting with your webpage. This control helps organize related data into categories making it easier for users to find what they're looking for quickly.
图片来源于网络,如有侵权联系删除
Example code snippet:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>DropDownList Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlCountries" DataSourceID="SqlDataSource1" DataTextField="CountryName" DataValueField="CountryCode" runat="server"/> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e) { // Retrieve selected country code when dropdownlist item changes string countryCode = ddlCountries.SelectedValue; Response.Write($"Selected Country Code: {countryCode}"); } }
4、GridView Control:
The GridView control is widely used for displaying tabular data within your webpage efficiently. It supports sorting, paging functionality along with editing rows directly through client-side scripts without requiring postbacks each time.
Example code snippet:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>GridView Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="gvEmployees" DataSourceID="SqlDataSource1" AutoGenerateColumns
标签: #asp.net 服务器控件 c
评论列表