C# 为什么我的itemCommand DataGrid事件只在第二次单击网格控件中的项时触发?
我的web窗体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,我将向
<%@ 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"
//};