ASP.Net TextBox 只读(ReadOnly)时后台不能赋值取值

本文介绍了ASP.NET中TextBox设置为只读时后台无法正常取值的问题,并提供了三种解决方案:1. 使用onfocus=this.blur()阻止焦点;2. 通过Request.Form获取值;3. 在Page_Load中设置只读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">	</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">当把ASP.Net的自带控件TextBox设置为只读时,后台无法正常取值(取到的结果为“”)。解决办法如下:</span>


1. 不设置ReadOnly属性,添加事件onfocus=this.blur()

<asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox> 

2、设置了ReadOnly属性后,通过Request来取值,如下:

string Text = Request.Form["TextBox1"].Trim(); 

3、在Page_Load()正设置文本框的只读属性,能正常读取,如下:

protected void Page_Load(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
    {  
        TextBox1.Attributes.Add("readonly","true");  
    }  
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值