1

クリック イベントに応答する Web フォーム データグリッドに問題があります。説明させてください:

  1. ページが初めて読み込まれると、ユーザーが項目を選択するためのドロップダウンリストが表示されます。
  2. ユーザーがドロップダウンリストの項目を選択すると、データグリッド (tmdg と呼ばれる) が表示され (2 ページ目の読み込み)、ButtonColumns が含まれます。ユーザーがデータグリッドの ButtonColumns のいずれかでボタンを選択すると、ボタンの値が false から true に反転します (または、開始値に応じて true から false に反転します)。Page_Load イベントで、Page.IsPostBack==true の場合、次のようにイベント ハンドラーをデータグリッド (tmdg) に割り当てます。

  3. Tmdg_ItemCommand は、データテーブルと最終的にデータグリッド セル値を反転する Save() を呼び出すメソッドです。

これはすべて、データグリッドの非常に最初のクリックで機能します。ただし、その後のデータグリッドのクリックでは、button.DataTextField の値はグリッドの 2 回目のクリックでのみ反転します。(基本的に、シングルクリックではなく「ダブルクリック」)。私の目標は、クリック イベントごとに ButtonColumn のセルの値を反転することです。

注意: 値が正常に反転するグリッドを最初にクリックした後、セル (5,6) をクリックしても何も起こらない場合、セル (7,2) をクリックすると、そのセルが反転します (7 、2)。同様に、何も起こらないところで (5,2) をもう一度クリックし、(5,2) をもう一度選択して反転させることもできます。(これは、本質的に「ダブルクリック」の意味です)

その他の注意事項:

  1. 複数の場所でアプリケーション全体にイベント ハンドラーを割り当てようとしました (ページの OnInit で Page_Load の前、または UpdatePanel の Panel_Init メソッドで、または Page.IsPostBack が Page_Load であるかどうかに関係なく、または Page_Load の後)。

  2. datagrid は動的に読み込まれるコントロールで、Panel に配置されます。Panel は、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 
        }

    }
}

私には見えるように、BindGrid() の両方の「if」ステートメント内にブレークポイントを設定すると、最初の「if」がスキップされることが多く、その時点で Panel1 にデータグリッド tmdg が含まれていないことを意味します。この「if」は、無視される「最初のクリック」では明確にスキップされます。

私からの情報がさらに必要な場合はお知らせください。誰かが私が不適切にしていることを理解できることを願っています!! ありとあらゆるコメントをお待ちしております..

4

1 に答える 1