Why does my itemCommand DataGrid event only fire on the second click of an item in the grid control?Button in a Repeater does not fire ItemCommandItemCommand not firing on first click in Repeater or GridViewItemCommand event doesn't fire with the Repeater controlItemCommand event doesn't fire with repeater controlListView ItemCommand Event Not Firing in FirefoxItemCommand event not firing for DataListGet ID of the Control which fires ItemCommand in RadGridStrange behaviour of ASP.NET DataGrid ItemCommand eventRepeater does not fire ItemCommandWPF - Update Image Source in DataGrid on Click

Is conquering your neighbors to fight a greater enemy a valid strategy?

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

How do I check that users don't write down their passwords?

What's the big deal about the Nazgûl losing their horses?

How to supply water to a coastal desert town with no rain and no freshwater aquifers?

Does the force of friction helps us to accelerate while running?

Red and White Squares

What is the highest level of accuracy in motion control a Victorian society could achieve?

Shipped package arrived - didn't order, possible scam?

How would a sea turtle end up on its back?

Is it acceptable that I plot a time-series figure with years increasing from right to left?

Machine Learning Golf: Multiplication

n-level Ouroboros Quine

Should I cheat if the majority does it?

Did William Shakespeare hide things in his writings?

Is it possible to spoof an IP address to an exact number?

Is it bad to suddenly introduce another element to your fantasy world a good ways into the story?

Boss furious on bad appraisal

What can a novel do that film and TV cannot?

Why no parachutes in the Orion AA2 abort test?

Will Jimmy fall off his platform?

How important is it for multiple POVs to run chronologically?

Is kapton suitable for use as high voltage insulation?

Are "confidant" and "confident" homophones?



Why does my itemCommand DataGrid event only fire on the second click of an item in the grid control?


Button in a Repeater does not fire ItemCommandItemCommand not firing on first click in Repeater or GridViewItemCommand event doesn't fire with the Repeater controlItemCommand event doesn't fire with repeater controlListView ItemCommand Event Not Firing in FirefoxItemCommand event not firing for DataListGet ID of the Control which fires ItemCommand in RadGridStrange behaviour of ASP.NET DataGrid ItemCommand eventRepeater does not fire ItemCommandWPF - Update Image Source in DataGrid on Click






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am having an issue with my web forms datagrid responding to a click event. Allow me to explain:



  1. Upon the page being loaded for the first time, a dropdownlist is filled for the user to select an item.

  2. When the user selects an item in the dropdownlist, a datagrid (called tmdg) appears (2nd page load) with ButtonColumns in it. When the user selects a button in one of the ButtonColumns in the datagrid, the value of the button flips from false to true (or from true to false, depending on its starting value). In the Page_Load event, if Page.IsPostBack==true, i assign an event handler to the datagrid (tmdg) like so:
    tmdg.ItemCommand += Tmdg_ItemCommand;


  3. Tmdg_ItemCommand is the method that calls Save() which flips the datatable and ultimately the datagrid cell value.


This all works for the VERY FIRST click in the datagrid.
HOWEVER, for subsequent clicking of the datagrid, the button.DataTextField values only flip on the SECOND click of the grid. (essentially a "double click" instead of a single click). My goal is to flip the value of the cell in the ButtonColumn for every single click event.



Please note: after the first click of the grid where the value flips succesfully, if I may click cell (5,6) where nothing happens, if then i click cell (7,2) I will get a flip of that cell (7,2). Likewise, I could just click (5,2) again where nothing happens, then select (5,2) again to get the flip. (this is what i mean by essentially a "double click")



Other notes:



  1. I have tried assigning the event handler ALL OVER the application in mulitple spots (before Page_Load in OnInit of the page; or in the UpdatePanel's Panel_Init method; or regardless of if Page.IsPostBack in Page_Load; or after Page_Load)


  2. The datagrid is a dynamically loaded control that is placed on a Panel which, in turn, is placed on an UpdatePanel.


I will try not to place a huge mess of code here, but do want to provide you with SOMETHING. I have redacted it a bit for the sake of brevity.



::::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






As it appears to me, when i set a breakpoint inside both "if" statements in BindGrid() the first "if" is often skipped implying that Panel1 does not contain my datagrid tmdg at that moment in time. This "if" is specifically skipped in that "first-click" that is ignored.



Please let me know if you need any more information from me!
I hope that one of you can figure out what i'm doing improperly!!
Any and all comments appreciated..










share|improve this question




























    1















    I am having an issue with my web forms datagrid responding to a click event. Allow me to explain:



    1. Upon the page being loaded for the first time, a dropdownlist is filled for the user to select an item.

    2. When the user selects an item in the dropdownlist, a datagrid (called tmdg) appears (2nd page load) with ButtonColumns in it. When the user selects a button in one of the ButtonColumns in the datagrid, the value of the button flips from false to true (or from true to false, depending on its starting value). In the Page_Load event, if Page.IsPostBack==true, i assign an event handler to the datagrid (tmdg) like so:
      tmdg.ItemCommand += Tmdg_ItemCommand;


    3. Tmdg_ItemCommand is the method that calls Save() which flips the datatable and ultimately the datagrid cell value.


    This all works for the VERY FIRST click in the datagrid.
    HOWEVER, for subsequent clicking of the datagrid, the button.DataTextField values only flip on the SECOND click of the grid. (essentially a "double click" instead of a single click). My goal is to flip the value of the cell in the ButtonColumn for every single click event.



    Please note: after the first click of the grid where the value flips succesfully, if I may click cell (5,6) where nothing happens, if then i click cell (7,2) I will get a flip of that cell (7,2). Likewise, I could just click (5,2) again where nothing happens, then select (5,2) again to get the flip. (this is what i mean by essentially a "double click")



    Other notes:



    1. I have tried assigning the event handler ALL OVER the application in mulitple spots (before Page_Load in OnInit of the page; or in the UpdatePanel's Panel_Init method; or regardless of if Page.IsPostBack in Page_Load; or after Page_Load)


    2. The datagrid is a dynamically loaded control that is placed on a Panel which, in turn, is placed on an UpdatePanel.


    I will try not to place a huge mess of code here, but do want to provide you with SOMETHING. I have redacted it a bit for the sake of brevity.



    ::::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






    As it appears to me, when i set a breakpoint inside both "if" statements in BindGrid() the first "if" is often skipped implying that Panel1 does not contain my datagrid tmdg at that moment in time. This "if" is specifically skipped in that "first-click" that is ignored.



    Please let me know if you need any more information from me!
    I hope that one of you can figure out what i'm doing improperly!!
    Any and all comments appreciated..










    share|improve this question
























      1












      1








      1








      I am having an issue with my web forms datagrid responding to a click event. Allow me to explain:



      1. Upon the page being loaded for the first time, a dropdownlist is filled for the user to select an item.

      2. When the user selects an item in the dropdownlist, a datagrid (called tmdg) appears (2nd page load) with ButtonColumns in it. When the user selects a button in one of the ButtonColumns in the datagrid, the value of the button flips from false to true (or from true to false, depending on its starting value). In the Page_Load event, if Page.IsPostBack==true, i assign an event handler to the datagrid (tmdg) like so:
        tmdg.ItemCommand += Tmdg_ItemCommand;


      3. Tmdg_ItemCommand is the method that calls Save() which flips the datatable and ultimately the datagrid cell value.


      This all works for the VERY FIRST click in the datagrid.
      HOWEVER, for subsequent clicking of the datagrid, the button.DataTextField values only flip on the SECOND click of the grid. (essentially a "double click" instead of a single click). My goal is to flip the value of the cell in the ButtonColumn for every single click event.



      Please note: after the first click of the grid where the value flips succesfully, if I may click cell (5,6) where nothing happens, if then i click cell (7,2) I will get a flip of that cell (7,2). Likewise, I could just click (5,2) again where nothing happens, then select (5,2) again to get the flip. (this is what i mean by essentially a "double click")



      Other notes:



      1. I have tried assigning the event handler ALL OVER the application in mulitple spots (before Page_Load in OnInit of the page; or in the UpdatePanel's Panel_Init method; or regardless of if Page.IsPostBack in Page_Load; or after Page_Load)


      2. The datagrid is a dynamically loaded control that is placed on a Panel which, in turn, is placed on an UpdatePanel.


      I will try not to place a huge mess of code here, but do want to provide you with SOMETHING. I have redacted it a bit for the sake of brevity.



      ::::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






      As it appears to me, when i set a breakpoint inside both "if" statements in BindGrid() the first "if" is often skipped implying that Panel1 does not contain my datagrid tmdg at that moment in time. This "if" is specifically skipped in that "first-click" that is ignored.



      Please let me know if you need any more information from me!
      I hope that one of you can figure out what i'm doing improperly!!
      Any and all comments appreciated..










      share|improve this question














      I am having an issue with my web forms datagrid responding to a click event. Allow me to explain:



      1. Upon the page being loaded for the first time, a dropdownlist is filled for the user to select an item.

      2. When the user selects an item in the dropdownlist, a datagrid (called tmdg) appears (2nd page load) with ButtonColumns in it. When the user selects a button in one of the ButtonColumns in the datagrid, the value of the button flips from false to true (or from true to false, depending on its starting value). In the Page_Load event, if Page.IsPostBack==true, i assign an event handler to the datagrid (tmdg) like so:
        tmdg.ItemCommand += Tmdg_ItemCommand;


      3. Tmdg_ItemCommand is the method that calls Save() which flips the datatable and ultimately the datagrid cell value.


      This all works for the VERY FIRST click in the datagrid.
      HOWEVER, for subsequent clicking of the datagrid, the button.DataTextField values only flip on the SECOND click of the grid. (essentially a "double click" instead of a single click). My goal is to flip the value of the cell in the ButtonColumn for every single click event.



      Please note: after the first click of the grid where the value flips succesfully, if I may click cell (5,6) where nothing happens, if then i click cell (7,2) I will get a flip of that cell (7,2). Likewise, I could just click (5,2) again where nothing happens, then select (5,2) again to get the flip. (this is what i mean by essentially a "double click")



      Other notes:



      1. I have tried assigning the event handler ALL OVER the application in mulitple spots (before Page_Load in OnInit of the page; or in the UpdatePanel's Panel_Init method; or regardless of if Page.IsPostBack in Page_Load; or after Page_Load)


      2. The datagrid is a dynamically loaded control that is placed on a Panel which, in turn, is placed on an UpdatePanel.


      I will try not to place a huge mess of code here, but do want to provide you with SOMETHING. I have redacted it a bit for the sake of brevity.



      ::::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






      As it appears to me, when i set a breakpoint inside both "if" statements in BindGrid() the first "if" is often skipped implying that Panel1 does not contain my datagrid tmdg at that moment in time. This "if" is specifically skipped in that "first-click" that is ignored.



      Please let me know if you need any more information from me!
      I hope that one of you can figure out what i'm doing improperly!!
      Any and all comments appreciated..







      c# datagrid webforms itemcommand






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 19:47









      WalkerWalker

      63 bronze badges




      63 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Instead of instantiating the datagrid tmdg in Push.aspx.cs, I added it to Push.aspx and the click event fires every time. I'm not sure why this works in .aspx but doesn't in the .aspx.cs file.



          New code follows to explain...



          ::::Push.aspx:::: (datagrid added here)



          <%@ 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 removed here)



           //DataGrid tmdg = new DataGrid
          //
          // AutoGenerateColumns = false,
          // CssClass = "gvClass push"
          //;


          Thoughts on WHY appreciated.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345366%2fwhy-does-my-itemcommand-datagrid-event-only-fire-on-the-second-click-of-an-item%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Instead of instantiating the datagrid tmdg in Push.aspx.cs, I added it to Push.aspx and the click event fires every time. I'm not sure why this works in .aspx but doesn't in the .aspx.cs file.



            New code follows to explain...



            ::::Push.aspx:::: (datagrid added here)



            <%@ 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 removed here)



             //DataGrid tmdg = new DataGrid
            //
            // AutoGenerateColumns = false,
            // CssClass = "gvClass push"
            //;


            Thoughts on WHY appreciated.






            share|improve this answer



























              0














              Instead of instantiating the datagrid tmdg in Push.aspx.cs, I added it to Push.aspx and the click event fires every time. I'm not sure why this works in .aspx but doesn't in the .aspx.cs file.



              New code follows to explain...



              ::::Push.aspx:::: (datagrid added here)



              <%@ 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 removed here)



               //DataGrid tmdg = new DataGrid
              //
              // AutoGenerateColumns = false,
              // CssClass = "gvClass push"
              //;


              Thoughts on WHY appreciated.






              share|improve this answer

























                0












                0








                0







                Instead of instantiating the datagrid tmdg in Push.aspx.cs, I added it to Push.aspx and the click event fires every time. I'm not sure why this works in .aspx but doesn't in the .aspx.cs file.



                New code follows to explain...



                ::::Push.aspx:::: (datagrid added here)



                <%@ 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 removed here)



                 //DataGrid tmdg = new DataGrid
                //
                // AutoGenerateColumns = false,
                // CssClass = "gvClass push"
                //;


                Thoughts on WHY appreciated.






                share|improve this answer













                Instead of instantiating the datagrid tmdg in Push.aspx.cs, I added it to Push.aspx and the click event fires every time. I'm not sure why this works in .aspx but doesn't in the .aspx.cs file.



                New code follows to explain...



                ::::Push.aspx:::: (datagrid added here)



                <%@ 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 removed here)



                 //DataGrid tmdg = new DataGrid
                //
                // AutoGenerateColumns = false,
                // CssClass = "gvClass push"
                //;


                Thoughts on WHY appreciated.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 26 at 14:50









                WalkerWalker

                63 bronze badges




                63 bronze badges


















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345366%2fwhy-does-my-itemcommand-datagrid-event-only-fire-on-the-second-click-of-an-item%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript