How do you set the Textbox read only property to true or false using JavaScript in ASP.NET?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can try
document.getElementById("textboxid").readOnly = true;
回答2:
document.getElementById('textbox-id').readOnly=true
should work
回答3:
Try This :-
set Read Only False ( Editable TextBox)
document.getElementById("txtID").readOnly=false;
set Read Only true(Not Editable )
var v1=document.getElementById("txtID");
v1.setAttribute("readOnly","true");
This can work on IE and Firefox also.
回答4:
I find that document.getElementById('textbox-id').readOnly=true
sometimes doesn't work reliably.
Instead, try:
document.getElementById('textbox-id').setAttribute('readonly', 'readonly')
and
document.getElementById('textbox-id').removeAttribute('readonly')
.
A little verbose but it seems to be dependable.
回答5:
Using asp.net, I believe you can do it this way :
myTextBox.Attributes.Add("readonly","readonly")
回答6:
it depends on how you trigger the event. the key you are looking is textbox.clientid.
x.aspx code
<script type="text/javascript">
function disable_textbox(tid) {
var mytextbox = document.getElementById(tid);
mytextbox.disabled=false
}
</script>
code behind x.aspx.cs
string frameScript = "<script language='javascript'>" + "disable_textbox(" + tx.ClientID ");</script>";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "FrameScript", frameScript);