How to access input control of RadAsyncUpload inside RadGrid

6 posts, 1 answers
  1. Robin
    Robin avatar
    41 posts
    Member since:
    Sep 2012

    Posted 07 Oct 2013 Link to this post

    Hello all, 

    Is there a way to access input control of RadAsyncUpload control from Grid ItemDataBound event?
    When grid row is in edit mode, I need to display the file name that user have previously chosen.

    I used findControl of RadAsyncUpload control like below. But cannot find child control.

    protected void gridMembers_ItemDataBound(object sender, GridItemEventArgs e)
    {
                if (e.Item.IsInEditMode)
                {
                    RadAsyncUpload ascUpload = e.Item.FindControl("RadAsyncUploadFile") as RadAsyncUpload;
                    Control l_Control = ascUpload.FindControl(ascUpload.ClientID.Replace(ascUpload.ID, "file1"));
    // null
                }
    }

    Thanks in advanced,
    Robin
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Oct 2013 Link to this post

    Hi Robin,

    Please try the following code snippet to get the last uploaded file from RadGrid ItemDataBound  event.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            RadAsyncUpload RadAsyncUpload1 = (RadAsyncUpload)edit.FindControl("RadAsyncUpload1");
            string path = Server.MapPath(RadAsyncUpload1.TargetFolder);
            DirectoryInfo targetDir = new DirectoryInfo(path);
            var myFile = targetDir.GetFiles()
                .OrderByDescending(f => f.LastWriteTime)
                .First();
            string name = myFile.Name;
            Label label1 = (Label)edit.FindControl("Label2");
            label1.Text = name;
        }
    }

    Also you can access input control of RadAsyncUpload either by getElementsByTagName or by getElementsByClassName as follows.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
            string script = "function f(){GetInput(" + RadAsyncUpload1.ClientID + "); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
    }

    JavaScript:
    <script type="text/javascript">
        function GetInput(RadAsync) {
            var inputcontrol = RadAsync.getElementsByTagName("<input type='file'>"); // byTageName
                                //or
            var inputcontrol = RadAsync.getElementsByClassName("ruFileInput"); //byClassName
        }
    </script>

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Robin
    Robin avatar
    41 posts
    Member since:
    Sep 2012

    Posted 10 Oct 2013 Link to this post

    This works!!!
    Thanks for the support.
  5. Ramkumar
    Ramkumar avatar
    12 posts
    Member since:
    Nov 2013

    Posted 29 Nov 2013 Link to this post

    How to insert the file using radasyncupload in radgrid item inserrted command???
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Nov 2013 Link to this post

    Hi Ramkumar,

    I guess you want to insert the Uploaded FileName into the DataBase in RadGrid InsertCommand event. Please have a look into the sample code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
        OnInsertCommand="RadGrid1_InsertCommand" OnItemInserted="RadGrid1_ItemInserted" >
        <MasterTableView CommandItemDisplay="Top">
            <Columns>
                <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID" HeaderText="OrderID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CustomerID" UniqueName="CustomerID" HeaderText="CustomerID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipAddress" UniqueName="ShipAddress" HeaderText="ShipAddress">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <EditItemTemplate>
                        <telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server">
                        </telerik:RadAsyncUpload>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings Selecting-AllowRowSelect="true">
        </ClientSettings>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
        {
            GridEditFormInsertItem editItem = (GridEditFormInsertItem)e.Item;
            RadAsyncUpload upload = (RadAsyncUpload)editItem.FindControl("RadAsyncUpload1");
            string filename= upload.UploadedFiles[0].FileName;
                //Query to insert the filename in database
        }
    }

    Thanks,
    Shinu.
  7. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 15 Nov 2014 in reply to Shinu Link to this post

    Basically, I want the Record_ID from table to be concatenated with the FileName.

    Can you please specify how to write an insert/update query in the InsertCommand event to upload the name concatenated with Record_ID in the database. I am using Entity Framework.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017