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

[Solved] Radgrid "popup editor" related questions

5 Answers 187 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John Haines
Top achievements
Rank 1
John Haines asked on 07 Sep 2009, 04:49 PM
I have a RadGrid that is on a .ascx webUserControl. In that radgrid I have defined another webusercontrol that I want to use as the editor form the when the user clicks "edit" on a row in the grid.

Because I want to display validator messages on the popup form, I coded the onclick events for the insert and update buttons as such:

         <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update"
            Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>' 
            OnClick="btnUpdate_Click" TabIndex="6" ></asp:Button> 
         <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert" 
            Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>' OnClick="btnInsert_Click" 
            TabIndex="7"></asp:Button> 
         &nbsp; 
         <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
            CommandName="Cancel" TabIndex="8"></asp:Button> 
 

I have several issues related to this for processing when the user clicks insert or update:

1. messages caught on the server side are not displaying on the popup form. (e.g. foreigh key violations, etc)
2. i cannot figure out how to exit the form if the user successfully enters the data.

Here is the full ascx for this control...

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProposalPartDetails.ascx.cs" 
   Inherits="UserControls_ProposalPartDetails" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<table id="Table1" cellspacing="1" cellpadding="1" border="0" width="600px"
   <tr> 
      <td colspan="2" align="center"
         <asp:ValidationSummary ID="ValidationSummary" runat="server" Font-Bold="true" DisplayMode="BulletList" /> 
         <asp:Label ID="Message" runat="server"></asp:Label> 
         <asp:CustomValidator ID="GeneralValidator" runat="server" Display="Dynamic" /> 
      </td> 
   </tr> 
   <tr> 
      <td> 
         Part Number: 
      </td> 
      <td class="style1"
         <telerik:RadTextBox ID="PartNumber" Text='<%# Bind("PartNumber") %>' runat="server" 
            EmptyMessage="Enter a Part Number" TabIndex="1" MaxLength="21" Width="222px" SelectionOnFocus="SelectAll"
         </telerik:RadTextBox> 
         <asp:RequiredFieldValidator ID="PartNumberValidator" runat="server" ControlToValidate="PartNumber" 
            Display="Dynamic" Text="Please enter a part number" /> 
      </td> 
   </tr> 
   <tr> 
      <td> 
         Part Description: 
      </td> 
      <td class="style1"
         <telerik:RadTextBox ID="Description" Text='<%# Bind("Description") %>' runat="server" Width="222px" EmptyMessage="Enter a Part Description" 
            TabIndex="2" MaxLength="35" SelectionOnFocus="SelectAll"
         </telerik:RadTextBox> 
         <asp:RequiredFieldValidator ID="DescriptionValidator" runat="server" ControlToValidate="Description" 
            Display="Dynamic" Text="please enter a description" /> 
      </td> 
   </tr> 
   <tr> 
      <td> 
         Part Type 
      </td> 
      <td class="style1"
         <telerik:RadComboBox ID="PartType" runat="server" SelectedValue='<%# Bind("PartType") %>' 
            EmptyMessage="Select a Part Type" Height="200px" Width="300px" TabIndex="3" AllowCustomText="true" 
            MarkFirstMatch="true"
         </telerik:RadComboBox> 
         <asp:RequiredFieldValidator ID="PartTypeValidator" Text="please select a part type" 
            runat="server" ControlToValidate="PartType" /> 
      </td> 
   </tr> 
   <tr> 
      <td> 
         UOM 
      </td> 
      <td class="style1"
         <telerik:RadComboBox ID="UnitOfMeasure" runat="server" SelectedValue='<%# Bind("UnitOfMeasure") %>' 
            EmptyMessage="Select a Unit of Measure" Height="200px" Width="400px" TabIndex="4" 
            AllowCustomText="true" MarkFirstMatch="true"
         </telerik:RadComboBox> 
         <asp:RequiredFieldValidator ID="UnitOfMeasureValidator" Text="please select a unit of measure" 
            runat="server" ControlToValidate="UnitOfMeasure" /> 
      </td> 
   </tr> 
   <tr> 
      <td> 
         Comm Code 
      </td> 
      <td class="style1"
         <telerik:RadComboBox ID="CommodityCode" runat="server" SelectedValue='<%# Bind("CommodityCode") %>' 
            EmptyMessage="Select a Commodity Code" Height="200px" Width="400px" TabIndex="5" 
            AllowCustomText="true" MarkFirstMatch="true"
         </telerik:RadComboBox> 
         <asp:CustomValidator ID="CommodityCodeValidator" runat="server" /> 
      </td> 
   </tr> 
   <tr> 
      <td align="right" colspan="2"
         <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>' 
            OnClick="btnUpdate_Click" TabIndex="6" ></asp:Button> 
         <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert" 
            Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>' OnClick="btnInsert_Click" 
            TabIndex="7"></asp:Button> 
         &nbsp; 
         <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
            CommandName="Cancel" TabIndex="8"></asp:Button> 
      </td> 
   </tr> 
   <tr> 
      <td colspan="2"
         <asp:TextBox ID="ProposalPartId" Text='<%# Bind("Id") %>' runat="server" Visible="false" 
            TabIndex="8" /> 
      </td> 
   </tr> 
</table> 
 

Here is the full code-behind for the popup control

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Web.UI; 
using Harris.Common.ModelStateWrapper_ASPNET; 
using Harris.Project.Mammoth.Domain; 
using Harris.Project.Mammoth.ServiceManager; 
using Telerik.Web.UI; 
 
public partial class UserControls_ProposalPartDetails : UserControl 
    private object _dataItem; 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        PartNumber.Focus(); 
 
        //if (!Page.IsPostBack) 
        //{ 
        //    LoadListBoxes(); 
        //} 
    } 
    #region Web Form Designer generated code 
    override protected void OnInit(EventArgs e) 
    { 
        // 
        // CODEGEN: This call is required by the ASP.NET Web Form Designer. 
        // 
        InitializeComponent(); 
        base.OnInit(e); 
    } 
 
    /// <summary> 
    ///        Required method for Designer support - do not modify 
    ///        the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
 
        this.DataBinding += new System.EventHandler(this.ProposalPartDetails_DataBinding); 
 
    } 
    #endregion 
 
    public object DataItem 
    { 
        get 
        { 
            return _dataItem; 
        } 
        set 
        { 
            _dataItem = value; 
        } 
    } 
 
    protected void ProposalPartDetails_DataBinding(object sender, EventArgs e) 
    { 
        LoadListBoxes(); 
    } 
 
    private void LoadListBoxes() 
    { 
        Common.SessionWrapper sessionStateWrapper = new Common.SessionWrapper(); 
 
        // populate the part type list 
        var pts = sessionStateWrapper.GetPartTypeListBoxEntries(); 
        PartType.DataSource = pts; 
        PartType.DataTextField = PartTypeConstants.DescriptionFieldname; 
        PartType.DataValueField = PartTypeConstants.CodeFieldname; 
        //PartType pt = (PartType)DataItem; 
        //string originalPartType = (string.IsNullOrEmpty(Session["originalPartType"].ToString())) ? string.Empty : Session["originalPartType"].ToString(); 
        //PartType.SelectedValue = IsPartTypeInList(pt.Code, pts) ? pt.Code : originalPartType; 
        PartType.DataBind(); 
 
        // populate the uom list 
        var uoms = sessionStateWrapper.GetUnitOfMeasureListBoxEntries(); 
        UnitOfMeasure.DataSource = uoms; 
        UnitOfMeasure.DataTextField = UnitOfMeasureConstants.DescriptionFieldname; 
        UnitOfMeasure.DataValueField = UnitOfMeasureConstants.CodeFieldname; 
        UnitOfMeasure.DataBind(); 
        object value = DataBinder.Eval(DataItem, ProposalPartConstants.UnitOfMeasureFieldname); 
        if (value == DBNull.Value) 
        { 
            UnitOfMeasure.SelectedValue = "EA"
        } 
 
        // populate the commodity code list 
        var commCodes = sessionStateWrapper.GetCommodityCodeListBoxEntries(); 
        CommodityCode.DataSource = commCodes; 
        CommodityCode.DataTextField = CommodityCodeConstants.DescriptionFieldname; 
        CommodityCode.DataValueField = CommodityCodeConstants.CodeFieldname; 
        CommodityCode.DataBind(); 
    } 
 
    private bool IsPartTypeInList(string code, IList<PartType> partTypes) 
    { 
        code = code.Trim().ToUpper(); 
 
        foreach (var partType in partTypes) 
        { 
            if (code == partType.Code) 
            { 
                return true
            } 
        } 
 
        return false
    } 
 
    protected void btnUpdate_Click(object sender, EventArgs e) 
    { 
        Message.Text = string.Empty; 
 
        string unitOfMeasure = UnitOfMeasure.SelectedValue; 
        string partType = PartType.SelectedValue; 
 
        // this section handles when the user enters a value not in the list (client side validator does not kick in) 
        Common propCode = new Common(Session, Request); 
        var modelStateWrapper = new ModelStateWrapper(propCode.GetDictionary()); 
        if (string.IsNullOrEmpty(partType)) 
        { 
            modelStateWrapper.AddError(ProposalPartConstants.PartTypeFieldname, "Please select a part type from the list."); 
        } 
 
        if (string.IsNullOrEmpty(unitOfMeasure)) 
        { 
            modelStateWrapper.AddError(ProposalPartConstants.UnitOfMeasureFieldname, "Please select a unit of measure from the list."); 
        } 
 
        if (modelStateWrapper.GetErrors().Count > 0) 
        { 
            AddErrorsToForm(modelStateWrapper); 
            return
        } 
 
        string partNumber = PartNumber.Text.Trim().ToUpper(); 
        string description = Description.Text.Trim().ToUpper(); 
        string commCode = CommodityCode.SelectedValue; 
 
        const char delim = '-'; 
        string sDelim = delim.ToString(); 
        if (unitOfMeasure.Contains(sDelim)) unitOfMeasure = unitOfMeasure.Split(delim)[0].Trim(); 
        if (partType.Contains(sDelim)) partType = partType.Split(delim)[0].Trim(); 
        if (commCode.Contains(sDelim)) commCode = commCode.Split(delim)[0].Trim(); 
 
        // get part from db 
        string sPartId = ProposalPartId.Text.Trim().ToUpper(); 
        long partId = Convert.ToInt64(sPartId); 
        var proposalPartService = new ProposalPartService(modelStateWrapper); 
        var myPart = proposalPartService.GetProposalPart(partId); 
 
        // save part to db 
        myPart.PartNumber = partNumber; 
        myPart.Description = description; 
        myPart.PartType = partType; 
        myPart.UnitOfMeasure = unitOfMeasure; 
        myPart.CommodityCode = commCode; 
        bool result = proposalPartService.EditProposalPart(myPart); 
 
        // add error messages to the form 
        if (result == false
        { 
            modelStateWrapper = (ModelStateWrapper)proposalPartService.GetModelStateWrapper(); 
            AddErrorsToForm(modelStateWrapper); 
        } 
    } 
 
    protected void btnInsert_Click(object sender, EventArgs e) 
    { 
        Message.Text = string.Empty; 
 
        string partNumber = PartNumber.Text; 
        string description = Description.Text; 
        string unitOfMeasure = UnitOfMeasure.Text; 
        string partType = PartType.SelectedValue; 
        string commCode = CommodityCode.Text; 
 
        const char delim = '-'; 
        string sDelim = delim.ToString(); 
        if (unitOfMeasure.Contains(sDelim)) unitOfMeasure = unitOfMeasure.Split(delim)[0].Trim(); 
        if (partType.Contains(sDelim)) partType = partType.Split(delim)[0].Trim(); 
        if (commCode.Contains(sDelim)) commCode = commCode.Split(delim)[0].Trim(); 
 
        // get proposal service object 
        Common propCode = new Common(Session, Request); 
        var _modelStateWrapper = new ModelStateWrapper(propCode.GetDictionary()); 
        var _proposalPartService = new ProposalPartService(_modelStateWrapper); 
 
        // save it now 
        var partId = propCode.GetCurrentProposalId(); 
        var proposalPart = new ProposalPart(partId, partNumber, description, unitOfMeasure, partType, commCode); 
        bool result = _proposalPartService.CreateProposalPart(proposalPart); 
 
        // add messages to the form 
        if (result == false
        { 
            var msw = _proposalPartService.GetModelStateWrapper(); 
            AddErrorsToForm(msw); 
        } 
        else 
        { 
            Message.Text = "Part " + partNumber + " saved.<br />You may continue adding parts.<br />Click Cancel to exit the form."
        } 
    } 
 
    private void AddErrorsToForm(IModelStateWrapper msw) 
    { 
        foreach (var error in msw.GetErrors()) 
        { 
            switch (error.Key) 
            { 
                case ProposalPartConstants.PartNumberFieldname: 
                    PartNumberValidator.ErrorMessage = error.Value; 
                    break
                case ProposalPartConstants.DescriptionFieldname: 
                    DescriptionValidator.ErrorMessage = error.Value; 
                    break
                case ProposalPartConstants.PartTypeFieldname: 
                    PartTypeValidator.ErrorMessage = error.Value; 
                    break
                case ProposalPartConstants.UnitOfMeasureFieldname: 
                    UnitOfMeasureValidator.ErrorMessage = error.Value; 
                    break
                case ProposalPartConstants.CommodityCodeFieldname: 
                    CommodityCodeValidator.ErrorMessage = error.Value; 
                    break
                default
                    GeneralValidator.ErrorMessage = error.Value; 
                    break
            } 
        } 
    } 
 

Can you help me figure out how to get these two items to behave the way I want to to?

thanks,

- John

5 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 08 Sep 2009, 01:27 PM
Hi John,

I went through your code and noticed that you are calling DataBind() method. Please note that Simple data-binding can be used in simple scenarios which does not require complex operations like insert/delete/update.
Please bind your grid using NeedDataSource event or declarative data sources in order to achieve the desired functionality.

I hope this information helps.

Regards,
Pavlina
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
John Haines
Top achievements
Rank 1
answered on 08 Sep 2009, 02:20 PM
Thank you for your response.

How do I code the  NeedDataSource event for the popup? On the popup I need to populate 3 listboxes with data from the database.

I am using a server side class library to supply the data source for these list boxes.

Thanks again fro your help.

- John
0
Pavlina
Telerik team
answered on 08 Sep 2009, 04:07 PM
Hello John,

To achieve the desired functionality, please refer to the following online demo:
User Control Edit Form

Let me know if you need further assistance.

Greetings,
Pavlina
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
John Haines
Top achievements
Rank 1
answered on 08 Sep 2009, 05:30 PM
I have followed that example. The wrinkle that I have is that I want to use server side generated validator messages for fields on the popup form.

The only way I can figure out how to get these messages to show up on the popup form is to override the default onclick events for the inset and update buttons in the example. This way I can set the error messages based on the information coming back from my data class library. (for example duplicate part number for an insert error or part number in use for a delete error). 

Basically I want the user to be able to see the errors for entries in the popup before they leave the popup form.

Thoughts?
0
Pavlina
Telerik team
answered on 11 Sep 2009, 02:49 PM
Hello John,

In order to progress in the resolution of this matter you can open a formal support ticket, and supply a small project, demonstrating your logic, for additional review.

Greetings,
Pavlina
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.
Tags
Grid
Asked by
John Haines
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
John Haines
Top achievements
Rank 1
Share this question
or