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

Clientside fireCommand and ItemCommand

7 Answers 405 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 26 Sep 2011, 03:33 PM
Hello,
This is what I do from client side:
 var master = $find(gridID).get_masterTableView();
 master.fireCommand("InitInsert""");
Grid correctly creates a new row but I never get to ItemCommand handler in codebehind. Why?
Thank you.

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Sep 2011, 06:40 AM
Hi Dmitry,

I have tried the same and the ItemCommand fired as expected. As an alternate you can try the following approach from server side.

C#:
protected void button1_Click(object sender, EventArgs e)
{
       GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
       commandItem.FireCommandEvent("InitInsert", null);
}
 
 protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
     //check the commandname here.
}

Thanks,
Princy.
0
Dmitry
Top achievements
Rank 1
answered on 27 Sep 2011, 10:00 AM
Thank you for reply. I tried both fireCommand and mastertableview.showInsertItem() functions call but still no success. It will not even enter ItemCommand handler. And I forgot something - my toolbar button has PostBack property set to false.
As for your scenery, the line (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem) does not return any items despite I have a CommandColumn in grid.
0
Dmitry
Top achievements
Rank 1
answered on 28 Sep 2011, 01:50 PM
Anyone please?
I forgot something to add. Grid is completely created in Page_Init handler. All handlers are also added there.
0
Princy
Top achievements
Rank 2
answered on 29 Sep 2011, 06:53 AM
Hi Dmitry,

I tired the same and that is worked as expected. Just go through the following code.

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Radgrid_pgmeticcreation.aspx.cs"
    Inherits="Radgrid_Default5" %>
 
<%@ 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">
<head runat="server">
 <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="scriptmngr" runat="server">
        </asp:ScriptManager>
        <asp:Button ID="Button1" runat="server" Text="Click" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
<script type="text/javascript" language="javascript">
  function test(id)
   {
        var grid = $find(id);
        var masterTable = grid.get_masterTableView();
        masterTable.fireCommand("InitInsert", "");
        return false;
    }
</script>

C#:
public partial class Radgrid_Default5 : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        PlaceHolder PlaceHolder1 = new PlaceHolder();
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        form1.Controls.Add(PlaceHolder1);
        form1.Controls.Add(RadGrid1);
        RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
        RadGrid1.PageSize = 5;
        RadGrid1.AllowPaging = true;
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.DataSourceID = "SqlDataSource1";
        RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "Customer ID";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "ContactName";
        boundColumn.HeaderText = "Contact Name";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
        Button1.Attributes.Add("onclick", "test('" + RadGrid1.ClientID + "');");
    }
protected void Page_Load(object sender, EventArgs e)
   {
    
   }
void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
   }
}

Thanks,
Princy.
0
Dmitry
Top achievements
Rank 1
answered on 29 Sep 2011, 09:26 AM
Hello Princy,
Thanks for your reply. I tried to reproduce your sample and got a full success. Except one thing - if you remove line CommandItemDisplay = GridCommandItemDisplay.Top from Page_Init, fireCommand will never fire ItemCommand event. I have a toolbar where I put an "Add record" button and I don't want to use grid's default "Add record" button to appear.
0
Princy
Top achievements
Rank 2
answered on 29 Sep 2011, 10:00 AM
Hi Dmitry,

I have faced the same issue without that CommandItem. One approach is to set that in pageInit and explicitly hide in PreRender event. Here is the code.

C#:
void RadGrid1_PreRender(object sender, EventArgs e)
 {
       RadGrid grid = sender as RadGrid;
       GridCommandItem headerItem = grid.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem;
       headerItem.Display = false;
 
 }

Thanks,
Princy.
0
Dmitry
Top achievements
Rank 1
answered on 29 Sep 2011, 12:18 PM
Hi Princy.
Thank you for workaround. There's 1 more way to make it work correctly in my case - put toolbar into CommandItemTemplate and assign CommandName="InitInsert" to one of its buttons. In this case everything works fine.
Tags
Grid
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dmitry
Top achievements
Rank 1
Share this question
or