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

EditFormSettings checkbox value doesn't save

6 Answers 153 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Q
Top achievements
Rank 1
Q asked on 16 Oct 2013, 09:48 PM
<EditFormSettings EditFormType="Template">
<FormTemplate>
......
Active:
<
asp:CheckBox ID="CheckBox11" runat="server" Checked='<%# Eval("active") == DBNull.Value? false: Convert.ToBoolean(Eval("active")) %>' />

    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
       

        if (e.CommandName == RadGrid.InitInsertCommandName)
        {

            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");

           editColumn.Visible = false;

           GridEditableItem editedItem = e.Item as GridEditableItem;

            e.Canceled = true;
            //Prepare an IDictionary with the predefined values 

            System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();

            //set initial checked state for the checkbox on init insert 

            newValues["active"] = true;

            //Insert the item and rebind 

            e.Item.OwnerTableView.InsertItem(newValues);

        }

        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
        {

            e.Canceled = true;

        }

        else
        {

            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");

            if (!editColumn.Visible)

                editColumn.Visible = true;

        }


protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
 
{
 
e.InputParameters["active"] = ??????
 
}

 

Somehow when I tried to save the value of checkbox after click in edit mode or insert mode. It won't take that value. Please help.

Thanks

6 Answers, 1 is accepted

Sort by
0
Q
Top achievements
Rank 1
answered on 16 Oct 2013, 10:08 PM

Below is my code. When I tried to update or insert new record, the checkbox updated value never save. Please help

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
  
        <script type="text/javascript">
  
            function RowDblClick(sender, eventArgs) {
  
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
  
            }
  
        </script>
  
    </telerik:RadCodeBlock>
  
   
   <p id="divMsgs" runat="server">
  
        <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080">
  
        </asp:Label>
  
        <asp:Label ID="Label2" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000">
  
        </asp:Label>
          
        <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0"
        DataSourceID="ObjectDataSource1" GridLines="None"
  
        AllowPaging="True" PageSize="20" AllowSorting="True" AutoGenerateColumns="False"
  
        ShowStatusBar="True"  AllowAutomaticInserts="True"
  
        AllowAutomaticUpdates="True"  OnItemInserted="RadGrid1_ItemInserted"
        OnItemUpdated="RadGrid1_ItemUpdated" OnItemCommand="RadGrid1_ItemCommand"
  
        OnPreRender="RadGrid1_PreRender">
             <MasterTableView AutoGenerateColumns="False" DataKeyNames="TrainingID" CommandItemDisplay="TopAndBottom"
                 DataSourceID="ObjectDataSource1">
                 <Columns>
                      <telerik:GridEditCommandColumn>
  
                </telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="TrainingID" DataType="System.Int32"
                        FilterControlAltText="Filter TrainingID column" HeaderText="TrainingID"
                        ReadOnly="True" SortExpression="TrainingID" UniqueName="TrainingID">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="name"
                        FilterControlAltText="Filter name column" HeaderText="name"
                        SortExpression="name" UniqueName="name">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="description"
                        FilterControlAltText="Filter description column" HeaderText="description"
                        SortExpression="description" UniqueName="description">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="completionDescription"
                        FilterControlAltText="Filter completionDescription column"
                        HeaderText="completionDescription" SortExpression="completionDescription"
                        UniqueName="completionDescription">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="startDate" DataType="System.DateTime"
                        FilterControlAltText="Filter startDate column" HeaderText="startDate"
                        SortExpression="startDate" UniqueName="startDate" DataFormatString="{0:d}">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="endDate" DataType="System.DateTime"
                        FilterControlAltText="Filter endDate column" HeaderText="endDate"
                        SortExpression="endDate" UniqueName="endDate" DataFormatString="{0:d}">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="termsAndConditions"
                        FilterControlAltText="Filter termsAndConditions column"
                        HeaderText="termsAndConditions" SortExpression="termsAndConditions"
                        UniqueName="termsAndConditions">
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="lastUpdated" DataType="System.DateTime"
                        FilterControlAltText="Filter lastUpdated column" HeaderText="lastUpdated"
                        SortExpression="lastUpdated" UniqueName="lastUpdated" DataFormatString="{0:d}">
                     </telerik:GridBoundColumn>
                     <telerik:GridCheckBoxColumn DataField="active" DataType="System.Boolean"
                        FilterControlAltText="Filter active column" HeaderText="active"
                        SortExpression="active" UniqueName="active">
                     </telerik:GridCheckBoxColumn>
                   
                </Columns>
                  <EditFormSettings EditFormType="Template">
  
<EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>
  
                <FormTemplate>
  
                    <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
  
                        style="border-collapse: collapse;">
  
                        <tr class="EditFormHeader">
  
                            <td colspan="2" style="font-size: small">
  
                                <b>Training Details</b>
  
                            </td>
  
                        </tr>
  
                        <tr>
  
                            <td colspan="2">
  
                                <b>Training Info:</b>
  
                            </td>
  
                        </tr>
  
                        <tr>
  
                            <td>
  
                                <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
  
                                    <tr>
  
                                        <td>
  
                                        </td>
  
                                        <td>
  
                                        </td>
  
                                    </tr>
  
                                    <tr>
  
                                        <td>
  
                                            Name:
  
                                        </td>
  
                                        <td>
  
                                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("name") %>'>
  
                                            </asp:TextBox>
  
                                        </td>
  
                                    </tr>
  
                                    <tr>
  
                                        <td>
  
                                           Description:
  
                                        </td>
  
                                        <td>
  
                                            <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("description") %>' TabIndex="1" TextMode="MultiLine"
  
                                                Rows="2" Columns="20">
  
                                            </asp:TextBox>
  
                                        </td>
  
                                    </tr>
  
                                    <tr>
  
                                        <td>
  
                                           Completion Description:
  
                                        </td>
  
                                        <td>
  
                                            <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("completionDescription") %>' TabIndex="2" TextMode="MultiLine"
  
                                                Rows="5" Columns="40">
  
                                            </asp:TextBox>
  
                                        </td>
  
                                    </tr>
  
                                    <tr>
  
                                        <td>
  
                                            Start Date:
  
                                        </td>
  
                                        <td>
  
                                            <telerik:RadDatePicker ID="StartDatePicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("startDate") %>'
  
                                                TabIndex="4">
  
                                            </telerik:RadDatePicker>
                                              <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="StartDatePicker"
  
                        ErrorMessage="Enter a date!" CssClass="Validator"></asp:RequiredFieldValidator>
  
                                        </td>
  
                                    </tr>
                                      <tr>
  
                                        <td>
  
                                            Start Date:
  
                                        </td>
  
                                        <td>
  
                                            <telerik:RadDatePicker ID="EndDatePicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("endDate") %>'
  
                                                TabIndex="4">
  
                                            </telerik:RadDatePicker>
                                             <asp:RequiredFieldValidator runat="server" ID="Requiredfieldvalidator2" ControlToValidate="EndDatePicker"
  
                        ErrorMessage="Enter a date!" CssClass="Validator"></asp:RequiredFieldValidator>
  
                                        </td>
  
                                    </tr>
                                      <tr>
  
                <td colspan="3">
  
                    <asp:CompareValidator ID="dateCompareValidator" runat="server" ControlToValidate="EndDatePicker"
  
                        ControlToCompare="StartDatePicker" Operator="GreaterThan" Type="Date" CssClass="Validator" ErrorMessage="The second date must be after the first one.<br /><br />">
  
                    </asp:CompareValidator>
  
                </td>
  
            </tr>
                                       <tr>
  
                                        <td>
  
                                          Active?:
  
                                        </td>
  
                                        <td>
  
                                    
                        
                                
<asp:CheckBox ID="CheckBox11" runat="server" Checked='<%# Eval("active") == DBNull.Value? false: Convert.ToBoolean(Eval("active")) %>'  />
  
                                    </tr>
  
                                </table>
  
                            </td>
  
                            <td>
  
                                <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
  
                                    <tr>
  
                                        <td>
  
                                            Terms and Conditions:
  
                                        </td>
  
                                    </tr>
  
                                    <tr>
  
                                        <td>
               
                                              <telerik:RadEditor ID="RadEditor1" runat="server" content='<%# Bind("termsAndConditions") %>' Width="300px"  >
                                     <Modules>
  
                                   <telerik:EditorModule Name="RadEditorStatistics" Visible="false" Enabled="true">
  
                                   </telerik:EditorModule>
  
                                   <telerik:EditorModule Name="RadEditorDomInspector" Visible="false" Enabled="true">
  
                                   </telerik:EditorModule>
  
                                   <telerik:EditorModule Name="RadEditorNodeInspector" Visible="false" Enabled="true">
  
                                   </telerik:EditorModule>
  
                                   <telerik:EditorModule Name="RadEditorHtmlInspector" Visible="false" Enabled="true">
  
                                   </telerik:EditorModule>
  
                              </Modules>
                               </telerik:RadEditor>
  
                                        </td>
  
                                    </tr>
  
                                </table>
  
                            </td>
  
                        </tr>
  
     
 
                        <tr>
  
                            <td align="right" colspan="2">
  
                                <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"></asp:Button>
  
                            </td>
  
                        </tr>
  
                    </table>
  
                </FormTemplate>
  
            </EditFormSettings>
             </MasterTableView>
              <ClientSettings>
  
            <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
  
        </ClientSettings>
         </telerik:RadGrid>
  
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        InsertMethod="InsertTraining" OldValuesParameterFormatString="original_{0}"
        SelectMethod="GetData"
        TypeName="dsTrainingTableAdapters.TrainingDetailsTableAdapter"
        UpdateMethod="UpdateTraining" onupdating="ObjectDataSource1_Updating"
        oninserting="ObjectDataSource1_Inserting">
         <InsertParameters>
             <asp:Parameter Name="name" Type="String" />
               <asp:Parameter Name="description" Type="String" />
             <asp:Parameter Name="completionDescription" Type="String" />
             <asp:Parameter Name="startDate" Type="DateTime" />
             <asp:Parameter Name="endDate" Type="DateTime" />
             <asp:Parameter Name="termsAndConditions" Type="String" />
             <asp:Parameter Name="lastUpdated" Type="DateTime" />
             <asp:Parameter Name="active" Type="Boolean"/>
         </InsertParameters>
         <UpdateParameters>
             <asp:Parameter Name="name" Type="String" />
             <asp:Parameter Name="description" Type="String" />
             <asp:Parameter Name="completionDescription" Type="String" />
             <asp:Parameter Name="startDate" Type="DateTime" />
             <asp:Parameter Name="endDate" Type="DateTime" />
               <asp:Parameter Name="termsAndConditions" Type="String" />
             <asp:Parameter Name="lastUpdated" Type="DateTime" />
             <asp:Parameter Name="active" Type="Boolean" />
             <asp:Parameter Name="Original_TrainingID" Type="Int32" />
         </UpdateParameters>
     </asp:ObjectDataSource>
  
    </p>
  
 
 
 
 protected void Page_Load(object sender, EventArgs e)
     {
  
    }
     protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
     {
  
        if (e.Exception != null)
         {
             e.ExceptionHandled = true;
             e.KeepInInsertMode = true;
             DisplayMessage(true, "Training ID cannot be inserted. Reason: " + e.Exception.Message);
         }
         else
         {
             DisplayMessage(false, "Training class inserted");
         }
  
    }
     protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
     {
        
        if (e.Exception != null)
         {
             e.KeepInEditMode = true;
             e.ExceptionHandled = true;
             DisplayMessage(true, "TrainingID " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["TrainingID"] + " cannot be updated. Reason: " + e.Exception.Message);
         }
         else
         {
             DisplayMessage(false, "TrainingID " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["TrainingID"] + " updated");
         }
  
    }
  
    private void DisplayMessage(bool isError, string text)
     {
         Label label = (isError) ? this.Label1 : this.Label2;
         label.Text = text;
  
    }
     protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
     {
          
        if (e.CommandName == RadGrid.InitInsertCommandName)
         {
  
           GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            editColumn.Visible = false;
  
           GridEditableItem editedItem = e.Item as GridEditableItem;
             e.Canceled = true;
             //Prepare an IDictionary with the predefined values 
            System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
             //set initial checked state for the checkbox on init insert 
            newValues["active"] = true;
             //Insert the item and rebind 
            e.Item.OwnerTableView.InsertItem(newValues);
  
        }
  
        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
         {
             e.Canceled = true;
         }
  
        else
         {
  
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
             if (!editColumn.Visible)
                  editColumn.Visible = true;
  
        }
  
    }
    
    protected void RadGrid1_PreRender(object sender, EventArgs e)
     {
  
        if (!Page.IsPostBack)
         {
             RadGrid1.EditIndexes.Add(0);
             RadGrid1.Rebind();
         }
  
    }
  
    protected void ObjectDataSource1_Updating(object sender, ObjectDataSourceMethodEventArgs e)
     {
         e.InputParameters["lastUpdated"] = DateTime.Now;
     }
     protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
     {
         e.InputParameters["lastUpdated"] = DateTime.Now;
     }
     

0
Viktor Tachev
Telerik team
answered on 21 Oct 2013, 02:33 PM
Hello,

It seems that the value for the CheckBox control is generated with Eval(), which is used as evaluating expression. It provides one-way binding for the control. Try using Bind() for the value of the Checked property.

<asp:CheckBox ID="CheckBox11" runat="server" Checked='<%# Bind("active") %>' />

Let me know if this approach is working for you.

Regards,
Viktor Tachev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Leland
Top achievements
Rank 1
answered on 21 Oct 2013, 03:12 PM
I tried to use Bind method but I got this Specified cast is not valid
Thanks
0
Q
Top achievements
Rank 1
answered on 21 Oct 2013, 03:16 PM
I got error message "Specified cast is not valid " when I tried to use Bind method. Please help
0
Viktor Tachev
Telerik team
answered on 23 Oct 2013, 03:40 PM
Hello,

Thank you for writing back.

When trying to insert a new record the RadGrid opens an edit form and tries to bind the values in it with the values from the datasource. As this data is not existent in insert mode the returned values are null. The RadGrid is trying to convert null to the corresponding data type for each column which results in the mentioned exception.

In order to avoid this you need to set a default value for the columns where two-way data binding is set and their values need to be updated in the data source. The approach for implementing this is illustrated in the "Setting predefined values for different column editors" section in this article. Note that the value for the DefaultInsertValue property is of type System.String.

Also ensure that the column in the database bound to the CheckBox control is of type Boolean.

I hope this information helps.

Regards,
Viktor Tachev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Q
Top achievements
Rank 1
answered on 28 Oct 2013, 06:17 PM
Thank you very much for your help. It's work :)
Tags
Grid
Asked by
Q
Top achievements
Rank 1
Answers by
Q
Top achievements
Rank 1
Viktor Tachev
Telerik team
Leland
Top achievements
Rank 1
Share this question
or