readonly,disabled是input的属性
ReadOnly,Enabled是asp:TextBox的属性
要求: 客户端不能输入,但可通过js赋值,并在服务器端获取到js赋的值
前台:
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Enabled ></asp:TextBox>
<input id="Text1" runat="server" type="text" readonly="readonly" />
<input id="Button1" type="button" value="客户端赋值" onclick="doClick()" />
<asp:Button ID="Button2" runat="server" Text="服务器端获取" OnClick="Button2_Click" />
<script language="javascript">
function $(s){return document.getElementById(s);}
function doClick()
{
$('<%=Text1.ClientID %>').value="wufeng";
$('<%=TextBox1.ClientID %>').value="wufeng";
}
</script>
后台:<input id="Text1" runat="server" type="text" readonly="readonly" />
<input id="Button1" type="button" value="客户端赋值" onclick="doClick()" />
<asp:Button ID="Button2" runat="server" Text="服务器端获取" OnClick="Button2_Click" />
<script language="javascript">
function $(s){return document.getElementById(s);}
function doClick()
{
$('<%=Text1.ClientID %>').value="wufeng";
$('<%=TextBox1.ClientID %>').value="wufeng";
}
</script>
protected
void
Button2_Click(
object
sender, EventArgs e)
{
Response.Write(Text1.Value + " Hello<br/>");
Response.Write(TextBox1.Text + " Hello<br/>");
}
{
Response.Write(Text1.Value + " Hello<br/>");
Response.Write(TextBox1.Text + " Hello<br/>");
}
执行服务器端回传前的客户端代码:
<input name="TextBox1" type="text" readonly="readonly" id="TextBox1" class="ClassText" />
<input name="Text1" type="text" id="Text1" readonly="readonly" />
<br />
<input id="Button1" type="button" value="客户端赋值" onclick="doClick()" />
<input type="submit" name="Button2" value="服务器端获取" id="Button2" class="ClassBtn" />
<input name="Text1" type="text" id="Text1" readonly="readonly" />
<br />
<input id="Button1" type="button" value="客户端赋值" onclick="doClick()" />
<input type="submit" name="Button2" value="服务器端获取" id="Button2" class="ClassBtn" />
执行服务器端回传后的客户端代码:
<
input
name
="TextBox1"
type
="text"
readonly
="readonly"
id
="TextBox1"
class
="ClassText"
/>
< input name ="Text1" type ="text" id ="Text1" readonly ="readonly" value="wufeng" />
< br />
< input id ="Button1" type ="button" value ="客户端赋值" onclick ="doClick()" />
< input type ="submit" name ="Button2" value ="服务器端获取" id ="Button2" class ="ClassBtn" />
区别:Text1的value属性前后发生了变化,而TextBox1并不能从后台获取到。< input name ="Text1" type ="text" id ="Text1" readonly ="readonly" value="wufeng" />
< br />
< input id ="Button1" type ="button" value ="客户端赋值" onclick ="doClick()" />
< input type ="submit" name ="Button2" value ="服务器端获取" id ="Button2" class ="ClassBtn" />
解决:在Page_Load中设置
TextBox1.Attributes[
"
contentEditable
"
]
=
"
false
"
;
//
等效于ReadOnly="true"
或直接从前台设置TextBox的属性
contentEditable="false"