最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论