Articles in this section
Category / Section

How to upload the image without the upload box?

1 min read

This knowledge base explains How to upload the image in Inline-form Template without using Upload button.

Solution:

In uploadBox when we upload the image the saveURL will be triggered once when we click the upload button in uploadBox.

If we need to trigger the saveURL when we click on save button in inline-form Template, we need to hide upload button by defining false for the showFileDetails API of ejUploadBox.

In actionBegin event we will triggered the saveURL of ejUploadBox using Upload method of ejUploadBox

 

1. Render the Grid as follows.

 

HTML

 

<div id="OrdersGrid"></div>

 

JS

<script type="text/javascript">
        $(function () {
            $("#OrdersGrid").ejGrid({
                // the datasource "window.gridData" is referred from jsondata.min.js
                dataSource: window.gridData,
                allowPaging: true,
                editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: ej.Grid.EditMode.InlineTemplateForm, inlineFormTemplateID:"#CustomerForm" },
                toolbarSettings: { showToolbar: true, toolbarItems : ["add", "edit", "delete", "update", "cancel"] },
                actionComplete: "complete",
                actionBegin: "begin",
                columns: [
                        { field: "OrderID", headerText: "Order ID",isPrimaryKey:true, width: 100 },
                        { field: "CustomerID", headerText: "Customer ID", width: 100 },              
                        { field: "EmployeeID", headerText: "Employee ID", width: 100 },
                ]
            });
        });
    </script>

 

ASP

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <div>
        <ej:Grid ID="OrdersGrid" runat="server"  AllowPaging="True" >
            <ClientSideEvents ActionComplete="complete" ActionBegin="begin" />
            <ToolbarSettings ShowToolbar="true" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
             <Columns>
                <ej:Column Field="OrderID" IsPrimaryKey="True" Width="90" />
                <ej:Column Field="CustomerID"  Width="100" />
                <ej:Column Field="EmployeeID" Width="100" />
            </Columns>
           <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True" EditMode="InlineFormTemplate" InlineFormTemplateID="#CustomerForm"></EditSettings>
        </ej:Grid>
    </div>
</asp:Content>

 

[CS]
 
namespace SyncfusionASPNETApplication2
{
public partial class _Default : Page
    {      
     protected void Page_Load(object sender, EventArgs e)
        {
            this.OrdersGrid.DataSource = OrderRepository.GetAllRecords();
            this.OrdersGrid.DataBind();
        }
    }
}

 

MVC

@(Html.EJ().Grid<MvcApplication14.OrdersView>("OrdersGrid")
  .Datasource((IEnumerable<object>)ViewBag.datasource)
 .AllowPaging(true)    /*Paging Enabled*/
 .EditSettings(edit => { edit.AllowEditing().AllowAdding().AllowDeleting().EditMode(EditMode.InlineFormTemplate).InlineFormTemplateID("#CustomerForm"); })
.ToolbarSettings(toolbar =>
 {
     toolbar.ShowToolbar().ToolbarItems(items =>
     {
         items.AddTool(ToolBarItems.Add);
         items.AddTool(ToolBarItems.Edit);
         items.AddTool(ToolBarItems.Delete);
         items.AddTool(ToolBarItems.Update);
         items.AddTool(ToolBarItems.Cancel);
     });
 })
 .Columns(col =>
  {
      col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(100).Add();
      col.Field("CustomerID").HeaderText("Customer ID").Width(100).Add();
      col.Field("EmployeeID").HeaderText("Employee ID").Width(100).Add();
  })
 .ClientSideEvents(eve => { eve.ActionComplete("complete").ActionBegin("begin"); }) )

 

[CS]
 
namespace SyncfusionMvcApplication121.Controllers
{
    public class GridController : Controller
    {
        public ActionResult GridFeatures()
        {
            IEnumerable DataSource = new NorthwindDataContext().OrdersViews.ToList();
            ViewBag.datasource = DataSource;
            return View();
        }

 

 

CORE

<ej-grid id="OrdersGrid" allow-paging="true" datasource="ViewBag.dataSource" action-begin="begin" action-complete="complete" >
    <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" edit-mode="InlineFormTemplate" inline-form-template-id="#CustomerForm"></e-edit-settings>
    <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() { "add","edit","delete","update","cancel" })"></e-toolbar-settings>
    <e-columns>
        <e-column field="OrderID" header-text="Order ID" is-primary-key="true" width="100"></e-column>
        <e-column field="CustomerID" header-text="Customer ID" width="100"></e-column>
        <e-column field="EmployeeID" header-text="Employee ID" width="100"></e-column>
    </e-columns>
</ej-grid>

 

[CS]
 
namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private NORTHWNDContext _context;
        public HomeController(NORTHWNDContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            ViewBag.dataSource = _context.Orders.Take(100).ToList();
return View();
        }
     }
 }

 

Angular

<ej-grid id="OrdersGrid" [dataSource]="gridData" [allowPaging]="true" [toolbarSettings]="toolbarItems" [editSettings]="editSettings" (actionBegin)="begin($event)" (actionComplete)="complete($event)" > 
   <e-columns>
        <e-column field="OrderID" headertext="Order ID" [isPrimaryKey] ="true" width="100"></e-column>
        <e-column field="CustomerID" headertext="Customer ID" width="100"></e-column>
        <e-column field="EmployeeID" headertext="Employee ID" width="100"></e-column>
    </e-columns>
</ej-grid>

 

TS

export class AppComponent {   
     public toolbarItems;
     public editSettings;
     public gridData: any;
 
constructor() {     
     this.gridData = (window as any).gridData;
     this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: ej.Grid.EditMode.InlineTemplateForm, inlineFormTemplateID:"#CustomerForm" };
     this.toolbarItems = { showToolbar : true, toolbarItems : ["add", "edit", "delete", "update", "cancel"]};           
     }
     begin(args: any) { 
        if (args.requestType == "save") {
                var uploadObj= $('#UploadInput').ejUploadbox("instance");
                uploadObj.upload();
        }   
     }
     complete(args: any){
        if ((args.requestType == "beginedit" || args.requestType == "add")) {
                $('#UploadInput').ejUploadbox({
                    saveUrl: "/saveFiles.ashx",
                    showFileDetails: false,
                    fileSelect : "select",
                });
         }
     }
}

 

Index.html

 

function select(args) {

            var filename = args.files[0].name;

            $("#imageName").html(filename);

        }

 

 

2. Defining inlineForm template for editing.

 

<script type="text/template" id="CustomerForm">
        <b>Order Details</b>
        <table cellspacing="10">
            <tr>
                <td style="text-align: right;">Order ID
                </td>
                <td style="text-align: left">
                    <input id="OrderID" name="OrderID" value="{{: OrderID}}" disabled="disabled"
                        class="e-field e-ejinputtext valid e-disable" style="text-align: right; width: 116px; height: 28px" />
                </td>
                <td style="text-align: right;">Customer ID
                </td>
                <td style="text-align: left">
                    <input id="CustomerID" name="CustomerID" value="{{:CustomerID}}" class="e-field e-ejinputtext valid"
                        style="width: 116px; height: 28px; text-align: left!important" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right">Picture
                </td>
                <td style="text-align: left">               
                    <div id="UploadInput"></div>
                    <p id="imageName"></p>
                </td>
 
            </tr>
        </table>
    </script>

 

3. ClientSide events for rendering the ejUploadBox and saving the image files.

 

    <script type="text/javascript">
        function complete(args) {
            //render uploadbox control
            if ((args.requestType == "beginedit" || args.requestType == "add")) {
                $('#UploadInput').ejUploadbox({
                    saveUrl: "/saveFiles.ashx",
                    showFileDetails: false,
                    fileSelect : "select",
                });
            }
        }
        function begin(args) {
            if (args.requestType == "save") {
                var uploadObj = $('#UploadInput').ejUploadbox("instance");
                uploadObj.upload();
            }
        }
        function select(args) {
            var filename = args.files[0].name;
            $("#imageName").html(filename);
        }
    </script>

 

 

Result:

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment