以编程方式设置itemcommand事件

本文关键字:itemcommand 事件 设置 方式 编程 | 更新日期: 2023-09-27 18:17:38

我有一个网格,我一直试图在后面的代码设置。到目前为止,一切都很顺利,但是什么时候我去设置itemcommand事件或任何事件,当我去单击命令或做一些应该触发事件的事情时,没有任何事情最终触发。所以我想知道我到底在我的项目命令上做错了什么。你会发现我的代码如下:

private void createRadGrid()
    {
        //create radgrid
        RadGrid rg = new RadGrid();
        rg.ID = "RadGridView";
        //setting the datasource and itemcommand event handler.
        rg.DataSourceID = "MachineDataSet";
        rg.ItemCommand += new GridCommandEventHandler(RadGridView_ItemCommand);
        rg.Width = 862;
        rg.CellSpacing = 2;
        rg.CellPadding = 4;
        rg.BorderWidth = 3;
        rg.BackColor = System.Drawing.Color.Transparent;
        rg.BorderColor = System.Drawing.Color.DarkGray;
        rg.ForeColor = System.Drawing.Color.Black;
        rg.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        rg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        rg.BorderStyle = BorderStyle.Ridge;
        rg.ShowStatusBar = true;
        rg.AllowPaging = true;
        rg.PageSize = 5;
        rg.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        rg.AutoGenerateColumns = false;
        rg.MasterTableView.PageSize = 5;
        rg.MasterTableView.DataKeyNames = new string[] { "ID" };
        rg.MasterTableView.ClientDataKeyNames = new string[] { "ID" };
        rg.MasterTableView.AutoGenerateColumns = false;
        rg.ClientSettings.Resizing.AllowColumnResize = true;
        rg.ClientSettings.Resizing.EnableRealTimeResize = true;
        rg.ClientSettings.Resizing.ResizeGridOnColumnResize = true;
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "ID";
        boundColumn.HeaderText = "ID";
        boundColumn.UniqueName = "MachineID";
        boundColumn.Visible = false;
        rg.MasterTableView.Columns.Add(boundColumn);
        GridBoundColumn boundColumn1 = new GridBoundColumn();
        boundColumn1.DataField = "SiteName";
        boundColumn1.HeaderText ="Site Name";
        boundColumn1.Resizable = true;
        boundColumn1.ReadOnly = true;
        rg.MasterTableView.Columns.Add(boundColumn1);
        GridBoundColumn boundColumn2 = new GridBoundColumn();
        boundColumn2.DataField = "Name";
        boundColumn2.HeaderText = "Machine Name";
        boundColumn2.Resizable = true;
        boundColumn2.ReadOnly = true;
        rg.MasterTableView.Columns.Add(boundColumn2);
        GridBoundColumn boundColumn3 = new GridBoundColumn();
        boundColumn3.DataField = "MachineType";
        boundColumn3.HeaderText = "Machine Type";
        boundColumn3.Resizable = true;
        boundColumn3.ReadOnly = true;
        rg.MasterTableView.Columns.Add(boundColumn3);
        GridBoundColumn boundColumn4 = new GridBoundColumn();
        boundColumn4.DataField = "MachineModel";
        boundColumn4.HeaderText = "Machine Model";
        boundColumn4.Resizable = true;
        boundColumn4.ReadOnly = true;
        rg.MasterTableView.Columns.Add(boundColumn4);
        GridButtonColumn buttonColumn = new GridButtonColumn();
        buttonColumn.ButtonType = GridButtonColumnType.PushButton;
        buttonColumn.CommandName = "AssignNewValues";
        buttonColumn.Resizable = true;
        buttonColumn.Text = "Assign New Values";
        rg.MasterTableView.Columns.Add(buttonColumn);
        PlaceHolder_RadGridView.Controls.Add(rg);
    }

问题区域似乎在这一行

rg.ItemCommand += new GridCommandEventHandler(RadGridView_ItemCommand);

以编程方式设置itemcommand事件

尝试在page_initpage_load事件中放置createRadGrid()。如果你在之后设置事件,那可能是它没有触发的原因。

希望对你有帮助。

欢呼。

试着移动这一行:

PlaceHolder_RadGridView.Controls.Add(rg);

右至行后:

RadGrid rg = new RadGrid();
rg.ID = "RadGridView";

看是否有影响