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

[Solved] Client Side functions (get_isItemInserted)

3 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
FreshOne
Top achievements
Rank 1
FreshOne asked on 03 Feb 2010, 11:03 AM
hi all,

i have a custom validator for a RadUpload control in the grid. i want the validator to validate empty text only on insert not on update, on update it's not required to upload a file but if the user selected one it should get validated.

i have put this code in ItemDataBound
    Private Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then 
            Dim item As GridEditableItem = CType(e.Item, GridEditableItem)  
            Dim imageValidator As CustomValidator = CType(item.FindControl("CustomValidator2"), CustomValidator)  
            If (item.OwnerTableView.IsItemInserted) Then 
                imageValidator.ValidateEmptyText = True 
            Else 
                imageValidator.ValidateEmptyText = False 
            End If 
        End If 
    End Sub 

but it didn't work bcoz the ClientValidationFunction still validating empty text in both cases. So i tried to modify this function to check if the item is in edit or insert mode using get_isItemInserted, but sth is wrong and i coudn't fix it. Here is my code
function validateImageType(source, e)   
{  
    e.IsValid=false;  
 
    var upload=$find(source.parentNode.getElementsByTagName('div')[0].id);            
    var inputs=upload.getFileInputs();  
      
    if (e.get_isItemInserted==true)  
    {  
        for (var i = 0; i < inputs.length; i++)   
        {  
            //check for empty string or invalid extension  
            if (inputs[i].value != "" && upload.isExtensionValid(inputs[i].value))   
            {  
                e.IsValid=true;  
                break;  
            }  
        }              
    }              
    else 
    {  
        if (inputs.length>0)  
        {  
            for (var i = 0; i < inputs.length; i++)   
            {  
                //check for invalid extension only  
                if (upload.isExtensionValid(inputs[i].value))   
                {  
                    e.IsValid=true;  
                    break;  
                }  
            }                    
        }  
        else 
        {  
            e.IsValid=true;  
        }  
    }  
}       

And here is my column if nedded
            <telerik:GridTemplateColumn HeaderText="Image" SortExpression="image" UniqueName="image" AutoPostBackOnFilter="true" DataField="image">  
                <ItemTemplate> 
                    <img src='/Files/PhotoGallery/<%# Eval("image") %>' width="100" height="100" /> 
                </ItemTemplate> 
                <EditItemTemplate> 
                    <telerik:RadUpload ID="RadUpload1" runat="server" AllowedFileExtensions=".jpg,.jpeg" ControlObjectsVisibility="None"></telerik:RadUpload> 
                    <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Allowed file extensions are .jpg, .jpeg" ClientValidationFunction="validateImageType" Display="Dynamic"></asp:CustomValidator> 
                </EditItemTemplate> 
            </telerik:GridTemplateColumn>     

Any ideas plzzz..??

thnx in advance

3 Answers, 1 is accepted

Sort by
0
FreshOne
Top achievements
Rank 1
answered on 08 Feb 2010, 09:17 AM
any hints or leads?
or is their any other approach to do this??
0
Accepted
Radoslav
Telerik team
answered on 08 Feb 2010, 11:55 AM
Hi,


I looked through your code and I found what is caused the problem. The get_isItemInserted() function gets a value indicating if the GridTableView is currently in insert mode. In your code the e is eventArgs and it is not of the GridTableView type.  So you need to modify the validateImageType function like that :

function validateImageType(source, e) {
 e.IsValid = false;
 var upload = $find(source.parentNode.getElementsByTagName('div')[0].id);
 var inputs = upload.getFileInputs();
 var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
 if (masterTable.get_isItemInserted() == true) {
    for (var i = 0; i < inputs.length; i++) {
       //check for empty string or invalid extension 
      if (inputs[i].value != "" && upload.isExtensionValid(inputs[i].value)) {
         e.IsValid = true;
         break;
      }
    }
 }
 else {
   if (inputs.length > 0) {
     for (var i = 0; i < inputs.length; i++) {
       //check for invalid extension only 
        if (upload.isExtensionValid(inputs[i].value)) {
          e.IsValid = true;
          break;
        }
      }
   }
   else {
      e.IsValid = true;
   }
 }
}      

I hope this helps.

Kind regards,
Radoslav
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
FreshOne
Top achievements
Rank 1
answered on 08 Feb 2010, 02:45 PM
hi Radoslav,
thnx a bunch, it worked perfectly..really appreciated
Tags
Grid
Asked by
FreshOne
Top achievements
Rank 1
Answers by
FreshOne
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or