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

Cannot insert using user control

6 Answers 105 Views
Grid
This is a migrated thread and some comments may be shown as answers.
BMEA
Top achievements
Rank 1
BMEA asked on 07 Dec 2011, 10:12 PM

Hi
I have been trying to use the RadGrid to maintain emergency contact information. I am having a difficulty getting my user control to perform the actual insert. I have looked through the documentation and still cannot figure out what I am doing wrong. My grid comes up ok and when I click on Add new record, my user control comes up and I can fill in the information. I even have validators on the page that fires ok. When I click the Insert button, the form returns and no record is added. I handle the InsertCommand event on the server side but it does not seem to get there. I really have an urgent need to move past this problem, so your urgent response here is very welcome.

Any leads will be very helpful. Here is my code

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pEmergencyContact.aspx.cs" Inherits="ARM.ETF.pEmergencyContact" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 760px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="grdEmergency">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="grdEmergency" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
        </telerik:RadAjaxLoadingPanel>
        <table class="style1">
            <tr>
                <td>
                    <asp:Panel ID="pnlGrid" runat="server">
                        <telerik:RadGrid ID="grdEmergency" runat="server" AutoGenerateColumns="False"
                            OnInsertCommand="grdEmergency_InsertCommand"
                            OnNeedDataSource="grdEmergency_NeedDataSource"
                            CellSpacing="0" GridLines="None">
                            <MasterTableView CommandItemDisplay="Top" AllowAutomaticInserts="false" EditMode="PopUp" >
                                <CommandItemSettings ExportToPdfText="Export to PDF" />
                                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                </RowIndicatorColumn>
                                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                </ExpandCollapseColumn>
                                <Columns>
                                    <telerik:GridBoundColumn DataField="FIRST_NAME"
                                        FilterControlAltText="Filter firstname column" HeaderText="Contact First Name"
                                        UniqueName="firstname">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="LAST_NAME"
                                        FilterControlAltText="Filter lastname column" HeaderText="Contact Last Name"
                                        UniqueName="lastname">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="RELATION"
                                        FilterControlAltText="Filter relationship column" HeaderText="Relation"
                                        UniqueName="relationship">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="TELEPHONE_HOME"
                                        FilterControlAltText="Filter homeTelephone column"
                                        HeaderText="Telephone (Home)" UniqueName="homeTelephone">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="TELEPHONE_BUSINESS"
                                        FilterControlAltText="Filter workTelephone column"
                                        HeaderText="Telephone (Work)" UniqueName="workTelephone">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="TELEPHONE_EXTENSION"
                                        FilterControlAltText="Filter extension column" HeaderText="Extension"
                                        UniqueName="extension">
                                    </telerik:GridBoundColumn>
                                </Columns>
                                <EditFormSettings UserControlName="~/Controls/emergencycontact.ascx" EditFormType="WebUserControl">
                                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                    </EditColumn>
                                </EditFormSettings>
                            </MasterTableView>
                            <FilterMenu EnableImageSprites="False">
                            </FilterMenu>
                            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                            </HeaderContextMenu>
                        </telerik:RadGrid>
                    </asp:Panel>
                    </td>
            </tr>
            <tr>
                <td>
                </td>
            </tr>
        </table>
     
    </div>
    </form>
</body>
</html>

ASCX

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="emergencycontact.ascx.cs" Inherits="ARM.ETF.Controls.emergencycontact" %>
<%@ Import Namespace="ARM.Common.Classes" %>
<style type="text/css">
        .grdTable
        {
            font-size: 12px;
            font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif;
            margin: 0px;
            padding: 0px;
            color: #696969;           
            width: 360px;
        }
        .grdText
        {
            font-family: 'Segoe UI';
            font-size: 12px;
            color: #000000;
            background-color: #EAEAEA;
            border: 1px solid #CCCCCC;
        }
        .grdlabel
        {
            width: 100px;
        }
    </style>
    <table class="grdTable">
        <tr>
            <td class="grdlabel">
                <asp:Label ID="lblgrdFirstName" runat="server" Text="lblgrdFirstName"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtgrdFirstName" Runat="server" Width="200px" CssClass="grdText"></asp:TextBox
                <asp:RequiredFieldValidator ID="rfvgrdFirstName" runat="server" ControlToValidate="txtgrdFirstName" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="grdlabel">
                <asp:Label ID="lblgrdLastName" runat="server" Text="lblgrdLastName"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtgrdLastName" Runat="server" Width="200px" CssClass="grdText"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvgrdLastName" runat="server" ControlToValidate="txtgrdLastName" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="grdlabel">
                <asp:Label ID="lblgrdHomeTelephone" runat="server" Width="100px" Text="lblgrdHomeTelephone"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtgrdHomeTelephone" Runat="server" CssClass="grdText"></asp:TextBox
                <asp:RequiredFieldValidator ID="rfvgrdHomeTelephone" runat="server" ControlToValidate="txtgrdHomeTelephone" ErrorMessage="*"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="revgrdHomeTelephone" runat="server" ControlToValidate="txtgrdHomeTelephone" ValidationExpression="^[2-9]\d{2}-\d{3}-\d{4}$" ErrorMessage="*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="grdlabel">
                <asp:Label ID="lblgrdWorkTelephone" runat="server" Text="lblgrdWorkTelephone"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtgrdWorkTelephone" Runat="server" Width="100px" CssClass="grdText"></asp:TextBox> <asp:RegularExpressionValidator
                    ID="revgrdWorkTelephone" runat="server" ControlToValidate="txtgrdWorkTelephone" ValidationExpression="^[2-9]\d{2}-\d{3}-\d{4}$" ErrorMessage="*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="grdlabel">
                <asp:Label ID="lblgrdExtension" runat="server" Text="lblgrdExtension"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtgrdExtension" Runat="server" Width="70px" CssClass="grdText"></asp:TextBox> <asp:RegularExpressionValidator
                    ID="revExtension" runat="server" ControlToValidate="txtgrdExtension" ValidationExpression="^\d+$" ErrorMessage="*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="grdlabel">
                <asp:Label ID="lblgrdRelation" runat="server" Text="lblgrdRelation"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="cbogrdRelation" runat="server" CssClass="grdText"></asp:DropDownList
                <asp:RequiredFieldValidator ID="rfvgrdRelation" runat="server" ControlToValidate="cbogrdRelation" InitialValue="0" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btngrdSave" CommandName="PerformInsert" runat="server"
                    Text="btngrdSave" />
            </td>
 
        </tr>
    </table>

Page Code Behind

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ARM.Common.Classes;
using YorkU.Data;
using Telerik.Web.UI;
 
namespace ARM.ETF
{
    public partial class pEmergencyContact : System.Web.UI.Page
    {
        SqlServer sql = new SqlServer();
        Command cmd = new Command();
        DataFactory df = new DataFactory();
        private string MyToken = "7EC07A5015A945A4A49037E315E9F8C3";
        private int MyPerson = 2;
 
        protected override void OnInit(EventArgs e)
        {
            grdEmergency.InsertCommand += new GridCommandEventHandler(grdEmergency_InsertCommand);
            grdEmergency.NeedDataSource += new GridNeedDataSourceEventHandler(grdEmergency_NeedDataSource);
            base.OnInit(e);
        }
 
        protected void grdEmergency_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.grdEmergency.MasterTableView.Items[0].Edit = true;
            ConfigureGrid();
        }
 
        protected void grdEmergency_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.PerformInsertCommandName)
            {
                GridEditableItem itm = e.Item as GridEditableItem;
                UserControl uc = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
 
                TextBox txtgrdFirstName = uc.FindControl("txtgrdFirstName") as TextBox;
                TextBox txtgrdLastName = uc.FindControl("txtgrdLastName") as TextBox;
                TextBox txtgrdHomeTelephone = uc.FindControl("txtgrdHomeTelephone") as TextBox;
                TextBox txtgrdWorkTelephone = uc.FindControl("txtgrdWorkTelephone") as TextBox;
                TextBox txtgrdExtension = uc.FindControl("txtgrdExtension") as TextBox;
                DropDownList cbogrdRelation = uc.FindControl("cbogrdRelation") as DropDownList;
 
                string firstName = txtgrdFirstName == null ? string.Empty : txtgrdFirstName.Text;
                string lastName = txtgrdLastName == null ? string.Empty : txtgrdLastName.Text;
                string homeTel = txtgrdHomeTelephone == null ? string.Empty : txtgrdHomeTelephone.Text;
                string workTel = txtgrdWorkTelephone == null ? string.Empty : txtgrdWorkTelephone.Text;
                string extn = txtgrdExtension == null ? string.Empty : txtgrdExtension.Text;
                int relation = cbogrdRelation == null ? 0 : int.Parse(cbogrdRelation.SelectedItem.Value);
 
                cmd.CommandText = ETFConstants.sp_CreateEmergencyContact;
                cmd.CommandType = CommandType.StoredProcedure;
 
                cmd.ParameterAdd("@PersonID", DbType.Int32, MyPerson);
                cmd.ParameterAdd("@FirstName", DbType.String, firstName);
                cmd.ParameterAdd("@LastName", DbType.String, lastName);
                cmd.ParameterAdd("@Relation", DbType.Int32, relation);
                cmd.ParameterAdd("@HomeTelephone", DbType.String, homeTel);
                cmd.ParameterAdd("@WorkTelephone", DbType.String, workTel);
                cmd.ParameterAdd("@Extension", DbType.String, extn);
 
                sql.ExecuteNonQuery(cmd);
            }
 
        }
 
 
 
        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigureGrid();
        }
 
        private void ConfigureGrid()
        {
            grdEmergency.DataSource = df.GetERContactData(MyToken);
            grdEmergency.DataBind();
        }
 
    }
}

And User Control Code Behind

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YorkU.Data;
using ARM.Common.Classes;
using Telerik.Web.UI;
 
namespace ARM.ETF.Controls
{
    public partial class emergencycontact : UserControl
    {
        DataFactory df = new DataFactory();
        public const int MyPerson = 2;
 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
 
 
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadLabels();
            LoadCombo();
        }
 
        private void LoadLabels()
        {
            lblgrdExtension.Text = ETFConstants.Label_Extension;
            lblgrdFirstName.Text = ETFConstants.Label_FirstName;
            lblgrdHomeTelephone.Text = ETFConstants.Label_TelephoneHome;
            lblgrdLastName.Text = ETFConstants.Label_LastName;
            lblgrdRelation.Text = ETFConstants.Label_Relation;
            lblgrdWorkTelephone.Text = ETFConstants.Label_TelephoneWork;
 
            btngrdSave.Text = ETFConstants.Btn_Save;
        }
 
        private void LoadCombo()
        {
            ListItem itm;
            DataTableReader dtr;
             
            cbogrdRelation.Items.Clear();
 
            itm = new ListItem();
            itm.Text = ETFConstants.ComboDefaultText;
            itm.Value = ETFConstants.ComboDefaultValue;
 
            cbogrdRelation.Items.Add(itm);
 
            dtr = df.GetRelations();
 
            while (dtr.Read())
            {
                itm = new ListItem();
 
                itm.Text = dtr["REFERENCE"].ToString();
                itm.Value = dtr["CODE"].ToString();
 
                cbogrdRelation.Items.Add(itm);
            }
        }
    }
 
}

6 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 12 Dec 2011, 03:57 PM
Hello,

Could you verify that you are entering in the event handler? You could check this by placing a break point right before the if statement(you could do that by placing the cursor on the desired line and hit F9 key or through the Debug menu by choosing Toggle Breakpoint).

If the event does not fire, that mean something is preventing you to perform postback to the server. Most probably one of the validator controls.

If you are getting in the event, you could execute the code step-by-step with the F10 key and to check whether it finds the UserControl.

Other than that your code looks O.K.

Kind regards,
Andrey
the Telerik team
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 their blog feed now
0
BMEA
Top achievements
Rank 1
answered on 12 Dec 2011, 07:49 PM
Thank you for your response. I performed this check before posting this thread. The page posts back to the server (Fired the page load event) but it does not handle the InsertCommand event. It definitely is not the validators because I see postback happening but I cannot understand why my event is not firing. I am so frustrated with my efforts and with my deadline fast approaching, I have to find a non-telerik solution, something I am really disappointed in doing.

Is it possible to use the code I supplied, get it working on your end (I tried copying your sample code and that did not work either) and then post the sample here? I am sure I am not alone in this situation. It would benefit others who visit this site regularly. Of course, you have to change the database but I would prefer to not use the SQLDataSource object but rather, the method I am using.

Thanks
0
Andrey
Telerik team
answered on 13 Dec 2011, 11:05 PM
Hi,

If you are making postback to the server, your event should be fired unless you have missed to hook it. But I see that you have attached the handler the right way. Because of that I made a sample project which is following your scenario. You could compare the code in this sample project with yours and check what are the differences.

Additionally, there are some things that I noticed recently:

As this article says, do not call DataBind when you are using advanced databinding with NeedDataSource event. Also, remove the call to the ConfigureGrid method from the Page_Load event handler. Try commenting this line of code:

this.grdEmergency.MasterTableView.Items[0].Edit = true;

Give these suggestion a try and see whether the issue is resolved.

Greetings,
Andrey
the Telerik team
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 their blog feed now
0
BMEA
Top achievements
Rank 1
answered on 15 Dec 2011, 08:10 PM
Hi Andrey
Thank you for your suggestions. After copying one of the demos again and changing it bit by bit, it eventually worked. Looking back my mistake seemed to be the omission of a DataKeyNames value for the grid's MasterTableView. I now have the grid working exactly as I want it. Now here's the problem. I copied the grid into a RadPageView of a multipage tab control. Now it does not work! The OnInsertCommand, OnUpdateCommand and OnDeleteCommand events don't seem to be firing but I am sure it posts back as it has hit Page Load. Can you suggest possible things to look out for? What is the difference when you place the grid on a RadPageView as opposed to having it by itself? What would cause these events not to fire?
0
Andrey
Telerik team
answered on 16 Dec 2011, 11:07 AM
Hello,

There is no change in the behavior of RadGrid when it is placed inside RadMultiPage and RadTabStrip controls. These controls are all part from the RadControls suite and they are developed with the idea to be compatible with each other.

Additionally, the events are the only thing you could be sure that will be raised, there is no way to not fire event if you configure your control(no matter if the control is part from our suite or is it from Microsoft ASP.NET suite) properly.

Moreover, you should check for any JavaScript errors,  do you have proper Ajax settings, how you are adding the user control to the page? I am writing this because you said that you have modified the code and I am not aware of the current setup.

Also, I have modified the sample project to show you how RadGrid should be set up in order to work as expected. You should use the sample project set up to find what could be causing the issue.

After all these steps have been tried and if you are still experiencing the issue, please open formal support ticket and send us sample project which is replicating the issue.


Kind regards,
Andrey
the Telerik team
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 their blog feed now
0
BMEA
Top achievements
Rank 1
answered on 16 Dec 2011, 03:39 PM
Hi Andrey
Thank you very much for your help in getting this done. In using your code I discovered what I was doing wrong. I was calling a method to create the grid on each postback rather than using the NeedDataSource method. The grid works perfectly now.

Thanks again
Tags
Grid
Asked by
BMEA
Top achievements
Rank 1
Answers by
Andrey
Telerik team
BMEA
Top achievements
Rank 1
Share this question
or