当前位置: 首页 > news >正文

asp.net webform组件和常见的html组件的使用

重写CheckBox
SimpleCheckBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public class SimpleCheckBox : CheckBox
    {
        protected override void Render(HtmlTextWriter writer)
        {
            // 添加基础属性
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);

            // 处理选中状态
            if (this.Checked)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
            }

            // 添加标题(ToolTip 映射到 title)
            if (!string.IsNullOrEmpty(this.ToolTip))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Title, this.ToolTip);
            }

            // 添加 CSS 类
            if (!string.IsNullOrEmpty(this.CssClass))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            }

            // 渲染 input 标签
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
        }
    }
}

重写RadioButton

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication1
{
    public class SimpleRadioButton: System.Web.UI.HtmlControls.HtmlInputRadioButton
    {
        public string ToolTip
        {
            get { return Attributes["title"]; }
            set { Attributes["title"] = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            // 确保基础属性(name、value、checked)正确渲染
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.Name);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Value);
            if (this.Checked)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
            }

            // 添加 ToolTip 到 title
            if (!string.IsNullOrEmpty(this.ToolTip))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Title, this.ToolTip);
            }

            // 渲染 input 标签
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
        }
    }
}

最终代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication1.Index" %>

<%@ Register TagPrefix="custom" Namespace="WebApplication1" Assembly="WebApplication1" %>

<!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">
        <h1>checkbox</h1>
        <%--checkbox--%>
        <input type="checkbox" name="sex" value="0" title="" checked="checked" />
        <input type="checkbox" name="sex" value="1" title="" />
        <%--Text会渲染lable标签,ToolTip会渲染span标签--%>
        <asp:CheckBox runat="server" Text="" Checked="true" />
        <asp:CheckBox runat="server" Text="" ToolTip="" />
        <%--为了提交表单之后还回显保存的数据,需要添加runat="server"--%>
        <input runat="server" type="checkbox" name="sex2" value="0" title="" checked="checked" />
        <input runat="server" type="checkbox" name="sex2" value="1" title="" />
        <%--自定义checkbox--%>
        <custom:SimpleCheckBox
            ID="sex_man"
            runat="server"
            Checked="true"
            ToolTip="" />
        <custom:SimpleCheckBox
            ID="sex_woman"
            runat="server"
            ToolTip="" />
        <h1>Radio</h1>
        <%--radio--%>
        <input type="radio" name="sex11" value="1" title="" checked="checked" />
        <input type="radio" name="sex11" value="2" title="" />
        <%--Text会渲染lable标签,ToolTip会渲染span标签--%>
        <asp:RadioButton runat="server" ID="sex_man_radio" Text="" ToolTip="" GroupName="sex12" Checked="true" />
        <asp:RadioButton runat="server" ID="sex_woman_radio" Text="" ToolTip="" GroupName="sex12" />
        <%--为了提交表单之后还回显保存的数据,需要添加runat="server"--%>
        <input runat="server" id="Radio1" type="radio" name="sex13" value="1" title="" checked="true" />
        <input runat="server" id="Radio2" type="radio" name="sex13" value="2" title="" />
        <%--自定义radio--%>
        <!---->
        <custom:SimpleRadioButton
            ID="SimpleRadioButton1"
            runat="server"
            Name="sex14"
            Value="1"
            ToolTip=""
            Checked="true">
        </custom:SimpleRadioButton>
        <custom:SimpleRadioButton
            ID="SimpleRadioButton2"
            runat="server"
            Name="sex14"
            Value="2"
            ToolTip="">
        </custom:SimpleRadioButton>
        <h1>Input</h1>
        <input type="text" value="" placeholder="请输入姓名" class="aaa" />
        <asp:TextBox runat="server" ID="inputBox1" CssClass="aaa" Text="" placeholder="请输入姓名" />
        <input type="text" runat="server" value="" placeholder="请输入姓名" class="aaa" />
        <h1>TextArea</h1>
        <textarea class="aaa" placeholder="请输入姓名"></textarea>
        <asp:TextBox TextMode="MultiLine" runat="server" ID="textarea1" CssClass="aaa" Text="" placeholder="请输入姓名" />
        <textarea runat="server" class="aaa" placeholder="请输入姓名"></textarea>
        <h1>Select</h1>
        <select name="select01">
            <option value="0">请选择</option>
            <option value="1">语文</option>
            <option value="2">英语</option>
            <option value="3">数学</option>
        </select>
        <asp:DropDownList runat="server" ID="select02">
            <asp:ListItem Value="0" Text="请选择"></asp:ListItem>
            <asp:ListItem Value="1" Text="语文"></asp:ListItem>
            <asp:ListItem Value="2" Text="英语"></asp:ListItem>
            <asp:ListItem Value="3" Text="数学"></asp:ListItem>
        </asp:DropDownList>

        <select name="select01" runat="server">
            <option value="0">请选择</option>
            <option value="1">语文</option>
            <option value="2">英语</option>
            <option value="3">数学</option>
        </select>
        <div>
            <asp:Button runat="server" Text="提交" ID="submit" OnClick="submit_Click" />
        </div>

    </form>
</body>
</html>

在这里插入图片描述

总结:原生的html提交之后会重置,带着runat的组件提交之后asp.net会重新选择赋值

相关文章:

  • 【设计模式】】工厂模式
  • R 语言科研绘图 --- 密度图-汇总
  • Spring Boot整合Sa-Token极简指南
  • C++ QT零基础教学(二)
  • 六种最新优化算法(TOC、MSO、AE、DOA、GOA、OX)求解多个无人机协同路径规划(可以自定义无人机数量及起始点),MATLAB代码
  • 算法专题一:双指针
  • 基于eNSP的IPV4和IPV6企业网络规划
  • Mac电脑python 有没有ros接口 查看lidar的数据
  • Vue配置和安装教程(2025最新)
  • 小米路由器SSH下安装DDNS-GO
  • Qt 控件概述 QPushButton 与 QRadioButton
  • JDBC技术基础
  • DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之添加列宽调整功能,示例Table14_09自定义单元格的固定表头表格
  • 数据结构------线性表(顺序表)
  • CAD球体密堆积3D插件V2.0
  • 【C++】STL全面简介与string类的使用(万字解析)
  • 大盗阿福-选和不选思路:状态数组st写法-->新写法DFS-->记忆化-->递推
  • 初一信息科技教程专用抓包软件1.4.2版本
  • 【经验分享】SpringBoot集成WebSocket开发02 之 实现一个基本示例并Spring Bean注入的方式来组织代码
  • 在浏览器中配置vue请求后端的接口地址
  • 每天少看1小时手机,就可能有神奇效果
  • 甘肃多地发生旱情,三大骨干工程已累计调水2.45亿立方米
  • 国家发改委:正在会同有关方面,加快构建统一规范、协同共享、科学高效的信用修复制度
  • 这个东西每道菜里都有,却可能让你得一身病,做好这些能避免
  • 去年上海60岁及以上户籍老年人口占总人口的37.6%
  • 慢品巴陵,看总编辑眼中的岳阳如何书写“山水人文答卷”