This is a migrated thread and some comments may be shown as answers.

EditFormTemplate textbox password mode not showing value for update

6 Answers 473 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 08 Jun 2014, 09:40 AM
Hi,
I have this grid with an EditFormTemplate :
 
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True" AllowSorting="True" Culture="it-IT" DataSourceID="SqlDataSource1" PageSize="20"  Width="100%" Skin="Outlook">
                        <ExportSettings>
                            <Pdf PageWidth="">
                            </Pdf>
                        </ExportSettings>
                        <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="Top" DataKeyNames="Id" DataSourceID="SqlDataSource1">
                            <Columns>
                                <telerik:GridEditCommandColumn ButtonType="ImageButton">
                                    <HeaderStyle Width="30px" />
                                </telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn DataField="Id" DataType="System.Int32" FilterControlAltText="Filter Id column" HeaderText="Id" ReadOnly="True" SortExpression="Id" UniqueName="Id">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="name" FilterControlAltText="Filter name column" HeaderText="Name" SortExpression="name" UniqueName="name">
                                    <ColumnValidationSettings EnableModelErrorMessageValidation="True" EnableRequiredFieldValidation="True" RenderValidatorBeforeEditor="True">
                                        <RequiredFieldValidator ErrorMessage="This filed is required! " ForeColor="Red"></RequiredFieldValidator>
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="surname" FilterControlAltText="Filter surname column" HeaderText="Surname" SortExpression="surname" UniqueName="surname">
                                    <ColumnValidationSettings EnableModelErrorMessageValidation="True" EnableRequiredFieldValidation="True" RenderValidatorBeforeEditor="True">
                                        <RequiredFieldValidator ErrorMessage="This filed is required! " ForeColor="Red"></RequiredFieldValidator>
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="userName" DefaultInsertValue="" FilterControlAltText="Filter userName column" HeaderText="User Name" SortExpression="userName" UniqueName="userName">
                                    <ColumnValidationSettings EnableModelErrorMessageValidation="True" EnableRequiredFieldValidation="True" RenderValidatorBeforeEditor="True">
                                        <RequiredFieldValidator ErrorMessage="This filed is required! " ForeColor="Red"></RequiredFieldValidator>
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="password" DefaultInsertValue="" FilterControlAltText="Filter password column" HeaderText="Password" SortExpression="password" UniqueName="password" Display="False">
                                    <ColumnValidationSettings EnableModelErrorMessageValidation="True" EnableRequiredFieldValidation="True" RenderValidatorBeforeEditor="True">
                                        <RequiredFieldValidator ErrorMessage="This filed is required! " ForeColor="Red"></RequiredFieldValidator>
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="email" DefaultInsertValue="@stadlerrail.com" FilterControlAltText="Filter email column" HeaderText="Email" SortExpression="email" UniqueName="email">
                                    <ColumnValidationSettings EnableModelErrorMessageValidation="True" EnableRequiredFieldValidation="True" RenderValidatorBeforeEditor="True">
                                        <RequiredFieldValidator ErrorMessage="This filed is required! " ForeColor="Red"></RequiredFieldValidator>
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="telephone" DefaultInsertValue="+" FilterControlAltText="Filter telephone column" HeaderText="Telephone" SortExpression="telephone" UniqueName="telephone">
                                    <ColumnValidationSettings EnableModelErrorMessageValidation="True" EnableRequiredFieldValidation="True" RenderValidatorBeforeEditor="True">
                                        <RequiredFieldValidator ErrorMessage="This filed is required! " ForeColor="Red"></RequiredFieldValidator>
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridButtonColumn ButtonType="ImageButton" Text="Delete" CommandName="Delete" FilterControlAltText="Filter column1 column" ConfirmDialogType="RadWindow" ConfirmText="Do you really want to delete this project and all its content?" UniqueName="column1">
                                    <HeaderStyle Width="30px" />
                                </telerik:GridButtonColumn>
                            </Columns>
                            <EditFormSettings EditFormType="Template">
                                <EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>
                                <FormTemplate>
                                    <style type="text/css">
                                        .auto-style1 {
                                            width: 100%;
                                            background-color: azure;
                                        }
 
                                        .auto-style2 {
                                            width: 84px;
                                        }
 
                                        .auto-style5 {
                                            text-align: left;
                                            width: 386px;
                                        }
                                    </style>
 
                                    <table class="auto-style1">
                                        <tr>
                                            <td class="auto-style2"></td>
                                            <td class="auto-style5"> </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2">Name:</td>
                                            <td class="auto-style5">
                                                <asp:TextBox ID="name" runat="server" Width="200px" Height="22px" Text='<%# Bind( "name" ) %>'>></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="name" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
                                                </asp:RequiredFieldValidator>
                                                 <asp:RegularExpressionValidator ID="validateName"
                                                    runat="server" ErrorMessage="Invalid Name. You shall insert letters only." ForeColor="Red"
                                                    ControlToValidate="name"
                                                    ValidationExpression="^[^\W\d_]+$" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2">Surname:</td>
                                            <td class="auto-style5">
                                                <asp:TextBox ID="surname" runat="server" Width="200px" Height="22px" Text='<%# Bind( "surname" ) %>'>></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="surname" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
                                                </asp:RequiredFieldValidator>
                                                <asp:RegularExpressionValidator ID="validateSurname"
                                                    runat="server" ErrorMessage="Invalid Surname. You shall insert letters only." ForeColor="Red"
                                                    ControlToValidate="surname"
                                                    ValidationExpression="^[^\W\d_]+$" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2">User Name:</td>
                                            <td class="auto-style5">
                                                <asp:TextBox ID="userName" runat="server" Width="200px" Height="22px" Text='<%# Bind( "userName" ) %>'>></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="UserName" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
                                                </asp:RequiredFieldValidator>
 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2">Password:</td>
                                            <td class="auto-style5">
                                                <asp:TextBox ID="password" runat="server" Width="200px" TextMode="Password" Height="22px" Text='<%# Bind( "password" ) %>'>></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" ControlToValidate="password" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
                                                </asp:RequiredFieldValidator>
                                                 <asp:RegularExpressionValidator ID="validatePassword"
                                                    runat="server" ErrorMessage="Password shall at least 8 characters that contains at least one lowercase and one uppercase ASCII character and also at least one character from the set @#$%^&+= (in any order)" ForeColor="Red"
                                                    ControlToValidate="password"
                                                    ValidationExpression="^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$" />
 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2">Email:</td>
                                            <td class="auto-style5">
                                                <asp:TextBox ID="email" runat="server" Width="200px" Height="22px" Text='<%# Bind( "email" ) %>'>></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" ControlToValidate="email" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
                                                </asp:RequiredFieldValidator>
                                                 <asp:RegularExpressionValidator ID="validateEmail"
                                                    runat="server" ErrorMessage="Invalid email. name.surname@stadlerrail.com" ForeColor="Red"
                                                    ControlToValidate="email"
                                                    ValidationExpression="^[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+@stadlerrail.com$" />
 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2">Telephone:</td>
                                            <td class="auto-style5">
                                                <asp:TextBox ID="telephone" runat="server" Width="200px" Height="22px" Text='<%# Bind( "telephone" ) %>'>></asp:TextBox>
                                             </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2"> </td>
                                            <td> </td>
                                        </tr>
                                        <tr>
                                            <td class="auto-style2"> </td>
                                            <td class="auto-style5">
                                                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
                                                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                    CommandName="Cancel" Style="text-align: right"></asp:Button>
                                            </td>
                                        </tr>
                                    </table>
 
 
                                </FormTemplate>
                            </EditFormSettings>
 
 
 
 
 
                        </MasterTableView>
                    </telerik:RadGrid>

Code behind is c#

Two problems:
1)
In the main grid the column password is set to visible=false. In the edit form template the value in "password" column is not displayed when the form is opened for edit/update. This cause a problem because:

A) the admin editing the record thinks that the value does not exist
B) having set a validator on such filed is not possible to update the record without entering a new password.

I would like to show the classic ************* without having to fight with the validator regex on a new update of the record.
How can I achieve that?

2)
I have set the default value of username and password to "" because on opening of a new record, the form displays the admin username and the password in password mode ******* in the relevant text boxes which should not happen but for some reason the values of the logged admin get displayed in those text boxes by default!
Is there a way to avoid this behavior rather than setting the default values to ""?
I have attached a picture, taken from the production server on creating a new record, showing such behavior when the default value of those two text boxes is not set to "".

Thanks,
Felice

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Jun 2014, 05:10 AM
Hi Felice,

You are not able to see the values in edit mode because you have set the TextMode as Password. You can set the ValidationGroup for the controls so as to fire it when needed. Please take a look at the sample code snippet.

ASPX:
<EditFormSettings EditFormType="Template">
    <FormTemplate>
        <br />
        <asp:TextBox ID="txtPassword" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvPassword" ControlToValidate="password" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
        </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="revPassword" runat="server" ErrorMessage="Password shall at least 8 characters that contains at least one lowercase and one uppercase ASCII character and also at least one character from the set @#$%^&+= (in any order)" ForeColor="Red" ControlToValidate="password" ValidationExpression="^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$" />
        <br />
        <asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
    </FormTemplate>
</EditFormSettings>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    //In EditMode view the password in *
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        TextBox txt = (TextBox)edit.FindControl("txtPassword");          
        string value = txt.Text;
        txt.Text = "";
        foreach (char c in value)
            txt.Text += "*";             
    }
    //In InsertMode, set validation group
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        TextBox txt = (TextBox)edit.FindControl("txtPassword");
        txt.TextMode = TextBoxMode.Password;
        txt.ValidationGroup = "InsertGroup";
        var rfvPassword = (RequiredFieldValidator)e.Item.FindControl("rfvPassword");
        rfvPassword.ValidationGroup = "InsertGroup";
        var revPassword = (RegularExpressionValidator)e.Item.FindControl("revPassword");
        revPassword.ValidationGroup = "InsertGroup";
        LinkButton lnkCancel = (LinkButton)edit.FindControl("lnkCancel");
        lnkCancel.ValidationGroup = "InsertGroup";
    }
}

Thanks,
Princy
0
Felice
Top achievements
Rank 1
answered on 09 Jun 2014, 06:55 PM
Hi Princy,
thanks a lot for your snippet. It works fine and it shows ********** but the problem is with the regex which does not validate the password anymore since there are ******* in the text box and not the expected set of chars defined in the validator. Let me know if you have an idea on how to overcome this problem otherwise I have no other solutions than to remove the regex validator and leave only the minimum amount of characters in it.
Kind regards
Felice
0
Felice
Top achievements
Rank 1
answered on 10 Jun 2014, 04:10 AM
Hi Princy,
please do not consider my previous post as I did more testing early this morning and found out the following:
The password appears in ******** in the text box and this is now correct.
In edit mode the RegularExpressionValidator is showing the error message that the password is not in accordance to the regex.
In insert mode I get an "Invalid cast on the requiredfieldvalidator: Impossible to execute cast of object type 'System.Web.UI.WebControls.TextBox' on type 'System.Web.UI.WebControls.RequiredFieldValidator'.
Please keep in mind that this is in a EditFormType="Template"
<td class="auto-style2">Password:</td>
                                           <td class="auto-style5">
                                               <asp:TextBox ID="password" runat="server" Width="200px"  Height="22px" Text='<%# Bind( "password" ) %>'>></asp:TextBox>
                                               <asp:RequiredFieldValidator ID="RequiredFieldValidator7" ControlToValidate="password" ErrorMessage="This field shall be filled" runat="server" BackColor="Red">
                                               </asp:RequiredFieldValidator>
                                                <asp:RegularExpressionValidator ID="validatePassword"
                                                   runat="server" ErrorMessage="Password shall at least 8 characters that contains at least one lowercase and one uppercase ASCII character and also at least one character from the set @#$%^&+= (in any order)" ForeColor="Red"
                                                   ControlToValidate="password"
                                                   ValidationExpression="^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$" />
 
                                           </td>

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
 
        //In EditMode view the password in *
        if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            TextBox txt = (TextBox)edit.FindControl("password");
            string value = txt.Text;
            txt.Text = "";
            foreach (char c in value)
                txt.Text += "*";
        }
        //In InsertMode, set validation group
        if (e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted)
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            TextBox txt = (TextBox)edit.FindControl("password");
            txt.TextMode = TextBoxMode.Password;
            txt.ValidationGroup = "InsertGroup";
            var rfvPassword = (RequiredFieldValidator)e.Item.FindControl("password");
            rfvPassword.ValidationGroup = "InsertGroup";
            var revPassword = (RegularExpressionValidator)e.Item.FindControl("password");
            revPassword.ValidationGroup = "InsertGroup";
            LinkButton lnkCancel = (LinkButton)edit.FindControl("Cancel");
            lnkCancel.ValidationGroup = "InsertGroup";
        }
 
    }

Thank you for your kind support,
Felice


0
Princy
Top achievements
Rank 2
answered on 10 Jun 2014, 05:22 AM
Hi Felice,

Please set your code behind as follows, remove the ValidationGroup settings:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{     
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        TextBox txtPassword = (TextBox)edit.FindControl("password");
        string value = txtPassword .Text;
        txtPassword .Text = "";
        foreach (char c in value)
            txtPassword .Text += "*";
    }    
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        TextBox txtPassword = (TextBox)edit.FindControl("password");
        //To set password mode in Insert
        txtPassword .TextMode = TextBoxMode.Password;          
    }
}

Thanks,
Princy
0
Felice
Top achievements
Rank 1
answered on 10 Jun 2014, 05:57 PM
Hi Princy,
thanks for your support.
Unfortunately what the code you suggested does is the following:
-on insert I can create the password and insert it correctly
-on modification ****** are shown in the password text box but, without modifying the password, if I update the record, ****** get inserted in the db replacing the password.

0
Princy
Top achievements
Rank 2
answered on 11 Jun 2014, 04:29 AM
Hi Felice,

You may access the cell value in the UpdateCommand and set the value of password to its original text and insert that to db.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == RadGrid.UpdateCommandName)
  {
    GridEditableItem editItem = (GridEditableItem)e.Item;
    TextBox txtPassword = (TextBox)editItem.FindControl("password");
    if ((txtPassword.Text).Contains("*"))
    {
     TableCell cell = editItem["password"];//access the password cell to retrieve its value
     string password = cell.Text; //Update to DB with this password
    }
    else
    {
     string password = txtPassword.Text;// Save the entered password
    }
  }
}

Thanks,
Princy
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Felice
Top achievements
Rank 1
Share this question
or