国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 2.0里當readonly遇上enableviewstate=false

2024-07-10 13:11:52
字體:
來源:轉載
供稿:網友

  偶然在一個老外的blog里看到有這樣的描述,當textbox控件里被設置為readonly時,而且頁面的enableviewsate設置為false時,提交后,textbox的值會丟失,這只發生在asp.net 2.0中,在asp.net 1.0/1.1中不會出現這樣的情況,代碼如下:

<%@ page language="c#" autoeventwireup="true"  codefile="default.aspx.cs" enableviewstate="false"  inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
    protected void page_load(object sender, eventargs e)
    {
        if (!this.ispostback)
        {
            this.textbox1.text = "readonly text";
        }
    }
    protected void button1_click(object sender, eventargs e)
    {
        this.lblmessage.text = this.textbox1.text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>untitled page</title>
</head>
<body>
    <form runat="server" id="form1">
<asp:textbox id="textbox1" runat="server" readonly="true" forecolor="silver"></asp:textbox>
<asp:textbox id="textbox2" runat="server" readonly="true">some text</asp:textbox>
<asp:button id="button1" runat="server" text="button" onclick="button1_click" /><br />
<asp:label id="lblmessage" runat="server" text="label"></asp:label>
</form>
</body>
</html>

  在.net 2.0下運行,的確會丟失了文本框的值。最后,找到了msdn的解析和微軟的bug反饋中心,其實這不是bug,是.net 2.0下為了安全的一個小改變,具體摘錄如下,大家就明白了:
微軟的反饋為:

after careful analysis, the explanation for the observed behaviour is that:
with a design change in asp .net based on user security concern, the input for a readonly textbox is saved in viewstate, which doesnt happen if viewstate is disabled. to workaround this, a page developer can add the readonly attribute to the textbox.attributes collection, which can then be used to access the 'value' of the textbox.

we hope this clarifies. thank you.
web server & tools

msdn 2005的解析:

the text value of a textbox control with the readonly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. this prevents a malicious user from changing a text value that is read-only. the value of the text property is preserved in the view state between postbacks unless modified by server-side code.

菜鳥學堂:
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灯塔市| 辽阳县| 两当县| 隆德县| 濮阳县| 高陵县| 大英县| 肥乡县| 昆明市| 高尔夫| 新干县| 万州区| 石屏县| 龙陵县| 革吉县| 晋中市| 河曲县| 双辽市| 滁州市| 莱州市| 灵台县| 连平县| 东乌珠穆沁旗| 大英县| 朝阳区| 旺苍县| 博兴县| 谢通门县| 台东市| 麻城市| 沙洋县| 简阳市| 凯里市| 确山县| 云梦县| 喀喇| 奉节县| 岑溪市| 礼泉县| 镇康县| 治多县|