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

写英文IEEE论文的技巧

@Valid的作用(级联校验)以及常用约束注解的解释说明

分布式事务——Senta(一)

傅里叶级数

Selenium+pytest+allure comprehensive exercise

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

Is it you who are not suitable for learning programming?

CNN training cycle reconstruction - hyperparametric test | pytorch series (XXVIII)

Intelligent industrial design software company Tianfu C round financing of hundreds of millions of yuan

Comprehensive comparative study of image denoising
随机推荐
P6118 [joi 2019 final] solution to the problem of Zhenzhou City
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
上位机与MES对接的几种方式
一次跨域问题的记录
clientY vs pageY
没法预测明天的涨跌
机器人工程是否有红利期
app 自动化 环境搭建(一)
Data Lake: database data migration tool sqoop
数据中台建设(三):数据中台架构介绍
Superparameter adjustment and experiment - training depth neural network | pytorch series (26)
JVM memory layout detailed, illustrated, well written!
分布式 session 的4个解决方案,你觉得哪个最好?
Skills in writing English IEEE papers
【2022牛客多校2 K Link with Bracket Sequence I】括号线性dp
Games101 review: ray tracing
QT专题1:实现一个简易计算器
Design and practice of unified security authentication for microservice architecture
QFileDevice、QFile、QSaveFile、QTemporaryFile
R 笔记 MICE