[Asp.net]說說密碼框和唯讀框

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

原文:[Asp.net]說說密碼框和唯讀框

引言

最近負責了一個公司的小項目,從前台到後代,都是自己搞的,為一個客戶弄一個資訊管理的小系統,雖然對介面什麼的沒要求,處於負責的態度,還是給他加了一下樣式,當然,大部分樣式還是從網上扒下來的。

密碼框

情景是這樣的,當你在使用者列表中選擇要編輯的記錄時,需要在編輯頁面將使用者資訊填在對應的框內,密碼框的問題就來了。這裡弄了一個簡單頁面進行測試,代碼如下:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Wolfy.TextBoxDemo.Default" %> 2 <!DOCTYPE html> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head runat="server"> 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6     <title></title> 7 </head> 8 <body> 9     <form id="form1" runat="server">10         <div>11             密碼框:<asp:TextBox runat="server" TextMode="Password" ID="txtPwd" />12            13         </div>14     </form>15 </body>16 </html>

現在我們,在頁面載入的時候,就給它設定個值,代碼如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 namespace Wolfy.TextBoxDemo 9 {10     public partial class Default : System.Web.UI.Page11     {12         protected void Page_Load(object sender, EventArgs e)13         {14             if (!IsPostBack)15             {16                 this.txtPwd.Text = "admin";17             }18         }19     }20 }

一般情況下,都會習慣這樣去寫,瀏覽一下,被坑了。

竟然沒有值,怎麼給它賦值呢?

幸虧,還有一種方式,不然這密碼框就廢了,只能取,不能改變值,太無語了。

 1 namespace Wolfy.TextBoxDemo 2 { 3     public partial class Default : System.Web.UI.Page 4     { 5         protected void Page_Load(object sender, EventArgs e) 6         { 7             if (!IsPostBack) 8             { 9                 //this.txtPwd.Text = "admin";10                 this.txtPwd.Attributes.Add("value","admin");11             }12         }13     }14 }

瀏覽看看結果:

這.....

為什麼非採用這方式,用Text屬性多好!

唯讀框

情景是這樣的,使用js或者jquery為唯讀框複製,在cs中再擷取這個值,單擊按鈕,然後儲存到資料庫中。

js代碼:

1     <script  type="text/javascript">2         window.onload = function () {3             document.getElementById("txtReadOnly").innerText="這是js動態賦值的";4         }5     </script>

修改代碼:

1             唯讀框:<asp:TextBox runat="server"  ReadOnly="true" ID="txtReadOnly" />2             <asp:Button Text="按鈕" ID="btn" OnClick="btn_Click" runat="server" />

效果

cs中的代碼:

出於習慣性,想去文字框的值,一般都會採用上面的方式,可是....

1         protected void btn_Click(object sender, EventArgs e)2         {3             string strReadOnly = this.txtReadOnly.Text;4         }

 調試一下,看能不能擷取到:

 

解決辦法:

1         protected void btn_Click(object sender, EventArgs e)2         {3             string strReadOnly = Request.Form["txtReadOnly"];4         }

結果:

分析:

html源碼:

修改唯讀框,給他一個預設值:

1             唯讀框:<asp:TextBox runat="server"  ReadOnly="true" Text="這是預設值" ID="txtReadOnly"  />2             <asp:Button Text="按鈕" ID="btn" OnClick="btn_Click" runat="server" />

調試一下看看結果:

對應的html源碼:

猜測:唯讀框通過Text屬性,只能擷取在用戶端瀏覽器產生的html標籤中有value屬性的文字框的值。(比較拗口)

總結

之前開發都是負責某個模組,這第一次從登入到其他的業務處理,自己全權負責的,所以覺得很奇怪。今天群裡面也有人問起了,就覺得有必要總結一下。對於唯讀框,如果本身就有值,那麼可以通過Text屬性擷取到。如果是前台,動態設定的內容,就會擷取不到。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.