当前位置:网站首页>C#设置Textbox控件不可编辑
C#设置Textbox控件不可编辑
2022-07-28 02:22:00 【黄昏和星空】
前言
一般而言,Textbox中有两个属性可以对其进行防止编辑的设定,这是最基础的知识,也是我要提出的前两种方法。而后两种方法实际为一种,但可以应用于不同环境中。
一、ReadOnly属性
这样设置,Textbox控件则限制不能输入,但可以读取已有文本,样式也与正常使用的Textbox一致。
1、在前端设置Readonly为true,样式如下:
<asp:TextBox ID=“TextBox1” runat=“server” ReadOnly=“true” Text=“测试”></asp:TextBox>
2、在后台设置Readonly为true,样式如下:
TextBox1.ReadOnly = true;
二、Enabled属性
设置此属性会将Textbox控件变成反灰样式,不可点击。
1、在前端设置Enabled为false,样式如下:
<asp:TextBox ID=“TextBox1” runat=“server” Enabled=“false” Text=“测试”></asp:TextBox>
2、在后台设置Readonly为true,样式如下:
TextBox1.Enabled= false;
三、onfocus方法
设置JS的事件,onfocus事件指的的是光标移到到此控件,this.blur()是将光标移开,如此便能够达到不能编辑的目的,代码如下。
<asp:TextBox ID=“TextBox1” runat=“server” Text=“测试” οnfοcus=“this.blur();”></asp:TextBox>
四、设置TextModel样式
使用CSS代码,设置TextModel属性。与第三项结合,这样设置的好处是在一些浏览器中同样能实现不能编辑的目的,但是能够保证原有的"X"号存在,所以还可以实现清楚控件内容的目的。经检测,在IE浏览器不支持此功能,但Google浏览器可以。
前端代码如下:
<asp:TextBox ID=“TextBox1” runat=“server” Text=“测试” TextModel=“search” οnfοcus=“this.blur();”></asp:TextBox>
Css样式如下:
边栏推荐
- Intelligent industrial design software company Tianfu C round financing of hundreds of millions of yuan
- ELS keyboard information
- exness:日本物价上涨收入下降,英镑/日元突破 165
- JS 事件对象2 e.charcode字符码 e.keyCode键码 盒子上下左右移动
- 决策树与随机森林学习笔记(1)
- Brush questions every day to consolidate knowledge
- JVM memory layout detailed, illustrated, well written!
- 优炫数据库客户端如何认证
- TFX airflow experience
- Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
猜你喜欢
随机推荐
阿里云国际版邮件服务套餐购买流程
Record of a cross domain problem
Oracle basicfile lob field space recycling shrink space doubts
智能工业设计软件公司天洑C轮数亿元融资
Web server
"29 years old, general function test, how do I get five offers in a week?"
els 显示一个随机方块
els 键盘信息
JVM memory layout detailed, illustrated, well written!
没法预测明天的涨跌
方案分享 | 高手云集 共同探索重口音AI语音识别
嵌入式开发:提示和技巧——用C进行防御性编程的最佳实践
Comprehensive case
Vscode debug displays multiple columns of data
R 笔记 MICE
Scheme sharing | experts gather to jointly explore accent AI speech recognition
Opengauss Developer Day 2022 sincerely invites you to visit the "database kernel SQL Engine sub forum" of Yunhe enmo
Gbase8s how to delete data in a table with a foreign key relationship
Niuke-top101-bm340
【红队】ATT&CK - 文件隐藏









