我有一个文本框
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>
文本框中的文本是通过 Jquery DatePicker 输入的。在后面的一些代码中,我像这样从这个文本框中获取文本。
string x=checkIn.Text;
为什么我不能从文本框中提取输入的日期?我猜这与它是只读的事实有关,因为当我删除它时它可以工作?
谁能帮我?
在 ASP.Net 中,如果更改只读值,它将在回发时恢复为原始值。
但是,您可以使用 wrokaround,而不是以readonly声明方式指定,而是将其分配为代码隐藏中的属性。IE
代替
<asp:textbox runat="server" id="checkIn" ReadOnly="true"...
应用这是代码隐藏
checkIn.Attributes.Add("readonly", "readonly");
但是,视图状态仍然可能不适用于此。
更多信息:
readonlyHTML 中的和disabled控件之间存在细微差别。那些disabled不会与表格一起提交,但是readonly那些会。从字面上看,readonly只是readonly,但disabled实际上是disabled。
来自 W3C:http ://www.w3.org/TR/html401/interact/forms.html#h-17.12
(下到 17.13.2 17.13 表单提交下的成功控制部分)
然而,如果一个控件是这样声明的,即如果在初始化期间设置了属性,则 ASP.Net 在回发时恢复为原始值。这就是为什么稍后设置属性(在页面加载中)不会影响此行为的原因。
用这个:-
<input type="text" readonly="readonly" runat="server" id="checkIn" clientidmode="Static"/>
而不是这个: -
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true"></asp:textbox>
在您的代码隐藏文件中编写以下代码,对于 asp.net .cs 或类文件,对于 .vb 文件中的 vb。
您必须在页面加载事件中编写它,具体取决于您是在回发之前还是之后或两者都无关...
textbox.Attributes.Add("readonly", "readonly");
在通过 jQuery 或 Java-Script 发布值之前尝试将其readonly设为 false。它应该工作。
$('#tbox').removeAttr('readonly');
假设我有一个asp:button然后在客户端单击,我需要调用一个从文本框中删除只读属性的函数。
<asp:Button ID="MessageButton" runat="server" Text="Hellow"
OnClientClick="return changeAttribute()" />
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>
并在javascript中更改属性函数如下
function changeAttribute()
{
$('#checkIn').removeAttr('readonly');
return true;
}
您还可以在 asp:TextBox 控件中关闭 readOnly 属性并使用 jquery $('#textbox').attr('readOnly','readOnly')。
我对 jQuery 有同样的问题,但是用一个技巧解决了:
$('#txtBoxID').removeAttr('readonly');
/// Do your stuff here..
$('#txtBoxID').add('readonly');
;)
请用:
Request.Form[txtText.UniqueID]
在asp.cs页面中应用: AttrName.Attributes.Add("readonly", "readonly");
如果它在表单中,您实际上可以获得只读文本框的值。
var text = Page.Request.Form[TextBox.UniqueID];
抱歉,这个回复晚了几年!