C# runat="server"

本文详细介绍了在ASP.NET中使用runat属性时的注意事项,包括如何正确地使用<%=str%>输出变量,以及如何将HTML元素转换为服务器控件。文章还解释了不同上下文中runat属性的行为差异。
在runat=”server”下的标签,如果可以转换成HtmlControl,那么它的Attribute将不能使用<%=str%>的方式输出,如果不能转换成HtmlControl,则没有具体要求。如果一定要使用<%=str%>的方式,则需要将其以及它的祖先节点上的runat=”server”去掉即可。但是,关于一个嵌套的结构是否会被自动提升为runat=”server”则是根据标准来制定的。比如将link标签放在head中,设置head为runat=”server”,则link会被转换成HtmlLink,但是将其放在<form runat=”server”>下则只会被当作文本输出。而在form下的控件则不会进行自动提升,如<form runat=”server”><input type=”button” /></form>则button将继续以文本的方式输出,遇到<%=str%>将被转换成__w.Write(str);。如果需要将其提升为HtmlInputButton控件,将显示指定其为<input runat=”server” type=”button” />。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>用户注册</title> <style type="text/css"> .error { color: red; } </style> </head> <body> <form id="form1" runat="server"> <div> <label for="txtName">姓名:</label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="用户必须填写" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator> </div> <div> <label for="txtPassword">密码:</label> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPassword" ValidationExpression="^.{8,}$" ErrorMessage="密码必须八位以上" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator> </div> <div> <label for="txtConfirmPassword">确认密码:</label> <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtConfirmPassword" ControlToCompare="txtPassword" ErrorMessage="两次密码必须一致" ForeColor="Red" Display="Dynamic"></asp:CompareValidator> </div> <div> <label for="txtPhone">手机号:</label> <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtPhone" ValidationExpression="^1[35789][0-35-9]\d{8}$" ErrorMessage="手机号格式不正确" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator> </div> <div> <label>请修正以下错误:</label> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="true" ShowMessageBox="false" HeaderText="" CssClass="error"></asp:ValidationSummary> </div> <div> <asp:Button ID="btnRegister" runat="server" Text="注册" OnClick="btnRegister_Click" /> </div> </form> </body> </html>
05-12
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="datasource2.aspx.cs" Inherits="WebApplication3.datasource2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2>商品信息管理</h2> <asp:Label ID="lblProduct" runat="server" Text="选择商品:"></asp:Label> <asp:DropDownList ID="ddlProducts" runat="server" AutoPostBack="True" DataTextField="Name" DataValueField="ProductId" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged"> </asp:DropDownList> <br /><br /> <asp:Label ID="lblCategory" runat="server" Text="分类:"></asp:Label> <asp:DropDownList ID="ddlCategories" runat="server" DataTextField="Name" DataValueField="CategoryId"></asp:DropDownList> <br /><br /> <asp:Label ID="lblListPrice" runat="server" Text="价格:"></asp:Label> <asp:TextBox ID="txtListPrice" runat="server"></asp:TextBox> <br /><br /> <asp:Label ID="lblUnitCost" runat="server" Text="单价:"></asp:Label> <asp:TextBox ID="txtUnitCost" runat="server"></asp:TextBox> <br /><br /> <asp:Label ID="lblSupplier" runat="server" Text="供应商:"></asp:Label> <asp:DropDownList ID="ddlSuppliers" runat="server" DataTextField="Name" DataValueField="SuppId"></asp:DropDownList> <br /><br /> <asp:Label ID="lblName" runat="server" Text="商品名称:"></asp:Label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br /><br /> <asp:Label ID="lblDescn" runat="server" Text="商品简介:"></asp:Label> <asp:TextBox ID="txtDescn" runat="server" TextMode="MultiLine"></asp:TextBox> <br /><br /> <asp:Label ID="lblImage" runat="server" Text="图片:"></asp:Label> <asp:FileUpload ID="fuImage" runat="server" /> <br /><br /> <asp:Label ID="lblQty" runat="server" Text="数量:"></asp:Label> <asp:TextBox ID="txtQty" runat="server"></asp:TextBox> <br /><br /> <asp:Label ID="lblDataSource" runat="server" Text="利用数据源控件:"></asp:Label> <asp:Button ID="btnAdd" runat="server" Text="添加商品" OnClick="btnAdd_Click" /> <asp:Button ID="btnUpdate" runat="server" Text="修改商品" OnClick="btnUpdate_Click" /> <asp:Button ID="btnDelete" runat="server" Text="删除商品" OnClick="btnDelete_Click" /> <!-- 数据源控件 --> <asp:SqlDataSource ID="sqlDSProducts" runat="server" ConnectionString="<%$ ConnectionStrings:YourConnectionString %>" SelectCommand="SELECT * FROM [Product]" InsertCommand="INSERT INTO [Product] ([CategoryId], [ListPrice], [UnitCost], [SuppId], [Name], [Descn], [Image], [Qty]) VALUES (@CategoryId, @ListPrice, @UnitCost, @SuppId, @Name, @Descn, @Image, @Qty)" UpdateCommand="UPDATE [Product] SET [CategoryId] = @CategoryId, [ListPrice] = @ListPrice, [UnitCost] = @UnitCost, [SuppId] = @SuppId, [Name] = @Name, [Descn] = @Descn, [Image] = @Image, [Qty] = @Qty WHERE [ProductId] = @ProductId" DeleteCommand="DELETE FROM [Product] WHERE [ProductId] = @ProductId"> <InsertParameters> <asp:ControlParameter Name="CategoryId" ControlID="ddlCategories" PropertyName="SelectedValue" Type="Int32" /> <asp:ControlParameter Name="ListPrice" ControlID="txtListPrice" PropertyName="Text" Type="Decimal" /> <asp:ControlParameter Name="UnitCost" ControlID="txtUnitCost" PropertyName="Text" Type="Decimal" /> <asp:ControlParameter Name="SuppId" ControlID="ddlSuppliers" PropertyName="SelectedValue" Type="Int32" /> <asp:ControlParameter Name="Name" ControlID="txtName" PropertyName="Text" Type="String" /> <asp:ControlParameter Name="Descn" ControlID="txtDescn" PropertyName="Text" Type="String" /> <asp:ControlParameter Name="Image" ControlID="fuImage" PropertyName="FileName" Type="String" /> <asp:ControlParameter Name="Qty" ControlID="txtQty" PropertyName="Text" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:ControlParameter Name="ProductId" ControlID="ddlProducts" PropertyName="SelectedValue" Type="Int32" /> <asp:ControlParameter Name="CategoryId" ControlID="ddlCategories" PropertyName="SelectedValue" Type="Int32" /> <asp:ControlParameter Name="ListPrice" ControlID="txtListPrice" PropertyName="Text" Type="Decimal" /> <asp:ControlParameter Name="UnitCost" ControlID="txtUnitCost" PropertyName="Text" Type="Decimal" /> <asp:ControlParameter Name="SuppId" ControlID="ddlSuppliers" PropertyName="SelectedValue" Type="Int32" /> <asp:ControlParameter Name="Name" ControlID="txtName" PropertyName="Text" Type="String" /> <asp:ControlParameter Name="Descn" ControlID="txtDescn" PropertyName="Text" Type="String" /> <asp:ControlParameter Name="Image" ControlID="fuImage" PropertyName="FileName" Type="String" /> <asp:ControlParameter Name="Qty" ControlID="txtQty" PropertyName="Text" Type="Int32" /> </UpdateParameters> <DeleteParameters> <asp:ControlParameter Name="ProductId" ControlID="ddlProducts" PropertyName="SelectedValue" Type="Int32" /> </DeleteParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="sqlDSCategories" runat="server" ConnectionString="<%$ ConnectionStrings:YourConnectionString %>" SelectCommand="SELECT [CategoryId], [Name] FROM [Category]"> </asp:SqlDataSource> <asp:SqlDataSource ID="sqlDSSuppliers" runat="server" ConnectionString="<%$ ConnectionStrings:YourConnectionString %>" SelectCommand="SELECT [SuppId], [Name] FROM [Supplier]"> </asp:SqlDataSource> </div> </form> </body> </html> 请对上述内容进行重写,并给出相应的cs代码。服务器为LAPTOP-8S143JCT,数据库为test5
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值