Daniel Brady
Top achievements
Rank 1
Daniel Brady
asked on 26 Mar 2009, 12:04 PM
I have a RadGrid popping up a UserControl for Edit and Insert which works great, except that I need to do some server side validation on the UserControl prior to having it disappear and the InsertCommand event firing.
Problem is, when I implement code in the btnOk_Click event it seems to screw things up.
Can anyone point me in the right direction on this one?
Thanks.
Problem is, when I implement code in the btnOk_Click event it seems to screw things up.
Can anyone point me in the right direction on this one?
Thanks.
7 Answers, 1 is accepted
0
Hello Daniel,
One possible option in this case would be to handle the validation on the server, in whatever event will be used to perform the inser/update operation for the grid. If validation fails, you can cancel out the event, as shown in the following code snippet:
.cs
I hope this gets you started properly.
All the best,
Yavor
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
One possible option in this case would be to handle the validation on the server, in whatever event will be used to perform the inser/update operation for the grid. If validation fails, you can cancel out the event, as shown in the following code snippet:
.cs
| protected void RadGrid1_UpdateCommand(object source, |
| Telerik.Web.UI.GridCommandEventArgs e) |
| { |
| GridEditableItem eeditedItem = e.Item as GridEditableItem; |
| DataTable ordersTable = this.GridSource; |
| //Locate the changed row in the DataSource |
| DataRow[] changedRows = ordersTable.Select("OrderID = " + |
| editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["OrderID"].ToString()); |
| if (changedRows.Length != 1) |
| { |
| this.Label1.Text += "Unable to locate the Order for updating."; |
| e.Canceled = true; |
| return; |
| } |
| //Update new values |
| Hashtable newnewValues = new Hashtable(); |
| e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem); |
| changedRows[0].BeginEdit(); |
| try |
| { |
| foreach (DictionaryEntry entry in newValues) |
| { |
| changedRows[0][(string)entry.Key] = entry.Value; |
| } |
| changedRows[0].EndEdit(); |
| } |
| catch (Exception ex) |
| { |
| changedRows[0].CancelEdit(); |
| Label1.Text += "Unable to update Orders. Reason: " + ex.Message; |
| e.Canceled = true; |
| } |
| } |
I hope this gets you started properly.
All the best,
Yavor
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Daniel Brady
Top achievements
Rank 1
answered on 01 May 2009, 04:58 AM
Hi Yavor,
Thanks for this, it seems to do the validation, however all of the data the user has entered into the user control is then cleared.
Is there anyway to either stop the information from being cleared or alternatively re-populate the items in the user control after setting e.Canceled = true?
Thanks.
Thanks for this, it seems to do the validation, however all of the data the user has entered into the user control is then cleared.
Is there anyway to either stop the information from being cleared or alternatively re-populate the items in the user control after setting e.Canceled = true?
Thanks.
0
Daniel Brady
Top achievements
Rank 1
answered on 04 May 2009, 02:15 AM
Further to this issue, I have embedded everything in my UserControl in a panel, which I have added to an Ajax Proxy in the UserControl itself. This seems to fix the problem to a degree in that the UserControl now performs server side validation correctly and also maintains the contents of text boxes etc however two instances of the panel contents are displayed on the UserControl even though only one is defined.
Is there something I am missing in the AjaxProxy configuration?
Is there something I am missing in the AjaxProxy configuration?
0
Hi Daniel,
Based on this information, it is hard to determine what is causing this unwanted behavior.
If the issue persists, you can open a formal support ticket, and send us the problematic implementation. We will review it locally, and advise you further.
Greetings,
Yavor
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Based on this information, it is hard to determine what is causing this unwanted behavior.
If the issue persists, you can open a formal support ticket, and send us the problematic implementation. We will review it locally, and advise you further.
Greetings,
Yavor
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Randy
Top achievements
Rank 1
answered on 06 Oct 2009, 02:54 PM
I am searching around the forums to a solution to my problem. My problem sounds a bit similar with the validation and clicking the add button. Basically what occurs in my scenario is that I have a drop down and 3 textboxes that are required. I have RequiredValidators for the textboxes and a CustomValidator for the drop down list. When I click to add a new record, my Insert Form displays, I don't enter in any information so my validation will fail (which is what I want), then I click on the Add button, I get my validation messages, I then click on the Add button again, and here is where the problem is....my Insert Form collapses or dissapears? Why? The only thing I can think of is the e.Canceled = true; in the RGRDUserList_ItemCommand event. Here is my code:
ASPX Page:
Code Behind:
So basically in the end I am left with a page that if I click the Add button twice in a row, the insert form dissapears...I am confused. Shouldn't the Insert form stay open and continue to display the validation messages? BTW the Telerik Forum Support team is awesome!
Thanks,
- Randy
ASPX Page:
| <%@ Page Title="" Language="C#" ValidateRequest="false" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeBehind="UserList.aspx.cs" Inherits="Page_UserList" %> |
| <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
| <asp:Content ID="Content1" ContentPlaceHolderID="cphHead" runat="server"> |
| <style type="text/css"> |
| .GridEditImageButton |
| { |
| cursor: hand; |
| } |
| .GridDeleteImageButton |
| { |
| cursor: hand; |
| } |
| .EditFormHeader td |
| { |
| font-size: 14px; |
| padding: 4px !important; |
| color: #808080; |
| font-weight: bolder; |
| } |
| </style> |
| </asp:Content> |
| <asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="server"> |
| <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> |
| <script type="text/javascript"> |
| function rgrdUserList_RowDblClick(sender, eventArgs) |
| { |
| sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical()); |
| } |
| </script> |
| </telerik:RadCodeBlock> |
| <telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server"> |
| <AjaxSettings> |
| <telerik:AjaxSetting AjaxControlID="ddlUserEditMode"> |
| <UpdatedControls> |
| <telerik:AjaxUpdatedControl ControlID="rgrdUserList" /> |
| </UpdatedControls> |
| </telerik:AjaxSetting> |
| <telerik:AjaxSetting AjaxControlID="rgrdUserList"> |
| <UpdatedControls> |
| <telerik:AjaxUpdatedControl ControlID="messageGeneral" /> |
| <telerik:AjaxUpdatedControl ControlID="valsumUserList" /> |
| <telerik:AjaxUpdatedControl ControlID="rgrdUserList" |
| LoadingPanelID="rajaxUserListLoadingPanel" /> |
| </UpdatedControls> |
| </telerik:AjaxSetting> |
| </AjaxSettings> |
| </telerik:RadAjaxManagerProxy> |
| <telerik:RadAjaxLoadingPanel ID="rajaxUserListLoadingPanel" runat="server" |
| Transparency="50" |
| MinDisplayTime="150" |
| BackColor="#E1DED1" |
| EnableEmbeddedSkins="false"> |
| <table style="height: 100%; width: 100%" border="0"> |
| <tr> |
| <td width="100%" align="center" valign="middle"> |
| <img id="rajaxUserListLoadingPanelLogo" runat="server" src="~/App_Themes/Default/Images/Logo.gif" alt="TriCore Reference Laboratories" style="border: 0; margin-top: 64px" /><br /> |
| <img id="rajaxUserListLoadingPanelImage" runat="server" src="~/App_Themes/Default/Images/LoadingLargeDrip.gif" alt="Loading..." style="border: 0; margin-top: 8px" /> |
| </td> |
| </tr> |
| </table> |
| </telerik:RadAjaxLoadingPanel> |
| <h1>User List</h1> |
| <div id="messageGeneral" runat="server" class="messageBox"></div> |
| <asp:ValidationSummary ID="valsumUserList" runat="server" |
| DisplayMode="BulletList" |
| HeaderText="Please review the following:" |
| ShowMessageBox="false" |
| ShowSummary="true" /> |
| <asp:DropDownList ID="ddlUserEditMode" runat="server" |
| AutoPostBack="true" |
| Visible="false" |
| onselectedindexchanged="DDLUserEditMode_SelectedIndexChanged"> |
| <asp:ListItem Value="EditForms" Text="Form" Selected="True" /> |
| <asp:ListItem Value="InPlace" Text="Inline" /> |
| </asp:DropDownList> |
| <telerik:RadGrid ID="rgrdUserList" runat="server" |
| AutoGenerateColumns="False" |
| GridLines="None" |
| AllowPaging="True" |
| PageSize="15" |
| AllowAutomaticInserts="True" |
| AllowAutomaticUpdates="True" |
| AllowAutomaticDeletes="True" |
| AllowSorting="true" |
| DataSourceID="dssqlUserList" |
| OnItemInserted="RGRDUserList_ItemInserted" |
| OnItemUpdated="RGRDUserList_ItemUpdated" |
| OnItemDeleted="RGRDUserList_ItemDeleted" |
| OnItemDataBound="RGRDUserList_ItemDataBound" |
| OnItemCreated="RGRDUserList_ItemCreated" |
| OnItemCommand="RGRDUserList_ItemCommand" |
| OnInsertCommand="RGRDUserList_InsertCommand" |
| OnUpdateCommand="RGRDUserList_UpdateCommand" |
| ValidationSettings-EnableValidation="true"> |
| <PagerStyle Mode="NextPrevAndNumeric" /> |
| <MasterTableView |
| Width="100%" |
| CommandItemDisplay="TopAndBottom" |
| DataKeyNames="UserID" |
| DataSourceID="dssqlUserList" |
| HorizontalAlign="NotSet" |
| AutoGenerateColumns="False"> |
| <Columns> |
| <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" |
| ButtonType="ImageButton" |
| EditFormColumnIndex="0"> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| CssClass="GridEditImageButton" |
| HorizontalAlign="Left" |
| VerticalAlign="Top" /> |
| </telerik:GridEditCommandColumn> |
| <telerik:GridButtonColumn UniqueName="DeleteColumn" |
| ConfirmText="Delete this item?" |
| ConfirmDialogType="RadWindow" |
| ConfirmTitle="Delete Item" |
| ButtonType="ImageButton" |
| CommandName="Delete" |
| Text="Delete"> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| HorizontalAlign="Left" |
| VerticalAlign="Top" |
| CssClass="GridDeleteImageButton" /> |
| </telerik:GridButtonColumn> |
| <telerik:GridTemplateColumn UniqueName="UserName" |
| HeaderText="User Name" |
| SortExpression="samAccountName"> |
| <ItemTemplate> |
| <asp:Label ID="lblUserName" runat="server" |
| Text='<%# Eval("UserName") %>' /> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| VerticalAlign="Top" |
| HorizontalAlign="Left" |
| Wrap="false" /> |
| <EditItemTemplate> |
| <telerik:RadComboBox ID="ddlUserName" runat="server" |
| AppendDataBoundItems="true" |
| AutoPostBack="true" |
| AllowCustomText="false" |
| CausesValidation="false" |
| DataTextField="samAccountName" |
| DataValueField="samAccountName" |
| DataSourceID="dsobjUserNameLookup" |
| OnSelectedIndexChanged="DDLUserName_SelectedIndexChanged"> |
| <Items> |
| <telerik:RadComboBoxItem Text="- Select One -" Value="" /> |
| </Items> |
| </telerik:RadComboBox> |
| <asp:CustomValidator ID="valcustUserName" runat="server" |
| ControlToValidate="ddlUserName" |
| Display="Dynamic" |
| EnableClientScript="false" |
| ValidateEmptyText="true" |
| ErrorMessage="Custom Validation Message Not Specified." |
| SetFocusOnError="true" |
| OnServerValidate="VALCUSTUserName_ServerValidate"><img src="../Images/Indicators/ValidationException.gif" class="validationException" alt="" /></asp:CustomValidator> |
| </EditItemTemplate> |
| </telerik:GridTemplateColumn> |
| <telerik:GridTemplateColumn UniqueName="FirstName" |
| HeaderText="First Name" |
| SortExpression="FirstName"> |
| <ItemTemplate> |
| <asp:Label ID="lblFirstName" runat="server" |
| Text='<%# Eval("FirstName") %>' /> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| VerticalAlign="Top" |
| HorizontalAlign="Left" |
| Wrap="false" /> |
| <EditItemTemplate> |
| <telerik:RadTextBox ID="txbFirstName" runat="server" |
| Width="200px" |
| Text='<%# Bind("FirstName") %>' /> |
| <asp:RequiredFieldValidator ID="valreqFirstName" runat="server" |
| ControlToValidate="txbFirstName" |
| EnableClientScript="false" |
| Display="Dynamic" |
| ErrorMessage="First Name is required." |
| SetFocusOnError="true"><img src="../Images/Indicators/ValidationException.gif" class="validationException" alt="" /></asp:RequiredFieldValidator> |
| </EditItemTemplate> |
| </telerik:GridTemplateColumn> |
| <telerik:GridTemplateColumn UniqueName="LastName" |
| HeaderText="Last Name" |
| SortExpression="LastName"> |
| <ItemTemplate> |
| <asp:Label ID="lblLastName" runat="server" |
| Text='<%# Eval("LastName") %>' /> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| VerticalAlign="Top" |
| HorizontalAlign="Left" |
| Wrap="false" /> |
| <EditItemTemplate> |
| <telerik:RadTextBox ID="txbLastName" runat="server" |
| Width="200px" |
| Text='<%# Bind("LastName") %>' /> |
| <asp:RequiredFieldValidator ID="valreqLastName" runat="server" |
| ControlToValidate="txbLastName" |
| EnableClientScript="false" |
| Display="Dynamic" |
| ErrorMessage="Last Name is required." |
| SetFocusOnError="true"><img src="../Images/Indicators/ValidationException.gif" class="validationException" alt="" /></asp:RequiredFieldValidator> |
| </EditItemTemplate> |
| </telerik:GridTemplateColumn> |
| <telerik:GridTemplateColumn UniqueName="EmailAddress" |
| HeaderText="Email Address" |
| SortExpression="EmailAddress"> |
| <ItemTemplate> |
| <asp:HyperLink ID="hlnkEmailAddress" runat="server" |
| Text='<%# Eval("EmailAddress") %>' |
| NavigateUrl='<%# DataBinder.Eval(Container, "DataItem.EmailAddress","MAILTO:{0}") %>' /> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| VerticalAlign="Top" |
| HorizontalAlign="Left" |
| Wrap="false" /> |
| <EditItemTemplate> |
| <telerik:RadTextBox ID="txbEmailAddress" runat="server" |
| Width="200px" |
| Text='<%# Bind("EmailAddress") %>' /> |
| <asp:RequiredFieldValidator ID="valreqEmailAddress" runat="server" |
| ControlToValidate="txbEmailAddress" |
| EnableClientScript="false" |
| Display="Dynamic" |
| ErrorMessage="Email Address is required." |
| SetFocusOnError="true"><img src="../Images/Indicators/ValidationException.gif" class="validationException" alt="" /></asp:RequiredFieldValidator> |
| </EditItemTemplate> |
| </telerik:GridTemplateColumn> |
| <telerik:GridTemplateColumn UniqueName="LastLoginDate" |
| HeaderText="Last Login Date" |
| SortExpression="LastLoginDate" Visible="false"> |
| <ItemTemplate> |
| <asp:Label ID="lblLastLoginDate" runat="server" |
| Text='<%# Eval("LastLoginDate") %>' /> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="false" /> |
| <ItemStyle |
| VerticalAlign="Top" |
| HorizontalAlign="Left" |
| Wrap="false" /> |
| <EditItemTemplate> |
| <asp:Label ID="lblLastLoginDate" runat="server" |
| Text='<%# Eval("LastLoginDate") %>' /> |
| </EditItemTemplate> |
| </telerik:GridTemplateColumn> |
| <telerik:GridCheckBoxColumn UniqueName="IsActive" |
| DataField="IsActive" |
| HeaderText="Active" |
| EditFormColumnIndex="0" |
| Visible="false" |
| SortExpression="IsActive"> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="true" /> |
| <ItemStyle |
| HorizontalAlign="Center" |
| VerticalAlign="Top" /> |
| </telerik:GridCheckBoxColumn> |
| <telerik:GridTemplateColumn UniqueName="IsActiveIndicator" |
| HeaderText="Active" |
| SortExpression="IsActive"> |
| <ItemTemplate> |
| <asp:Image ID="imgIsActive" runat="server" |
| BorderWidth="0px" |
| Height="16px" |
| Width="16px" |
| ImageUrl='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsActive"))? "../Images/Indicators/checkmark.png" : "../Images/Indicators/cross.png" %>' |
| AlternateText="Active" |
| Style="float: right; cursor: pointer;"> |
| </asp:Image> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="true" /> |
| <ItemStyle |
| HorizontalAlign="Center" |
| VerticalAlign="Top" /> |
| </telerik:GridTemplateColumn> |
| <telerik:GridCheckBoxColumn UniqueName="IsApplicationMasterAdmin" |
| DataField="IsApplicationMasterAdmin" |
| HeaderText="Master Admin" |
| EditFormColumnIndex="0" |
| Visible="false" |
| SortExpression="IsApplicationMasterAdmin"> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="true" /> |
| <ItemStyle |
| HorizontalAlign="Center" |
| VerticalAlign="Top" /> |
| </telerik:GridCheckBoxColumn> |
| <telerik:GridTemplateColumn UniqueName="IsApplicationMasterAdminIndicator" |
| HeaderText="Master Admin" |
| SortExpression="IsApplicationMasterAdmin"> |
| <ItemTemplate> |
| <asp:Image ID="imgIsApplicationMasterAdmin" runat="server" |
| BorderWidth="0px" |
| Height="16px" |
| Width="16px" |
| ImageUrl='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsApplicationMasterAdmin"))? "../Images/Indicators/checkmark.png" : "../Images/Indicators/cross.png" %>' |
| AlternateText="Master Admin" |
| Style="float: right; cursor: pointer;"> |
| </asp:Image> |
| </ItemTemplate> |
| <HeaderStyle |
| VerticalAlign="Bottom" |
| Wrap="true" /> |
| <ItemStyle |
| HorizontalAlign="Center" |
| VerticalAlign="Top" /> |
| </telerik:GridTemplateColumn> |
| </Columns> |
| <EditFormSettings |
| ColumnNumber="1" |
| CaptionDataField="UserName" |
| CaptionFormatString="Edit Properties of User {0}"> |
| <FormTableItemStyle Wrap="False" /> |
| <FormCaptionStyle CssClass="EditFormHeader" /> |
| <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="16" BackColor="White" Width="100%" /> |
| <FormTableStyle CellSpacing="0" CellPadding="2" Height="110px" BackColor="White" /> |
| <FormTableAlternatingItemStyle Wrap="False" /> |
| <EditColumn |
| ButtonType="PushButton" |
| CancelText="Cancel" |
| InsertText="Add" |
| UpdateText="Save" |
| UniqueName="EditCommandColumn1" /> |
| <FormTableButtonRowStyle HorizontalAlign="Right" /> |
| </EditFormSettings> |
| <NoRecordsTemplate> |
| <div> |
| No data was returned. |
| </div> |
| </NoRecordsTemplate> |
| </MasterTableView> |
| <ClientSettings AllowRowsDragDrop="true"> |
| <ClientEvents OnRowDblClick="rgrdUserList_RowDblClick" /> |
| <Selecting AllowRowSelect="True" EnableDragToSelectRows="false" /> |
| </ClientSettings> |
| </telerik:RadGrid> |
| <telerik:RadWindowManager ID="rwmgrUserList" runat="server" /> |
| <asp:SqlDataSource ID="dssqlUserList" runat="server" |
| ConnectionString="<%$ ConnectionStrings:DoSConnectionString %>" |
| ProviderName="System.Data.SqlClient" |
| InsertCommand="INSERT INTO [User]([UserName], [FirstName], [LastName], [EmailAddress], [LastLoginDate], [IsActive], [IsApplicationMasterAdmin]) VALUES (@UserName, @FirstName, @LastName, @EmailAddress, @LastLoginDate, @IsActive, @IsApplicationMasterAdmin)" |
| InsertCommandType="Text" |
| SelectCommand=" |
| SELECT |
| [U].[UserID], |
| [U].[UserName], |
| [U].[FirstName], |
| [U].[LastName], |
| [U].[EmailAddress], |
| [U].[LastLoginDate], |
| [U].[IsActive], |
| [U].[IsApplicationMasterAdmin] |
| FROM |
| [User] AS [U] |
| ORDER BY |
| [U].[FirstName] ASC, |
| [U].[Lastname] ASC" |
| SelectCommandType="Text" |
| UpdateCommand="UPDATE [User] SET [UserName] = @UserName, [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress, [LastLoginDate] = @LastLogiNDate, [IsActive] = @IsActive, [IsApplicationMasterAdmin] = @IsApplicationMasterAdmin WHERE [UserID] = @original_UserID" |
| UpdateCommandType="Text" |
| DeleteCommand="DELETE FROM [User] WHERE [UserID] = @original_UserID" |
| DeleteCommandType="Text" |
| OldValuesParameterFormatString="original_{0}" |
| ConflictDetection="OverwriteChanges"> |
| <InsertParameters> |
| <asp:Parameter Name="UserName" Type="String" /> |
| <asp:Parameter Name="FirstName" Type="String" /> |
| <asp:Parameter Name="LastName" Type="String" /> |
| <asp:Parameter Name="EmailAddress" Type="String" /> |
| <asp:Parameter Name="LastLoginDate" Type="DateTime" /> |
| <asp:Parameter Name="IsActive" Type="Boolean" /> |
| <asp:Parameter Name="IsApplicationMasterAdmin" Type="Boolean" /> |
| </InsertParameters> |
| <UpdateParameters> |
| <asp:Parameter Name="UserName" Type="String" /> |
| <asp:Parameter Name="FirstName" Type="String" /> |
| <asp:Parameter Name="LastName" Type="String" /> |
| <asp:Parameter Name="EmailAddress" Type="String" /> |
| <asp:Parameter Name="LastLoginDate" Type="DateTime" /> |
| <asp:Parameter Name="IsActive" Type="Boolean" /> |
| <asp:Parameter Name="IsApplicationMasterAdmin" Type="Boolean" /> |
| <asp:Parameter Name="original_UserID" Type="Int32" /> |
| </UpdateParameters> |
| <DeleteParameters> |
| <asp:Parameter Name="original_UserID" Type="Int32" /> |
| </DeleteParameters> |
| </asp:SqlDataSource> |
| <asp:ObjectDataSource ID="dsobjUserNameLookup" runat="server" |
| OldValuesParameterFormatString="original_{0}" |
| SelectMethod="GetListItemCollectionByGroup" |
| TypeName="ADUserManager"> |
| </asp:ObjectDataSource> |
| </asp:Content> |
Code Behind:
| //----------------------------------------------------------------------- |
| // <copyright file="UserList.aspx.cs" company="TriCore Reference Laboratories"> |
| // Copyright (c) TriCore Reference Laboratories. All rights reserved. |
| // </copyright> |
| //----------------------------------------------------------------------- |
| using System; |
| using System.Collections.Generic; |
| using System.Configuration; |
| using System.Linq; |
| using System.Web; |
| using System.Web.UI; |
| using System.Web.UI.WebControls; |
| using Telerik.Web.UI; |
| /// <summary> |
| /// Represents a Page_UserList. |
| /// </summary> |
| public partial class Page_UserList : PageBase |
| { |
| /// <summary> |
| /// The Page_Load event. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| // Clear the message for the first time. |
| DoSWebApp.Non_App_Code.MessageBox.ClearMsg(this.messageGeneral); |
| // Set the parameters for the objectdatasource |
| dsobjUserNameLookup.SelectParameters.Clear(); |
| dsobjUserNameLookup.SelectParameters.Add("domain", TypeCode.String, Convert.ToString(ConfigurationSettings.AppSettings["ActiveDorectory"])); |
| dsobjUserNameLookup.SelectParameters.Add("group", TypeCode.String, Convert.ToString(ConfigurationSettings.AppSettings["ActiveDirectorySecurityGroupDN"])); |
| dsobjUserNameLookup.SelectParameters.Add("sortExpression", TypeCode.String, "name"); |
| dsobjUserNameLookup.SelectParameters.Add("sortDirection", TypeCode.String, "ASC"); // or DESC |
| if (!this.IsPostBack) |
| { |
| // Set the grid edit mode according to the selection |
| switch (this.ddlUserEditMode.SelectedValue) |
| { |
| case "EditForms": |
| { |
| this.rgrdUserList.MasterTableView.EditMode = GridEditMode.EditForms; |
| break; |
| } |
| case "InPlace": |
| { |
| this.rgrdUserList.MasterTableView.EditMode = GridEditMode.InPlace; |
| break; |
| } |
| } |
| } |
| } |
| /// <summary> |
| /// This event fires when the grid has inserted an item. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_ItemInserted(object sender, GridInsertedEventArgs e) |
| { |
| if (e.Exception != null) |
| { |
| e.ExceptionHandled = true; |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User cannot be added. Reason: " + e.Exception.Message, |
| MessageBoxTypeEnum.Failure); |
| } |
| else |
| { |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User added!", |
| MessageBoxTypeEnum.Success); |
| } |
| } |
| /// <summary> |
| /// This event fires when the grid has updated an item. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_ItemUpdated(object sender, Telerik.Web.UI.GridUpdatedEventArgs e) |
| { |
| GridEditableItem item = (GridEditableItem)e.Item; |
| string id = Convert.ToString(item.GetDataKeyValue("UserID")); |
| if (e.Exception != null) |
| { |
| e.KeepInEditMode = true; |
| e.ExceptionHandled = true; |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User cannot be saved. Reason: " + e.Exception.Message, |
| MessageBoxTypeEnum.Failure); |
| } |
| else |
| { |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User saved!", |
| MessageBoxTypeEnum.Success); |
| } |
| } |
| /// <summary> |
| /// This event fires when the grid has deleted an item. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_ItemDeleted(object sender, GridDeletedEventArgs e) |
| { |
| GridDataItem dataItem = (GridDataItem)e.Item; |
| string id = Convert.ToString(dataItem.GetDataKeyValue("UserID")); |
| if (e.Exception != null) |
| { |
| e.ExceptionHandled = true; |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User cannot be removed. Reason: " + e.Exception.Message, |
| MessageBoxTypeEnum.Failure); |
| } |
| else |
| { |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User removed!", |
| MessageBoxTypeEnum.Success); |
| } |
| } |
| /// <summary> |
| /// This event fires when the dropdownlist index has changed. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void DDLUserEditMode_SelectedIndexChanged(object sender, EventArgs e) |
| { |
| // Determin what was selected and change the grid properties based on the selection |
| switch ((sender as DropDownList).SelectedValue) |
| { |
| case "EditForms": |
| { |
| rgrdUserList.MasterTableView.EditMode = GridEditMode.EditForms; |
| break; |
| } |
| case "InPlace": |
| { |
| rgrdUserList.MasterTableView.EditMode = GridEditMode.InPlace; |
| break; |
| } |
| } |
| // Rebind so that the grid refreshes |
| rgrdUserList.Rebind(); |
| } |
| /// <summary> |
| /// This event fires when an item is being databound. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridEditableItem && e.Item.IsInEditMode) |
| { |
| if (e.Item.OwnerTableView.IsItemInserted) |
| { |
| // item is about to be inserted |
| } |
| else |
| { |
| // item is about to be edited |
| } |
| } |
| if (e.Item is GridDataItem) |
| { |
| GridDataItem dataItem = (GridDataItem)e.Item; |
| ImageButton deleteButton = (ImageButton)dataItem["DeleteColumn"].Controls[0]; |
| CheckBox isApplicationMasterAdminCheckbox = (CheckBox)dataItem["IsApplicationMasterAdmin"].Controls[0]; |
| if (isApplicationMasterAdminCheckbox.Checked) |
| { |
| deleteButton.Visible = false; |
| } |
| } |
| } |
| /// <summary> |
| /// This event fires when an item is being created. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_ItemCreated(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridEditableItem && e.Item.IsInEditMode) |
| { |
| if (e.Item.OwnerTableView.IsItemInserted) |
| { |
| // item is about to be inserted |
| //the checkbox list will be the first control in the Controls collection of the corresponding cell |
| CheckBox isActiveCheckBox = (e.Item as GridEditableItem)["IsActive"].Controls[0] as CheckBox; |
| isActiveCheckBox.Checked = true; |
| } |
| else |
| { |
| // item is about to be edited |
| } |
| } |
| } |
| /// <summary> |
| /// This event fires when an command is being executed. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e) |
| { |
| RadGrid grid = sender as RadGrid; |
| // This is when Add New Item is clicked. |
| if (e.CommandName == RadGrid.InitInsertCommandName) |
| { |
| if (grid.EditItems.Count > 0) |
| { |
| grid.MasterTableView.ClearEditItems(); |
| } |
| } |
| // This is when Add is executed. |
| if (e.CommandName == RadGrid.PerformInsertCommandName) |
| { |
| // Page.Validate(); |
| if (this.IsValid) |
| { |
| // grid.MasterTableView.ClearEditItems(); |
| } |
| else |
| { |
| // To display the confirmation messages |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| this.valsumUserList, |
| MessageBoxTypeEnum.Alert); |
| e.Canceled = true; |
| } |
| } |
| // This is when Edit is clicked. |
| if (e.CommandName == RadGrid.EditCommandName) |
| { |
| e.Item.OwnerTableView.IsItemInserted = false; |
| } |
| // This is when the Edit is executed |
| if (e.CommandName == RadGrid.UpdateCommandName) |
| { |
| // Page.Validate(); |
| if (this.IsValid) |
| { |
| e.Item.OwnerTableView.IsItemInserted = false; |
| } |
| else |
| { |
| // To display the confirmation messages |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| this.valsumUserList, |
| MessageBoxTypeEnum.Alert); |
| e.Canceled = true; |
| } |
| } |
| } |
| /// <summary> |
| /// This event fires when the insert command is being executed. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_InsertCommand(object sender, GridCommandEventArgs e) |
| { |
| } |
| /// <summary> |
| /// This event fires when the update command is being executed. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void RGRDUserList_UpdateCommand(object sender, GridCommandEventArgs e) |
| { |
| } |
| /// <summary> |
| /// This event fires when the index changes on the drop down list. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void DDLUserName_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e) |
| { |
| //first reference the edited grid item through the NamingContainer attribute |
| GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem; |
| ADUserManager userManagerAD = new ADUserManager(); |
| ADUserListItem userListItemAD = new ADUserListItem(); |
| userListItemAD = userManagerAD.GetListItemBySAMAccountName(Convert.ToString(ConfigurationSettings.AppSettings["ActiveDirectory"]), (sender as RadComboBox).SelectedValue); |
| //the dropdown list will be the first control in the Controls collection of the corresponding cell |
| //for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) method |
| RadTextBox firstNameTextBox = (RadTextBox)editedItem["FirstName"].FindControl("txbFirstName"); |
| RadTextBox lastNameTextBox = (RadTextBox)editedItem["LastName"].FindControl("txbLastName"); |
| RadTextBox emailAddressTextBox = (RadTextBox)editedItem["EmailAddress"].FindControl("txbEmailAddress"); |
|
// change the data of the controls based on what came back
|
| firstNameTextBox.Text = userListItemAD.FirstName != null ? userListItemAD.FirstName : string.Empty; |
| lastNameTextBox.Text = userListItemAD.LastName != null ? userListItemAD.LastName : string.Empty; |
| emailAddressTextBox.Text = userListItemAD.EmailAddress != null ? userListItemAD.EmailAddress : string.Empty; |
| DoSWebApp.Non_App_Code.MessageBox.DisplayMsg( |
| this.messageGeneral, |
| "User information retrieved!", |
| MessageBoxTypeEnum.Info); |
| } |
| /// <summary> |
| /// This event fires when the server validate executes. |
| /// </summary> |
| /// <param name="sender">The object raising the event.</param> |
| /// <param name="e">The event arguments.</param> |
| protected void VALCUSTUserName_ServerValidate(object sender, ServerValidateEventArgs e) |
| { |
| CustomValidator userNameCustomValidator = (CustomValidator)sender; // have to use a custom validator to check required because the
|
| if (!(e.Value.Length > 0) || (e.Value == "- Select One -")) |
| { |
| e.IsValid = false; |
| userNameCustomValidator.ErrorMessage = "User Name is required."; |
| } |
| } |
| /// <summary> |
| /// This trims text and places a ... at the end of the text. |
| /// </summary> |
| /// <param name="content">The content being trimmed.</param> |
| /// <param name="concatEndpoint">The endpoint where the ... needs to be added.</param> |
| /// <param name="endStringInidcator">The ... or something else.</param> |
| /// <returns>The trimed value.</returns> |
| protected string TrimContent(string content, int concatEndpoint, string endStringInidcator) |
| { |
| if (!string.IsNullOrEmpty(content) && content.Length > concatEndpoint) |
| { |
| return string.Concat(content.Substring(0, concatEndpoint), endStringInidcator); |
| } |
| return content; |
| } |
| } |
So basically in the end I am left with a page that if I click the Add button twice in a row, the insert form dissapears...I am confused. Shouldn't the Insert form stay open and continue to display the validation messages? BTW the Telerik Forum Support team is awesome!
Thanks,
- Randy
0
Hello Randy,
I reviewed the code, and it looks correct. By just reviewing the code, however, it is not possible to pinpoint the cause of the problem. To do so, you can send us a small working project, demonstrating your setup, in a formal support ticket. Upon reviewing it, we will advise you further.
Kind regards,
Yavor
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.
I reviewed the code, and it looks correct. By just reviewing the code, however, it is not possible to pinpoint the cause of the problem. To do so, you can send us a small working project, demonstrating your setup, in a formal support ticket. Upon reviewing it, we will advise you further.
Kind regards,
Yavor
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
mathieu cupryk
Top achievements
Rank 1
answered on 25 Feb 2013, 03:51 PM
dsobjUserNameLookup.SelectParameters.Add("domain", TypeCode.String, Convert.ToString(ConfigurationManager.AppSettings["ActiveDorectory"])); Is this a mistake?