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

[Solved] How to validate a RadUpload control inside an edititemtemplate

1 Answer 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Condorito
Top achievements
Rank 1
Condorito asked on 30 Nov 2009, 08:19 PM
Hi,

I have a RadUpload control inside an edititemtemplate.  I was able to get it work by looking at some examples I've found around here, but the only thing I can't seem to be able to do is validate the field.   If a user doesn't select a file to upload I want it to display a message but it doesn't display anything for some reason.  I posted a strip-down version of my project below, please advice as to what I'm doing wrong, thank you!

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="RadGridUpload.aspx.vb" Inherits="MyTestProject.RadGridUpload" %> 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">  
    <title></title>  
      
     <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">  
         
         
        <script type="text/javascript">  
            //<![CDATA[
       
            function conditionalPostback(e, sender) {
               
                var theRegexp = new RegExp("\.UpdateButton$|\.PerformInsertButton$", "ig");
                if (sender.get_eventTarget().match(theRegexp)) {
                  
                    var upload = $find(window['UploadId']);
                    //AJAX is disabled only if file is selected for upload
                    if (upload.getFileInputs()[0].value != "") {
                        sender.set_enableAjax(false);
                    }
                }
            }
            function validateRadUpload(source, e) {
                alert("true");
                e.IsValid = false;
               
                var upload = $find(source.parentNode.getElementsByTagName('div')[0].id);
                var inputs = upload.getFileInputs();
                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;
                    }
                }
            }
            
            //]]> 
      </script> 
         
         
     
          
        </telerik:RadScriptBlock> 
</head> 
<body> 
    <form id="form1" runat="server">  
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
    </telerik:RadScriptManager> 
    <div> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">  
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="MyRadGrid" /> 
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
          
          
        Click Edit to Upload a File:  
        <br /> 
          
          
        <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1" ClientEvents-OnRequestStart="conditionalPostback">  
          
         
        <telerik:RadGrid runat="server" ID="MyRadGrid" AutoGenerateColumns="false" > 
            <MasterTableView> 
                <Columns> 
                    <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn> 
                    <telerik:GridTemplateColumn HeaderText="Upload" UniqueName="Upload">  
                        <ItemTemplate> 
                                <%#Eval("Col1")%> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                                <telerik:RadUpload runat="server" ID="RadUpload" ControlObjectsVisibility="None" /> 
                        </EditItemTemplate> 
                          
                    </telerik:GridTemplateColumn> 
                      
                </Columns> 
              
            </MasterTableView> 
          
        </telerik:RadGrid> 
         </telerik:RadAjaxPanel> 
    </div> 
    </form> 
</body> 
</html> 
 

Imports Telerik.Web.UI  
Partial Public Class RadGridUpload  
    Inherits System.Web.UI.Page  
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
 
    End Sub 
 
    Private Sub MyRadGrid_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles MyRadGrid.NeedDataSource  
        MyRadGrid.DataSource = GridDataSource()  
    End Sub 
 
 
    Private Function GridDataSource() As DataTable  
        Dim dtMyTable As New DataTable  
        dtMyTable.Columns.Add("Col1")  
        Dim dtRow As DataRow = dtMyTable.NewRow  
        dtRow("Col1") = "2" 
        dtMyTable.Rows.Add(dtRow)  
 
        Return dtMyTable  
 
    End Function 
 
    Private Sub MyRadGrid_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles MyRadGrid.ItemCreated  
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then 
            Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
            Dim upload As RadUpload = TryCast(e.Item.FindControl("RadUpload"), RadUpload)  
            Dim validator As CustomValidator = New CustomValidator()  
            validator.ErrorMessage = "Please select file to be uploaded" 
            validator.ClientValidationFunction = "validateRadUpload" 
            validator.Display = ValidatorDisplay.Dynamic 
 
            DirectCast(upload.Parent, TableCell).Controls.Add(validator)  
 
        End If 
    End Sub 
 
    Private Sub MyRadGrid_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles MyRadGrid.ItemDataBound  
        If TypeOf (e.Item) Is GridEditableItem AndAlso e.Item.IsInEditMode Then 
            Dim upload As RadUpload = TryCast(e.Item.FindControl("RadUpload"), RadUpload)  
            RadAjaxPanel1.ResponseScripts.Add(String.Format("window['UploadId'] = '{0}';", upload.ClientID))  
        End If 
    End Sub 
 
End Class 

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 01 Dec 2009, 06:03 PM
Hello Juan,

The flexible validation mechanism of RadGrid is demonstrated in the following online resources:
http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/DataEditing/Validation/DefaultVB.aspx
http://www.telerik.com/help/aspnet-ajax/grdvalidation.html

Feel free to utilize the logic presented their for your custom case in order to attain the validation functionality you are after.

Greetings,
Pavlina
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.
Tags
Grid
Asked by
Condorito
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or