HTML Form button click triggers Radgrid's previous grid event.

1 Answer 37 Views
Grid
Kasun
Top achievements
Rank 1
Kasun asked on 28 May 2024, 09:54 AM

Hi,

I have an HTML form and a RadGrid configured on a page. RadGrid's events and all the properties are dynamically built on the server side. RadGrid has an ExportToExcel toolbar command and works as it is. The issue is when I click the ExportToExcel toolbar button, It downloads the Excel file. When I click the HTML form's Save button, which means input type submit button, Radgrid downloads the Excel file again. If I click a different Radgrid toolbar command and then click the Form submit button, Radgrid will trigger its previous toolbar event.

Thanks

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 31 May 2024, 05:12 AM

Hello Kasun,

I tested the provided scenario, but since I don't know how the Grid is set up, I wasn't able to replicate the issue. Below you can see the code I tested with:

private RadGrid grid;

protected void Page_Init(object sender, EventArgs e)
{
    grid = new RadGrid
    {
        ID = "RadGrid1",
        Width = Unit.Pixel(600),
        MasterTableView = 
        { 
            CommandItemDisplay = GridCommandItemDisplay.Top 
        }
    };

    grid.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
    grid.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx;

    grid.NeedDataSource += Grid_NeedDataSource;
    grid.ItemCommand += Grid_ItemCommand;

    placeholder1.Controls.Add(grid);
}

private void Grid_ItemCommand(object sender, GridCommandEventArgs e)
{
    // Clear any previous command state
    if (e.CommandName == RadGrid.ExportToExcelCommandName)
    {
        // Handle the export command
        grid.MasterTableView.ExportToExcel();
    }
}

private void Grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = OrdersTable();
}

private DataTable OrdersTable()
{
    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
    dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("Freight", typeof(double)));
    dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
    dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));

    dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };

    for (int i = 0; i < 3; i++)
    {
        int index = i + 1;

        DataRow row = dt.NewRow();

        row["OrderID"] = index;
        row["OrderDate"] = DateTime.Now.Date.AddDays(index);
        row["Freight"] = index * 0.01;
        row["ShipName"] = "Name " + index;
        row["ShipCountry"] = "Country " + index;

        dt.Rows.Add(row);
    }

    return dt;
}

A potential fix for the issue would be to check the current command in the ItemCommand Event and only export when the ExportToExcel command is used.

If that doesn't help, I'd like to ask you to modify the above code so that it replicates the issue and send it for further investigation.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
Grid
Asked by
Kasun
Top achievements
Rank 1
Answers by
Vasko
Telerik team
Share this question
or