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

Manipulate GridEditFormItem from Customvalidator event

3 Answers 149 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 1
Jack asked on 23 Jan 2009, 03:49 PM
Hi,

I had a problem to get value from editform template from a validator event.

    Public Sub ValidateOccupation(ByVal source As Object, ByVal e As ServerValidateEventArgs) 
        ' verifie s'il est en edition 
        Dim mess As StringString = String.Empty 
        If grdTerritory.EditItems.Count > 0 Then 
            Dim Description As StringString = String.Empty 
            Dim lstOcc As List(Of Occupation) = New List(Of Occupation) 
 
            Dim editedItem As GridItem = grdTerritory.EditItems(0) 
 
            DirectCast(editedItem.FindControl("lblErrorMessage"), Label).Text = mess 
 
            If GetInfoEditableItem(editedItem, Description, ID.ToString, lstOcc) Then 
                Try 
                    mess = OccupationValidator(ID.ToString, lstOcc, TerritoryAction.Update) 
                Catch ex As Exception 
                    Throw 
                End Try 
 
            End If 
 
            If Not String.IsNullOrEmpty(mess) Then 
                e.IsValid = False 
            Else 
                e.IsValid = True 
            End If 
        End If 

This DirectCast(editedItem.FindControl("lblErrorMessage"), Label) crash on NullReferenceException

How can access my label from my validator event into editTemplate on my item in editmode?

ty


3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 26 Jan 2009, 07:43 PM
Hello Jack,

I can suggest you this exemplary approach:

The code below shows model of EditTemplateSettings:

                <EditFormSettings EditFormType="Template"
                    <EditColumn UniqueName="EditCommandColumn1"
                    </EditColumn> 
                    <FormTemplate> 
                        <asp:CheckBox ID="chkVisibility" runat="server" AutoPostBack="true"  
                            Text="Visibility" /> 
                        <br /> 
                        <asp:Panel ID="pnlControls" runat="server" Visible="false"
                            <asp:TextBox ID="TextBox1" runat="server" Text="Type something"></asp:TextBox> 
                             
                            <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" 
                            ErrorMessage="ServeR validation" Display="Dynamic" onservervalidate="CustomValidator1_ServerValidate"
                            </asp:CustomValidator> 
                             
                        </asp:Panel> 
                        <asp:Button ID="btn" runat="server" CausesValidation="true" CommandName="Cancel" Text="Cancel" /> 
                    </FormTemplate> 
                </EditFormSettings> 

Now when you click the button, the post back will initiate a validation on server. In my case CustomValidator1_ServerValidate will handle the validation event. In this method the 'source' variable in this case will be the CustomValidator control which raises the event. The parent of this CustomValidator is the Panel. When you have the panel you can use FindControl method inherited from Control class and to find, for example, the TextBox control.

Well if you want to get whole edit form, you need to get the parent of the Panel, because the Panel is a child for the EditForm.

Below is a code excerpts which describes how to find the textbox in current EditFormSettings:

Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs) 
    Dim txt As TextBox = DirectCast((DirectCast(source, CustomValidator)).Parent.FindControl("TextBox1"), TextBox) 
    txt.Text = "some text here" 
End Sub 

Best wishes,
Georgi Krustev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jack
Top achievements
Rank 1
answered on 26 Jan 2009, 07:55 PM
Hi,

I can't do that because my ControlToValidate is de ASP:CheckBoxList and is a ASP limitation on controlToValidation for this control type.

I can't catch data into a row in editMode outside of gridEvent with a eventArgument with gridItem inside?

Ty Georgi


0
Georgi Krustev
Telerik team
answered on 29 Jan 2009, 03:12 PM
Hi Jack,

I reviewed your requirements and can suggest two options for you.

The first one is to create a custom validator, which can validate CheckBoxList. Here you can find some information on the subject.

The second option is which answers your question. You can get all items which are in edit mode referencing the EditItems collection of the RadGrid.

Here is a code snippet showing how you can get the first item of EditItems collection and find a specific control in it. In my case CheckBoxList with id chkBoxList inside template column:
... 
(((Telerik.Web.UI.GridDataItem)(RadGrid4.EditItems[0]))).EditFormItem.FindControl("chkBoxList"
... 

Kind regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Jack
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Jack
Top achievements
Rank 1
Share this question
or