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

FormTemplate Binding Errors - DropDownList and CheckBox

1 Answer 101 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 16 Feb 2015, 09:02 PM
After hours of research, I have figured out my own solution to some issues I reported earlier. So to summarize all my issues and solutions:

I experienced two major BINDING issues with fields under <EditFormSettings EditFormType="Template"><FormTemplate>:

1. DropDownList - Setting the SelectedValue property to <%# Bind("security_webpage_id") %> caused an error when clicking on the Add New Record button. The error was 'ddlWebPages' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value.

Fix: I added a blank listitem. My Dropdown tag looked like this after fix:

<asp:DropDownList ID="ddlWebPages" DataSourceID='sqlDataSourceWebpages'
                                               AppendDataBoundItems="True" DataTextField="webpage_name" DataValueField="ID"
                                               SelectedValue='<%# Bind("security_webpage_id") %>' runat="server"> 
                                      <asp:ListItem Text=" " Value=""></asp:ListItem>
                                          
</asp:DropDownList>
 
2. Checkbox - I had three checkboxes that received an error when I clicked on the Add New Record button.
The error was "Specified cast is not valid".

Fix: I created a User Control for the CheckBox and handled the passing of null value to the Checked property which is what was happening when the Add New Record button was clicked. Below is my code for the code behind and markup of the checkbox control.

Code-Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace FRC
{
    public partial class CheckBoxNullableChecked : System.Web.UI.UserControl
    {
        private bool m_checked = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            m_checked = CheckBox1.Checked;
        }
 
        public object Checked
        {
            get { return m_checked; }
            set
            {
                if (value.GetType() == DBNull.Value.GetType()) m_checked = false;
                else if (value == null) m_checked = false;
                else if (value.GetType() == typeof(bool)) m_checked = (bool)value;
                else m_checked = false;
            }
        }
        public string Text
        {
            get { return this.CheckBox1.Text; }
            set { this.CheckBox1.Text = value; }
        }
 
        protected void Page_PreRender()
        {
            CheckBox1.Checked = m_checked;
        }
 
    }
}



Mark-up:
%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxNullableChecked.ascx.cs" Inherits="FRC.CheckBoxNullableChecked" %>
<asp:CheckBox ID="CheckBox1" runat="server" />


Registering User Control in markup file that contains the standard Checkbox;
<%@ Register src="~/CheckBoxNullableChecked.ascx" TagName="CheckBoxNullableChecked"
    TagPrefix="uc1" %>

Your Checkbox and/or Drop Down List will look like this now under the FormTemplate:
<EditFormSettings EditFormType="Template">
               <FormTemplate>   
                   
                  
                   <div id="divGridEdit" class="divGrid">
                           <b>Add New Role to Webpage Association</b>
                           <br /><br />
                           <table>
 
                               <tr>
                                   <td><asp:Label Text="Web Page:" runat="server"></asp:Label></td>
                                   <td>
                                      <asp:DropDownList ID="ddlWebPages" DataSourceID='sqlDataSourceWebpages'
                                              AppendDataBoundItems="True" DataTextField="webpage_name" DataValueField="ID"
                                              SelectedValue='<%# Bind("security_webpage_id") %>' runat="server">
                                     <asp:ListItem Text=" " Value=""></asp:ListItem>
                                          
                                        </asp:DropDownList>
 
 
                                   </td>
                               </tr>
                               
                               <tr>
                                   <td><br /></td>
                                   <td></td>
                               </tr>
                               <tr>
                                    <td><asp:Label Text="Allow Add:" runat="server"></asp:Label></td>
                                    <td><uc1:CheckBoxNullableChecked ID="chkboxAllowAdd" Checked='<%# Bind("add_privledge") %>' runat="server" />
                                    </td>
                               </tr>
                               <tr>
                                    <td><asp:Label Text="Allow Edit:" runat="server"></asp:Label></td>
                                    <td><uc1:CheckBoxNullableChecked ID="chkboxAllowEdit" Checked='<%# Bind("edit_privledge") %>' runat="server" />
                                    </td>
                               </tr>
                               <tr>
                                    <td><asp:Label Text="Allow Delete:" runat="server"></asp:Label></td>
                                    <td><uc1:CheckBoxNullableChecked ID="chkboxAllowDelete" Checked='<%# Bind("delete_privledge") %>' runat="server" /></td>
                               </tr>
 
                               <tr>
                                   <td><asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                       CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'  runat="server" /></td>
                                   <td><asp:Button ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false" /></td>
                               </tr>
                           </table>
                       </div>            
 
               </FormTemplate>
           </EditFormSettings>
 
 
           </MasterTableView>
           <ClientSettings>
               <ClientEvents OnRowDblClick="rowDblClick" />
           </ClientSettings>
               
       </telerik:RadGrid>
 
 
   </div>
 
   <asp:SqlDataSource ID="sqlDataSourceWebpages" runat="server" SelectCommand="SELECT * FROM dbo.security_webpage" ConnectionString="<%$ ConnectionStrings:GATEConnectionString %>"></asp:SqlDataSource>

Hopefully this will help someone else not have to go through days of pain that I did over there two issues

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 19 Feb 2015, 03:03 PM
Hello David,

Thank you for sharing your solution with our community. As an alternative, you can also use the DataBinding event handler of the controls to configure their settings.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or