我有一个问题,我的网页表单数据,响应一个点击事件。请允许我解释:
所有这些都适用于数据网格中的第一次单击。但是,对于随后单击的数据,button.DataTextField值仅在网格的第二次单击时翻转。(本质上是“双击”,而不是一次单击)。我的目标是为每个单击事件翻转ButtonColumn中单元格的值。
请注意:在值成功翻转的网格第一次单击之后,如果我单击单元格(5,6),如果没有发生任何事情,如果单击单元格(7,2),我将得到该单元格的翻转(7,2)。同样,我只需再次单击(5,2)没有发生任何事情,然后选择(5,2)再次得到翻转。(这就是我所说的“双击”)
其他说明:
我将尽量不要在这里放置一堆乱七八糟的代码,但我确实想为您提供一些东西。为了简洁起见,我把它改了一点。
::::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
}
}
}在我看来,当我在BindGrid()中的两个"if“语句中设置一个断点时,通常会跳过第一个"if”,这意味着Panel1当时不包含我的数据式tmdg。这个"if“在被忽略的"first-click”中被特别跳过。
如果你需要我提供更多的信息,请告诉我!我希望你们中的一个能弄清楚我做的不对!!任何和所有的评论都很感谢。
发布于 2019-03-26 14:50:51
我没有在Push.aspx.cs中实例化datagrid,而是将它添加到Push.aspx中,每次单击事件都会触发。我不知道为什么这在.aspx中工作,但在.aspx.cs文件中不起作用。
下面是新代码来解释..。
::::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:DataGrid ID="tmdg" CssClass="gvClass push" AutoGenerateColumns="false" runat="server" >
</asp:DataGrid>
</asp:Panel>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>::::Push.aspx.cs:(此处移除数据)
//DataGrid tmdg = new DataGrid
//{
// AutoGenerateColumns = false,
// CssClass = "gvClass push"
//};关于为什么欣赏的思考。
https://stackoverflow.com/questions/55345366
复制相似问题