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:
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...
Here is the full code-behind for the popup control
Can you help me figure out how to get these two items to behave the way I want to to?
thanks,
- John
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> |
| |
| <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> |
| |
| <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