asp.net - Removing READONLY attribute from textbox using client side code - Stack Overflow

I have a multiline textbox that by default, is set to ReadOnly. I would like a button on the page to ch

I have a multiline textbox that by default, is set to ReadOnly. I would like a button on the page to change the control to allow it to be edited. I would like this code to run on the client side because the page renders slowly and I'd like to avoid the post back.

The problem I'm having is the javascript code that I wrote to remove the readonly attribute appears to have no effect. I posted a stripped down example that illustrates the problem for your review.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">

<script runat="server">

</script>

<html xmlns="">
<head runat="server">
    <title>Test</title>
    <script  type="text/javascript" language="javascript">
    function EnableEditing() {
    var e = document.getElementById("<%=TextBox1.ClientID%>");
        var result = e.removeAttribute("readonly");
        alert(result);

    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>     </div>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" TextMode="MultiLine">test</asp:TextBox>        
        <input id="Button1"  onclick="EnableEditing()" type="button" value="Remove RO" />

    </form>
</body>
</html>

I have a multiline textbox that by default, is set to ReadOnly. I would like a button on the page to change the control to allow it to be edited. I would like this code to run on the client side because the page renders slowly and I'd like to avoid the post back.

The problem I'm having is the javascript code that I wrote to remove the readonly attribute appears to have no effect. I posted a stripped down example that illustrates the problem for your review.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3/1999/xhtml">
<head runat="server">
    <title>Test</title>
    <script  type="text/javascript" language="javascript">
    function EnableEditing() {
    var e = document.getElementById("<%=TextBox1.ClientID%>");
        var result = e.removeAttribute("readonly");
        alert(result);

    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>     </div>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" TextMode="MultiLine">test</asp:TextBox>        
        <input id="Button1"  onclick="EnableEditing()" type="button" value="Remove RO" />

    </form>
</body>
</html>
Share Improve this question edited Mar 5, 2010 at 18:59 Aheho asked Mar 5, 2010 at 18:51 AhehoAheho 12.8k13 gold badges56 silver badges84 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

TextBox1 is the server side id,

try

var e = document.getElementById("<%=TextBox1.ClientID%>");

 var result = e.removeAttribute("readonly",0);

or if you dont want the caseinsensitive search

 var result = e.removeAttribute("readOnly");//note upercase Only

Use var e = document.getElementById("<%=TextBox1.ClientID%>");

Also, if you want to read the modified text on postback, you can't set the readonly attribute on the server control. You have to set it on the client only, as in: TextBox1.Attributes("readOnly") = "readOnly";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745573471a4633822.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信