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

[Solved] Change textbox value during insert

2 Answers 286 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 24 Sep 2009, 05:35 AM
Hi, I'm using a radgrid with a FormTemplate that has many textboxes and a couple of other standard asp.net controls. One of the textboxes is for saving a password, before the insert is sent to the database I'd like to take the password text, hash it up then send the hash to the database (I'm using entity framework for accessing the db).

Problem is I seem to be able to do everything but actually save the record. Below is some of my code but
a) nothing gets to the db and
b) the grid stays in insert mode and the values clean out on clicking the "insert" like

Any any sight would be much appreciated.


<telerik:RadGrid ID="CandidateGrid" runat="server" AutoGenerateColumns="false" DataSourceID="CandidateData"
            AllowFilteringByColumn="true" AllowPaging="true" OnItemCommand="CandidateGrid_Command" OnInsertCommand="CandidateGrid_Insert">
            <PagerStyle Mode="NumericPages" Visible="true" />
            <MasterTableView DataSourceID="CandidateData" DataKeyNames="OptimumUserId" AllowMultiColumnSorting="true"
                GroupLoadMode="Server" AllowAutomaticUpdates="true" AllowAutomaticInserts="true"
                CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="../Assets/Images/document_edit.png" />
                    <telerik:GridCheckBoxColumn DataField="IsActive" HeaderText="Active" />
                    <telerik:GridBoundColumn DataField="OptimumPayrollId" HeaderText="Payroll Id" />
                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" />
                    <telerik:GridBoundColumn DataField="Surname" HeaderText="Surname" />
                    <telerik:GridBoundColumn DataField="Email" HeaderText="Email" />
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <table>
                            <tr>
                                <td>
                                    Active:
                                </td>
                                <td class="required">
                                    *
                                </td>
                                <td>
                                    <asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Payroll Id:
                                </td>
                                <td class="required">
                                    *
                                </td>
                                <td>
                                    <asp:TextBox ID="PayrollIdTextBox" runat="server" Text='<%# Bind("OptimumPayrollId") %>' />
                                    <asp:RequiredFieldValidator ID="PayrollIdTextBoxValidation" runat="server" ControlToValidate="PayrollIdTextBox"
                                        ErrorMessage="Required" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    First Name:
                                </td>
                                <td class="required">
                                    *
                                </td>
                                <td>
                                    <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>' />
                                    <asp:RequiredFieldValidator ID="FirstNameValidation" runat="server" ControlToValidate="FirstNameTextBox"
                                        ErrorMessage="Required" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Surname:
                                </td>
                                <td class="required">
                                    *
                                </td>
                                <td>
                                    <asp:TextBox ID="SurnameTextBox" runat="server" Text='<%# Bind("Surname") %>' />
                                    <asp:RequiredFieldValidator ID="SurnameValidation" runat="server" ControlToValidate="SurnameTextBox"
                                        ErrorMessage="Required" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Email:
                                </td>
                                <td class="required">
                                    *
                                </td>
                                <td>
                                    <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                                    <asp:RequiredFieldValidator ID="EmailValidation" runat="server" ControlToValidate="EmailTextBox"
                                        ErrorMessage="Required" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Password:
                                </td>
                                <td class="required">
                                    *
                                </td>
                                <td>
                                    <asp:TextBox ID="PasswordTextBox" runat="server" Text='' />
                                    <asp:RequiredFieldValidator ID="PasswordValidation" runat="server" ControlToValidate="PasswordTextBox"
                                        ErrorMessage="Required" />
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    <asp:LinkButton ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                    </asp:LinkButton>&nbsp;
                                    <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                        CommandName="Cancel"></asp:LinkButton>
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                </EditFormSettings>
...
 protected void CandidateGrid_Insert(object source, GridCommandEventArgs e)
    {

        e.Canceled = true;
        ListDictionary newValues = new ListDictionary();

        OptimumProviders.MembershipProvider p = new MembershipProvider();
        GridEditableItem editedItem = e.Item.OwnerTableView.GetInsertItem();

        string password = (editedItem.FindControl("PasswordTextBox") as TextBox).Text;
        bool isActive = (editedItem.FindControl("ActiveCheckBox") as CheckBox).Checked;
        string encodedPassword = p.EncodePassword(password);

        newValues["Password"] = encodedPassword;
        newValues["IsActive"] = isActive;

        e.Item.OwnerTableView.InsertItem(newValues);
    }

2 Answers, 1 is accepted

Sort by
0
Accepted
Tsvetoslav
Telerik team
answered on 26 Sep 2009, 01:20 PM
Hello Chris,

Please, note that you cannot use the code in the CandidateGrid_Insert event handler to dynamically change the values of the entity that is being inserted. The approach with the ListDictionary newvalues collection being passed to the InsertItem method of the table view (explained in our documentation) refers to a completely different case - the case when you need to set default values for the insert form when the latter is just being opened. Consequently, this code should be moved to the ItemCommand event when the command that is coming is InitInsert, not PerformInsert.

As far as I understand you need to encode the value of the password just before it is inserted in the database. Therefore, you need to remove all the code from the CandidateGrid_Insert (btw, due to setting e.Canceled = true the insert operation fails and no data goes to the database), override the Inserting event of the EntityDataSource and use the coming e.Entity object to change the corresponding property - your code should look like the following:

protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)  
{  
     OptimumProviders.MembershipProvider p = new MembershipProvider();  
     e.Entity.Password = p.EncodePassword(e.Entity.Password);  

I hope this helps.

Greetings,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Chris
Top achievements
Rank 1
answered on 26 Sep 2009, 01:58 PM
Thanks for the idea with the entity datasource, I was so hung up on doing it via the grid events that I didn't even think of achieving the same outcome a different way.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Chris
Top achievements
Rank 1
Share this question
or