Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的itemCommand DataGrid事件只在第二次单击网格控件中的项时触发?_C#_Datagrid_Webforms_Itemcommand - Fatal编程技术网

C# 为什么我的itemCommand DataGrid事件只在第二次单击网格控件中的项时触发?

C# 为什么我的itemCommand DataGrid事件只在第二次单击网格控件中的项时触发?,c#,datagrid,webforms,itemcommand,C#,Datagrid,Webforms,Itemcommand,我的web窗体datagrid在响应单击事件时遇到问题。请允许我解释: 第一次加载页面时,将填充dropdownlist,供用户选择项目 当用户在dropdownlist中选择一个项目时,将出现一个数据网格(称为tmdg)(第二页加载),其中包含按钮列。当用户在datagrid中的一个ButtonColumn中选择一个按钮时,按钮的值将从false翻转为true(或从true翻转为false,具体取决于其起始值)。在Page_Load事件中,如果Page.IsPostBack==true,我将向

我的web窗体datagrid在响应单击事件时遇到问题。请允许我解释:

  • 第一次加载页面时,将填充dropdownlist,供用户选择项目
  • 当用户在dropdownlist中选择一个项目时,将出现一个数据网格(称为tmdg)(第二页加载),其中包含按钮列。当用户在datagrid中的一个ButtonColumn中选择一个按钮时,按钮的值将从false翻转为true(或从true翻转为false,具体取决于其起始值)。在Page_Load事件中,如果Page.IsPostBack==true,我将向datagrid(tmdg)分配一个事件处理程序,如下所示: tmdg.itemcond+=tmdg_itemcond

  • Tmdg_ItemCommand是调用Save()的方法,该方法翻转datatable并最终翻转datagrid单元格值

  • 这一切都适用于datagrid中的第一次单击。 但是,对于数据网格的后续单击,button.DataTextField值仅在第二次单击网格时翻转。(基本上是“双击”而不是单击一次)。我的目标是为每个单击事件翻转ButtonColumn中单元格的值

    请注意:在值成功翻转的网格第一次单击后,如果我可以单击单元格(5,6),但什么也没有发生,如果我单击单元格(7,2),我将得到该单元格(7,2)的翻转。同样,我也可以在没有发生任何事情的地方再次单击(5,2),然后再次选择(5,2)以获得翻转。(这就是我所说的“双击”)

    其他说明:

  • 我已经尝试在多个位置(在页面的OnInit中加载页面之前;或者在UpdatePanel的Panel Init方法中;或者不管Page.IsPostBack是否在页面加载中;或者在页面加载之后)在整个应用程序中分配事件处理程序

  • datagrid是一个动态加载的控件,放置在面板上,而面板又放置在UpdatePanel上

  • 我将尽量不在这里放置大量混乱的代码,但我想为您提供一些东西。为了简洁起见,我对它作了一些修改

    ::Push.aspx::::

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Push.aspx.cs" Inherits="TMUWF.Push"  MasterPageFile="~/Site.Master" %>
        <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
            <asp:DropDownList ID="DropDownList1" 
                OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                runat="server" 
                AutoPostBack="True"
                AppendDataBoundItems="true" 
                OnMouseDown="this.size=10;" 
                OnFocusOut="this.size=1;" 
                OnDblClick="this.size=1;"
                >
            </asp:DropDownList>
    
            <asp:UpdatePanel  ID="UpdatePanel1" runat="server" UpdateMode="Conditional" OnInit="Panel_Init">
                <contenttemplate>
                    <h3 id="div-Col-Title">Node</h3>
                    <asp:Panel runat="server" ID="Panel1">
                        <div id="div-Row-Title"><h3 >Channel</h3></div>
                    </asp:Panel>
                </contenttemplate>
            </asp:UpdatePanel>
        </asp:Content>
    
    
    节点
    频道
    
    ::Push.aspx.cs:::

    namespace TMUWF 
    {
        public partial class Push : System.Web.UI.Page
        {
            DataGrid tmdg = new DataGrid
            {
                AutoGenerateColumns = false,
                CssClass = "gvClass push"            
            };
            DataTable TraffMat = new DataTable();
            DataView TraffMatView;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    UpdatePanel1.Visible = false;
                    FillDropDown();
                }
                else if (!(Session["PushIntId"] == null))
                {
                    int IntID = GetSession();
                    BindGrid(IntID);
    
                    // instead of checking for null, just remove the event handler
                    tmdg.ItemCommand -= Tmdg_ItemCommand;
                    // Manually register the event-handling method for the item click 
                    tmdg.ItemCommand += Tmdg_ItemCommand;
                }
            }
    
            private void FillDropDown()
            { //redacted, pulls values from database for dropdownlist 
            }
    
            private void BindGrid(int IntID)
            {
                if (Panel1.Controls.Contains(tmdg))
                {
                    Panel1.Controls.Remove(tmdg);
                }
                SaveSession(IntID);
    
                tmdg = BuildTmdg(tmdg, TraffMat);
                TraffMatView = new DataView(TraffMat);
    
                // Set the data source and bind to the Data Grid control.
                tmdg.DataSource = TraffMatView;
                tmdg.DataBind();
    
                if (!Panel1.Controls.Contains(tmdg))
                {
                    Panel1.Controls.Add(tmdg);
                }
                UpdatePanel1.Visible = true;
                UpdatePanel1.Update();
            }
    
            private DataGrid BuildTmdg(DataGrid dg, DataTable dt)
            {
                dg.Columns.Clear();
                for (int col = 0; col<17; col++)
                {
                    if (col == 0)
                    {
                        BoundColumn bc = new BoundColumn
                        {
                            HeaderText = " ",
                            DataField = dt.Columns[col].ToString(),
                            ReadOnly = true
                        };
                        dg.Columns.Add(bc);
                    }
                    else
                    {
                        ButtonColumn btnc = new ButtonColumn
                        {
                            HeaderText = col.ToString(),
                            ButtonType = ButtonColumnType.PushButton,
                            DataTextField = dt.Columns[col].ToString(),
                            CommandName = col.ToString()
                        };
                        dg.Columns.Add(btnc);
                    }
                }
                return dg;
            }
    
            private void Tmdg_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                Save((Int32)Session["PushIntID"], Convert.ToInt32(e.CommandName), e.Item.DataSetIndex+1);
            }
    
            private void Save(int IntID, int col, int row)
            {
                int newIntID = IntID;
                int newcol = col;
                int newrow = row;
    
                // Apply changes to DataTable
                string newVal = UpdateDataTable(IntID, col, row);
    
                // Apply changes to Database
                int rowsAffected = Apply(IntID, col, row, newVal);
    
                // Bind DataTable to TMDG
                BindGrid(IntID);
            }
    
            private string UpdateDataTable(int IntID, int col, int row)
            {
                row--;
                string val = TraffMat.Rows[row][col].ToString();
                if (val == "False")
                {
                    val = "True";
                    TraffMat.Rows[row][col] = val;
                }
                else
                {
                    val = "False";
                    TraffMat.Rows[row][col] = val;
                }
                TraffMat.AcceptChanges();
                SaveSession(IntID);
                TraffMatView = new DataView(TraffMat);
                return val;
            }
    
            private int Apply(int IntID, int col, int row, string NewVal)
            { //redacted, saves values to database 
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            { //redacted, fills DataTable from database, calls SaveSession, calls BindGrid 
            }
    
            private int GetSession()
            { //redacted, gets session state 
            }
    
            private void SaveSession(int IntID)
            { //redacted, sets session state 
            }
    
        }
    }
    
    名称空间TMUWF
    {
    公共部分类推送:System.Web.UI.Page
    {
    DataGrid tmdg=新DataGrid
    {
    AutoGenerateColumns=false,
    CssClass=“gvClass推送”
    };
    DataTable TraffMat=新DataTable();
    DataView TraffMatView;
    受保护的无效页面加载(对象发送方、事件参数e)
    {
    如果(!Page.IsPostBack)
    {
    UpdatePanel1.Visible=false;
    FillDropDown();
    }
    else if(!(会话[“PushIntId”]==null))
    {
    int IntID=GetSession();
    BindGrid(IntID);
    //只需删除事件处理程序,而不是检查null
    tmdg.itemcond-=tmdg_itemcond;
    //手动注册项目的事件处理方法单击
    tmdg.itemcond+=tmdg_itemcond;
    }
    }
    私有空填充下拉列表()
    {//已编辑,从数据库中为dropdownlist提取值
    }
    私有void BindGrid(int IntID)
    {
    if(控制面板1.包含(tmdg))
    {
    面板1.控制装置。拆除(tmdg);
    }
    保存会话(IntID);
    tmdg=建筑物tmdg(tmdg,交通流量);
    TraffMatView=新数据视图(TraffMat);
    //设置数据源并绑定到数据网格控件。
    tmdg.DataSource=TraffMatView;
    tmdg.DataBind();
    如果(!Panel1.Controls.Contains(tmdg))
    {
    控制面板1.添加(tmdg);
    }
    UpdatePanel1.Visible=true;
    UpdatePanel1.Update();
    }
    专用DataGrid BuildTmdg(DataGrid dg,DataTable dt)
    {
    dg.Columns.Clear();
    
    对于(int col=0;col,我没有在Push.aspx.cs中实例化datagrid tmdg,而是将其添加到Push.aspx中,每次都会触发click事件。我不确定为什么在.aspx中可以这样做,但在.aspx.cs文件中不能这样做

    下面是新代码来解释

    ::Push.aspx:::(此处添加了datagrid)


    关于为什么的想法很受欢迎。

    我没有在Push.aspx.cs中实例化datagrid tmdg,而是将其添加到Push.aspx中,每次都会触发单击事件。我不确定为什么在.aspx中可以这样做,但在.aspx.cs文件中不能

    下面是新代码来解释

    ::Push.aspx:::(此处添加了datagrid)

    关于为什么的思考受到赞赏

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Push.aspx.cs" Inherits="TMUWF.Push"  MasterPageFile="~/Site.Master" %>
        <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
            <asp:DropDownList ID="DropDownList1" 
                OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                runat="server" 
                AutoPostBack="True"
                AppendDataBoundItems="true" 
                OnMouseDown="this.size=10;" 
                OnFocusOut="this.size=1;" 
                OnDblClick="this.size=1;"
                >
            </asp:DropDownList>
    
            <asp:UpdatePanel  ID="UpdatePanel1" runat="server" UpdateMode="Conditional" OnInit="Panel_Init">
                <contenttemplate>
                    <h3 id="div-Col-Title">Node</h3>
                    <asp:Panel runat="server" ID="Panel1">
                        <div id="div-Row-Title"><h3 >Channel</h3></div>
                        <asp:DataGrid ID="tmdg" CssClass="gvClass push" AutoGenerateColumns="false" runat="server" >
                        </asp:DataGrid>
                    </asp:Panel>
                </contenttemplate>
            </asp:UpdatePanel>
        </asp:Content>
    
            //DataGrid tmdg = new DataGrid
            //{
            //    AutoGenerateColumns = false,
            //    CssClass = "gvClass push"            
            //};