Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Grid > Requiredfieldvalidator not fired before the ItemCommand event

Not answered Requiredfieldvalidator not fired before the ItemCommand event

Feed from this thread
  • Gerardo Perez avatar

    Posted on Feb 18, 2010 (permalink)

    Hi, I have the following radgrid:

     <telerik:RadAjaxPanel ID="MyPanel" runat="server">
            <telerik:RadInputManager ID="MyRadManager" runat="server">
                    <telerik:RegExpTextBoxSetting BehaviorID="RegExpBehavior1" InitializeOnClient="false"
                        ErrorMessage="Invalid date format" InvalidCssClass="invalid"
                        ValidationExpression="((0[1-9]|1[012])[- /.](19|20)\d\d)">
                        <Validation IsRequired="true" />
                    </telerik:RegExpTextBoxSetting>
                </telerik:RadInputManager>
                <telerik:RadGrid
                                ID="myGrid"
                                DataSourceID="MyDataSource"
                                runat="server"
                                AutoGenerateColumns="False"
                                GridLines="None"
                                AllowAutomaticDeletes="True"
                                AllowAutomaticInserts="True"
                                Skin="Outlook"
                                OnItemCreated="myGrid_ItemCreated"
                                OnItemCommand="myGrid_ItemCommand">
                        <HeaderContextMenu EnableTheming="True">
                        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
                        </HeaderContextMenu>

                        <MasterTableView CommandItemDisplay="Top" DataKeyNames ="Id" DataSourceID="DegreesDataSource">
                           <CommandItemTemplate>
                                                
                                              <asp:LinkButton ID="LinkButton2" runat="server" CommandName="InitInsert" Visible='<%# !myGrid.MasterTableView.IsItemInserted %>'>
                                                    <%--<img style="border:0px" alt="" src="../../DataEditing/Img/AddRecord.gif" />--%> Add New Record</asp:LinkButton>
                                                <br />
                                            </CommandItemTemplate>
                        <RowIndicatorColumn>
                        <HeaderStyle Width="20px"></HeaderStyle>
                        </RowIndicatorColumn>

                        <ExpandCollapseColumn>
                        <HeaderStyle Width="20px"></HeaderStyle>
                        </ExpandCollapseColumn>
                            <Columns>
                                <telerik:GridBoundColumn DataField="Title" HeaderText="Title"
                                    SortExpression="Title" >
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Area"
                                    HeaderText="Subject Matter or Speciality Field" SortExpression="Area">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Year" HeaderText="Date" SortExpression="Year">
                                </telerik:GridBoundColumn>
                                <telerik:GridButtonColumn UniqueName="Delete" ButtonType="LinkButton" CommandName="Delete" Text="Remove"></telerik:GridButtonColumn>
                            </Columns>
                             <EditFormSettings EditFormType="Template">
                                                <EditColumn UniqueName="EditCommandColumn1">
                                                </EditColumn>
                                                <FormTemplate>
                                                    <table cellspacing="2" cellpadding="1" width="100%" border="0">
                                                        <tr>
                                                        <td>
                                                            Degree:</td>
                                                        <td>
                                                            <asp:TextBox Width="200px" ID="TextBox1" runat="server" Text='<%# Bind("Title") %>' MaxLength="50"></asp:TextBox>
                                                            <asp:requiredfieldvalidator id="ReqTextBox1" runat="server" ErrorMessage="*" EnableClientScript="False"
                                                                                        ControlToValidate="TextBox1" Display="Static" />
                                                        </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Area:
                                                            </td>
                                                            <td>
                                                                <asp:TextBox Width="200px" ID="TextBox2" runat="server" Text='<%#Bind("Area") %>' MaxLength="200"></asp:TextBox>
                                                                <asp:requiredfieldvalidator id="ReqTextBox2" runat="server" ErrorMessage="*" EnableClientScript="False"
                                                                                        ControlToValidate="TextBox2" Display="Static"  />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Institution:
                                                            </td>
                                                            <td>
                                                                <asp:TextBox Width="200px" ID="TextBox5" runat="server" Text='<%#Bind("CompanyName") %>' MaxLength="200"></asp:TextBox>
                                                                <asp:requiredfieldvalidator
                                                                                           id="ReqTextBox5"
                                                                                           runat="server"
                                                                                           ErrorMessage="*"
                                                                                           EnableClientScript="False"
                                                                                           ControlToValidate="TextBox5"
                                                                                           Display="Static"  />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Date:</td>
                                                            <td>
                                                                <asp:TextBox Width="200px" ID="TextBox4" runat="server" Text='<%#Bind("Year") %>'/>                                                         
                                                            </td>
                                                                
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                <asp:Label ID="ErrorMessage" runat="server"  EnableViewState="false" Visible="false" Text="Entry already exists." ForeColor="Red" />
                                                                <br></br>
                                                                <asp:LinkButton
                                                                                ID="Button1"
                                                                                runat="server"
                                                                                Text='<%# (Container is GridEditFormInsertItem) ? "Save" : "Update" %>'
                                                                                CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />
                                                                <asp:LinkButton
                                                                               ID="Button2"
                                                                               runat="server"
                                                                               Text="Cancel"
                                                                               CausesValidation="false"
                                                                               CommandName="Cancel" />
                            
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </FormTemplate>
                                            </EditFormSettings>
                        </MasterTableView>                  
                        <FilterMenu EnableTheming="True">
                        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
                        </FilterMenu>
                </telerik:RadGrid>
            </telerik:RadAjaxPanel>


    <pp:ObjectContainerDataSource ID="MyDataSource" runat="server"                          
            DataObjectTypeName = "MVPWithCWABQuickStart.HelloWorld.BusinessEntities.MyEntity"
            oninserted="MyDataSource_Inserted"
            ondeleted="MyDataSource_Deleted" />

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Code-behind

     protected void MyDataSource_Inserted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
            {
             
            }
            protected void MyDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
            {
             
            }


     protected void myGrid_ItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditFormItem editedItem = e.Item as GridEditFormItem;

                    TextBox txt1 = editedItem.FindControl("TextBox4") as TextBox;
                   if (txt1 != null)
                    {
                        RegExpTextBoxSetting regExpSetting = (RegExpTextBoxSetting)RadDegreesManager.GetSettingByBehaviorID("RegExpBehavior1");
                        regExpSetting.TargetControls.Add(new TargetInput(txt1.UniqueID, true));
                    }
                }

                if (e.Item is GridDataItem && !e.Item.IsInEditMode && e.Item.DataItem != null) //Checks that the item is a GridDataItem, is not in Edit mode and is not NULL
                {
                    DateTime datetime;
                    if (DateTime.TryParse(((ProfileDegree)e.Item.DataItem).Year, out datetime))
                    {
                        ((ProfileDegree)e.Item.DataItem).Year = String.Format("{0:MMM/yyyy}", datetime); //Change the format of the date to MMM/yyyy (e.g. Jan/2010)
                    }
                }
            }



     protected void myGrid_ItemCommand(object source, GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
                {
                    e.Canceled = true;
                    var newValues = new System.Collections.Specialized.ListDictionary();
                    newValues["Title"] = string.Empty;
                    newValues["Area"] = string.Empty;
                    newValues["CompanyName"] = string.Empty;
                    newValues["Year"] = string.Empty;
                    //Insert the item and rebind
                    e.Item.OwnerTableView.InsertItem(newValues);
                }
                if (e.Item.ItemType == GridItemType.EditFormItem)
                {
                    if (e.CommandName == "PerformInsert")
                    {
                        TextBox titleTextBox = (TextBox)e.Item.FindControl("TextBox1");
                        TextBox areaTextBox = (TextBox)e.Item.FindControl("TextBox2");
                        TextBox companyTextBox = (TextBox)e.Item.FindControl("TextBox5");
                        TextBox yearTextBox = (TextBox)e.Item.FindControl("TextBox4");
                        Label errorMessage = (Label)e.Item.FindControl("ErrorMessage");

                        if (titleTextBox != null && areaTextBox != null && companyTextBox != null && yearTextBox != null)
                        {
                            //If the addition operation was not triggered from a refresh, do the database insert.
                            e.Canceled = expertiseDateValidationCancel(yearTextBox, degreeErrorMessage);
                            if (!e.Canceled)
                            {
                                MyEntity metaData = new MyEntity();
                                metaData.Degree = titleTextBox.Text;
                                metaData.Area = areaTextBox.Text;
                                metaData.InstitutionName = companyTextBox.Text;
                                metaData.Year = yearTextBox.Text;
                        

                               if(InsertObject(metaData))
                                {
                                    errorMessage.Visible = true;
                                    e.Canceled = true;
                                }
                              
                            }
                        }
                        else
                            errorMessage.Visible = true;
                    }
                }
            }



    My problem is that the required field validators in the edit form of the grid are not being fired or at least the validation do not stop the postback of the command, because I can see that Page.IsValid property is false when debugging myGrid_ItemCommand. Do you have an idea of what could be happening. my understanding is that there should not be a postback when the form is empty. I have tried using validation groups, removing the radPanel and the RadInputManager but still no luck. I know that I could stop the execution of the command using something like this:

    if(!Page.IsValid)
    {
     e.Cencled = true;
    return;
    }

    but I want to avoid the post back.

    I'm using RadControls for ASPNET AJAX Q1 2009 (2009.1.527.35)

    Thank you for your help!!!

    Reply

  • Radoslav Radoslav avatar

    Posted on Feb 23, 2010 (permalink)

    Hi Gerardo,

    Please try setting the RequiredFieldValidator.EnableClientScript property to true.

    You could find more information about the EnableClientScript property on the following link:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.enableclientscript.aspx

    I hope this helps.

    Regards,
    Radoslav
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.
  • Gerardo Perez avatar

    Posted on Feb 23, 2010 (permalink)

    Thanks, that did the job, It is working fine now...

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Grid > Requiredfieldvalidator not fired before the ItemCommand event
Related resources for "Requiredfieldvalidator not fired before the ItemCommand event"

ASP.NET Grid Features  |  Documentation  |  Demos  |  Step-by-step Tutorial  ]