一般來說,當表單回發之後,密碼框将會被自動清空。但是有些時候,我們并不希望這樣。
這時,我們可以添加一點的代碼将避免這個問題。
VB.NET:
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
If Not String.IsNullOrEmpty(txtPassword.Text.Trim()) Then
txtPassword.Attributes.Add("value", txtPassword.Text)
End If
End If
End Sub
C#:
C#
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (!(String.IsNullOrEmpty(txtPassword.Text.Trim())))
{
txtPassword.Attributes["value"] = txtPassword.Text;
}
}
}
缺點:這種方法會導緻密碼洩露,當檢視HTML源代碼的時候可以看到密碼!!!!