How to save textbox viewstate that modetype is password, Show password
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password" Value='<%#Eval("Password") %>'></asp:TextBox>
Or
txtPassword.Attributes.Add("Value", password);
See AddAttributesToRender method you can find the answer. because the control is check modetype if modetype is password
that don't add the Attribute.
如何让asp.net 密码框****不会消失掉。
使用Reflector 我们可以看到它的控件会判断 Mode是不是password是的话就不把值写进来了
我们可以自己绕过这一层用txtPassword.Attributes.Add("Value", password);这样就可以把password写进来.
也可以自己弄一个小控件
以下就是PasswordBox的代码
Code
public class PasswordBox:TextBox,IPostBackDataHandler,ITextControl
{
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}
protected override void OnPreRender(EventArgs e)
{
//base.OnPreRender(e);
this.Page.RegisterRequiresPostBack(this);
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
if (this.ID != null)
{
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
}
if (!this.Enabled)
{
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
}
int tabIndex = this.TabIndex;
if (tabIndex != 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
}
if (!string.IsNullOrEmpty(this.CssClass))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
}
if (this.ControlStyleCreated && !this.ControlStyle.IsEmpty)
{
this.ControlStyle.AddAttributesToRender(writer, this);
}
}
#region IPostBackDataHandler Members
public new bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string text = this.Text;
string str2 = postCollection[postDataKey];
if (!text.Equals(str2, StringComparison.Ordinal))
{
this.Text = str2;
return true;
}
return false;
}
#endregion
} 原文地址:http://www.cnblogs.com/lovebanyi/archive/2008/10/29/1322151.html