当前位置:网站首页>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样式如下:
边栏推荐
猜你喜欢

QFileDevice、QFile、QSaveFile、QTemporaryFile

Day 19 of leetcode

What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?

Original title of Blue Bridge Cup

Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough

Using pytorch's tensorboard visual deep learning indicators | pytorch series (25)

数据湖:海量日志采集引擎Flume

Full of dry goods, hurry in!!! Easy to master functions in C language

Building of APP automation environment (I)

蓝桥杯原题
随机推荐
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
随机森林与集成方法学习笔记
线程基础
R 笔记 MICE
JVM memory layout detailed, illustrated, well written!
Pychart shortcut key for quickly modifying all the same names on the whole page
Promise object
NPDP candidates! The exam requirements for July 31 are here!
蓝桥杯:第九届—“彩灯控制器”
关于权重衰退和丢弃法
Superparameter adjustment and experiment - training depth neural network | pytorch series (26)
Distributed transaction Senta (I)
P6118 [joi 2019 final] solution to the problem of Zhenzhou City
style=“width: ___“ VS width=“___“
Trivy [1] tool scanning application
牛客-TOP101-BM340
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
Industry insight | is speech recognition really beyond human ears?
MySQL essay
Games101 review: ray tracing