GridView with Password Field

F

Flyguy

I have a gridview that contains a password field. I’d like that password
field to be in textmode = "Password" when in edit mode. That way it will
only show *'s. How can I do that?
 
S

Steven Cheng [MSFT]

Hi Flyguy,

Regarding on the your scenario that you want to use Password input field
for Gridview editing, I think you can convert the certain column(in
Gridview) to a TemplateField, thus you can customize the "EditTemplate" and
use Password mode TextBox for editing data.

Also, for Password mode textbox, it has a natural limitation that we can
not programmtically assign the value to it(through databinding expression)
like normal mode textbox. One method I used is put an additional Label in
that column which display the original value for the user's reference. Here
is a example page I've used for test:

=========================
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True"
SortExpression="id" />

<asp:TemplateField HeaderText="name" SortExpression="name">
<EditItemTemplate>

<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("name") %>'
TextMode="Password" ></asp:TextBox>
<br />original value:
<asp:Label ID="lblOld" runat="server" Text='<%#
Eval("name") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
=========================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
F

Flyguy

Steven,

This is great stuff. My only question is how I get the value that the user
enters into the textbox. I have no idea how I reference the data in my code.
 
S

Steven Cheng [MSFT]

Thanks for your reply Flyguy,

As for the TextBox in template field , if you use "Bind" expression and
datasource control, it automatically provide two-way databinding function.
Are you using custom code to do the database updating(so that you need to
manually get reference to the data in textbox)?

generally, if you use the databound column or standard databinding
expression(two-way) in template, you can register "RowUpdating" event of
gridview and check those fields in the parameter:

=================================
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
//e.Keys;
//e.OldValues;
// e.NewValues;
}
=========================

Please feel free to let me know if you have any other questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: =?Utf-8?B?Rmx5Z3V5?= <[email protected]>
References: <[email protected]>
Subject: RE: GridView with Password Field
Date: Sat, 5 Jul 2008 16:44:00 -0700
 
F

Flyguy

I am using custom code to do the database updating (so I do need to manually
get a reference to the data in textbox).
 
S

Steven Cheng [MSFT]

Thanks for your reply Flyguy,

Yes, if you manually do the update, you need to extract the values(the user
enter for edit/update) and call ADO.NET functions to do the data accessing.
As I mentioned in previous reply, for GridView, the "RowUpdating" is the
proper place where you put your code which extract values from GridView
line and do the updating. For example:

#I assume you use <%# Eval("") %> expression to bind data and the following
is the EditTemplate:
=================

<asp:TemplateField HeaderText="name" SortExpression="name">
<EditItemTemplate>
<asp:TextBox ID="txtPassword" runat="server"
TextMode="Password" ></asp:TextBox>
....................
</EditItemTemplate>
..........................
</asp:TemplateField>
</Columns>
</asp:GridView>
=====================

You can use the below code to extract value from the password textbox:
=========
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
TextBox txtPass =
GridView1.Rows[GridView1.EditIndex].FindControl("txtPassword") as TextBox;

if(txtPass!= null)
Response.Write("<br/>you enter: " + txtPass.Text + " as
password.");

//do your data updating code here


e.Cancel = true;
}
}


==================

it also applies if you have more controls inside the template. Just Find
them via their ID.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
<3$#[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top