Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I display asterisk for password in Gridview

Status
Not open for further replies.

tuyendvu

Programmer
Feb 27, 2009
7
US
Hi,

I am trying to bind data from database and display on the gridview, but want to display password as asterisk. I tried so many ways but can;t get it. Please help

<asp:DataList ID="myList" runat="server">
<ItemTemplate>
<td valign="top">
<table border="0" cellpadding="0" cellspacing="0" style="border-top:0">
<tr>
<td align="right" class="black8" > E-Mail Address:
</td>
<td align="left" class="black8">
<asp:TextBox ID="txtEmailAddr" runat="server" Wrap="true" MaxLength="50" Width="130" Text='<%#DataBinder.Eval(Container.DataItem,"USER_ID") %>' />
<asp:RequiredFieldValidator ID="rfvUserEmail" runat="server" ControlToValidate="txtEmailAddr" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rfvUserEmailValidate" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtEmailAddr" ErrorMessage="Invalid Format"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td align="right" class="black8" >
Password*:
</td>
<td align="left">
<asp:TextBox ID="txtPassword1" MaxLength="8" Width="130" Enabled="true" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"PASSWORD") %>' />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" display="dynamic" ControlToValidate="txtPassword1" ErrorMessage="Must be 8 characters." ValidationExpression="[^\s]{8,15}" />
</td>
</tr>
<tr>
<td align="right" class="black8" >Verify Password*:
</td>
<td align="left" >
<asp:TextBox ID="txtPassword2" TextMode="Password" MaxLength="15" Width="130" runat="server"></asp:TextBox>
<asp:CompareValidator ID="PassComp" ControlToCompare="txtPassword1" ControlToValidate="txtPassword2" runat="server" Display="Dynamic" ErrorMessage="Don't Match"/>
</td>
</tr>
 
Here is my CS

protected void Page_Load(object sender, EventArgs e)
{

if (Session["LOGGED_IN"] == "YES")
{
if (!IsPostBack)
{
string txtCompany = Session["COMPANY"].ToString();

txtEmail = Session["USER_ID"].ToString();
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

String sqlSelect = "PROC_GET_IA_TAEROSPACE_PORTAL_USER";
SqlCommand sqlCmd = new SqlCommand(sqlSelect, conn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@sEmail", txtEmail);
conn.Open();

SqlDataReader dr = sqlCmd.ExecuteReader();
myList.DataSource = dr;
myList.DataBind();
conn.Close();

lblCompany.Text = txtCompany;
lblCompany.Visible = true;
lblMyAcct.Visible = false;
}
}
else
{
Response.Redirect("~/default.aspx");
}
}
 
When the data just displays in the grid, it is using a lable to display the data, not a textbox. So, setting the textbox mode to password only will work in edit mode. If you want to display an asterisk(s) then you will have to return them in your sql.
 
Thanks,

But my other question is: If the user they want to edit/change the password on this page. How can I do that?
 
If you are using the gridview as I said, then in edit mode the password will be in a textbox, change that to textmode=password.
 
I tired that but if I have
TextMode="Password" then the field is BLANK
If not the display real password.

Please help
 
Oopss

I am sorry I used the DATALIST not GridVIEW
 
If I have TextMode="Password" then the field is BLANK
That's correct; it's for security. When you set the type to a password it doesn't write a value property out. Check the HTML and you will see...

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
ca8msm

How can display password as **** when databind on DATALIST. and also this field is editable, will allow user to edit password field.
 
YOu can't. As Mark said, the textbox will be blank if the mode=password. And it will show **** as the user is tying in the textbox
 
Thanks for you help.. I will look into alterative ways.

 
there is a more fundamental problem. why do you need to get the passwords from the database? and why are the passwords stored as plain text?

passwords should be a salted, one-way hash. you shouldn't ever need to get the value, only reference it in a query like.
Code:
if(exists(select 1 from user where id=@userid and pwd=@pwd)) return 1
else return 0

when saving the password to the db. it should look something like this.
Code:
var salt = "secrete text to salt the password";
var pwd = pwd_text_box.Text;
var salted_pwd = pwd + salt;
var hashed_salted_pwd = new Sha1Encryption.Encrypt(salted_pwd);

create_user(username, hashed_salted_pwd);
or
var is_valid(username, hashed_salted_pwd);

Response.Redirect("welcome.aspx");

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top