Set TextBox value when TextMode is Password on ASP.Net GridView RowCommand using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

i have issue of getting password value using textmode=password 

<asp:LinkButton ID="lnkEdit" runat="server" CommandName="EditDetails" CssClass="btn btn-primary btn-sm waves-effect waves-light"
    CommandArgument='<%# Eval("userId")+"$"+Eval("fullName")+"$"+Eval("email")+"$"+Eval("mobile")+"$"+Eval("password")+"$"+Eval("roleId")+"$"+Eval("roleName") %>'>                                             
<i class="fa fa-edit im"></i></asp:LinkButton>

at onrowcommand event

txtPassword.Attributes["value"] = Convert.ToString(args[4]);
txtConfPass.Attributes["value"] = Convert.ToString(args[4]);

here i am not getting password value for eg. In my db password value like 'abc123$' but in during binding i am getting 'abc123' value

Posted one month ago Modified on one month ago

Hi Saiansh,

You will not get the password with $. As you are spliting the CommandArgument string with $. So while assigning multiple value to CommandArgument through Evel use other character to concadinate the string. But there will be same issue happen when you are spliting with the character. So the better way is use HiddenField and save the password in HiddenFeld and retrieve from HiddenField.

HTML

<asp:GridView runat="server" ID="gdvUsers" OnRowCommand="gdvUsers_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="hfPassword" runat="server" Value='<%#Eval("password") %>' />
                <asp:LinkButton ID="lnkEdit" runat="server" Text="Select" CommandName="EditDetails"
                    CommandArgument='<%# Eval("userId")+"$"+Eval("fullName")+"$"+Eval("email")+"$"+Eval("mobile")+"$"+Eval("password")+"$"+Eval("roleId")+"$"+Eval("roleName") %>'> </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password" />
<asp:TextBox runat="server" ID="TextBox1" />

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("userId", typeof(int)), new DataColumn("fullName", typeof(string)), new DataColumn("email", typeof(string)), new DataColumn("mobile", typeof(string)), new DataColumn("password", typeof(string)), new DataColumn("roleId", typeof(string)), new DataColumn("roleName", typeof(string)) });
        dt.Rows.Add(1, "mudassar", "mu@gmail.com", "6565465", "pass123$", "r1", "admin");
        dt.Rows.Add(2, "test", "mu@gmail.com", "6565465", "test$", "r1", "admin");
        gdvUsers.DataSource = dt;
        gdvUsers.DataBind();
    }
}

protected void gdvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditDetails")
    {
        HiddenField hfPass = (((e.CommandSource as LinkButton).Parent.FindControl("hfPassword")) as HiddenField);
        txtPassword.Text = "";
        txtPassword.Attributes.Add("value", hfPass.Value);
        TextBox1.Text = hfPass.Value;
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("userId", GetType(Integer)), New DataColumn("fullName", GetType(String)), New DataColumn("email", GetType(String)), New DataColumn("mobile", GetType(String)), New DataColumn("password", GetType(String)), New DataColumn("roleId", GetType(String)), New DataColumn("roleName", GetType(String))})
        dt.Rows.Add(1, "mudassar", "mu@gmail.com", "6565465", "pass123$", "r1", "admin")
        dt.Rows.Add(2, "test", "mu@gmail.com", "6565465", "test$", "r1", "admin")
        gdvUsers.DataSource = dt
        gdvUsers.DataBind()
    End If
End Sub

Protected Sub gdvUsers_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.CommandName = "EditDetails" Then
        Dim hfPass As HiddenField = (TryCast(((TryCast(e.CommandSource, LinkButton)).Parent.FindControl("hfPassword")), HiddenField))
        txtPassword.Text = ""
        txtPassword.Attributes.Add("value", hfPass.Value)
        TextBox1.Text = hfPass.Value
    End If
End Sub

Screenshot