Setting the Textbox read only property to true usi

2020-03-01 05:49发布

问题:

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);