DataGrid ButtonColumn/BoundColumn problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    DataGrid ButtonColumn/BoundColumn problem

    The ItemCommand event not getting fired when I add both a BoundColumn and a
    ButtonColumn to a datagrid. When I add a ButtonColumn by itself, everything
    works fine, but as soon as I add a BoundColumn, the ItemCommand event
    doesn't seem to get called.

    I've been struggling with this for too long now, I'm certain there's
    something I'm not getting. Here's the relevant code. Thanks in advance!

    // As is, this code does not correctly call the ItemCommand event handler.
    // Comment out AddIDColumn however and the ItemCommand event handler
    // is called when expected.
    private void Page_Load(objec t sender, System.EventArg s e) {
    AddIDColumn();
    AddButtonColumn ();
    dg.DataSource = GetDataSource() ;
    dg.DataKeyField = "employeeid ";
    if ( !IsPostBack )
    dg.DataBind();
    }

    private void AddIDColumn() {
    BoundColumn bound = new BoundColumn();
    bound.DataField = "EmployeeID ";
    bound.HeaderTex t = "EmployeeID ";
    dg.Columns.Add( bound );
    }

    private void AddButtonColumn () {
    ButtonColumn button = new ButtonColumn();
    button.ButtonTy pe = ButtonColumnTyp e.LinkButton;
    button.Text = "Click Me";
    button.CommandN ame = "whatever";
    dg.Columns.Add( button );
    }

    private void dg_ItemCommand( object source,
    System.Web.UI.W ebControls.Data GridCommandEven tArgs e) {
    dg.DataBind();
    }


  • Giorgio Parmeggiani

    #2
    Re: DataGrid ButtonColumn/BoundColumn problem

    Hi

    If you add some column at runtime, these have to be loaded at the
    initialization of the page (OnInit method) not in Page_load otherwise the
    page doesn't succeed to load the relative viewstate.

    If you move AddIDColumn(); and AddButtonColumn ();
    into the OnInit method your example it will work

    Ciao
    Giorgio

    "John" <fuzzy333@hotma il.com> ha scritto nel messaggio
    news:1Hhpb.884$ mB5.149626@news 20.bellglobal.c om...[color=blue]
    > The ItemCommand event not getting fired when I add both a BoundColumn and[/color]
    a[color=blue]
    > ButtonColumn to a datagrid. When I add a ButtonColumn by itself,[/color]
    everything[color=blue]
    > works fine, but as soon as I add a BoundColumn, the ItemCommand event
    > doesn't seem to get called.
    >
    > I've been struggling with this for too long now, I'm certain there's
    > something I'm not getting. Here's the relevant code. Thanks in advance!
    >[/color]


    Comment

    • John

      #3
      Re: DataGrid ButtonColumn/BoundColumn problem

      Thanks for your response, you're right this does work. But this brings upon
      more questions! Why do these need to be set in the Init exactly? Also, I
      want to allow the user to show or hide the ButtonColumn, so I store a flag
      in the ViewState to control this. I believe the ViewState is only loaded
      after Init is called. How can I decide to show or hide the ButtonColumn
      then? What's the solution to this problem?

      "Giorgio Parmeggiani" <gipasoft@gipad otnet.com> wrote in message
      news:eAtWc2aoDH A.2592@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi
      >
      > If you add some column at runtime, these have to be loaded at the
      > initialization of the page (OnInit method) not in Page_load otherwise the
      > page doesn't succeed to load the relative viewstate.
      >
      > If you move AddIDColumn(); and AddButtonColumn ();
      > into the OnInit method your example it will work
      >
      > Ciao
      > Giorgio
      >
      > "John" <fuzzy333@hotma il.com> ha scritto nel messaggio
      > news:1Hhpb.884$ mB5.149626@news 20.bellglobal.c om...[color=green]
      > > The ItemCommand event not getting fired when I add both a BoundColumn[/color][/color]
      and[color=blue]
      > a[color=green]
      > > ButtonColumn to a datagrid. When I add a ButtonColumn by itself,[/color]
      > everything[color=green]
      > > works fine, but as soon as I add a BoundColumn, the ItemCommand event
      > > doesn't seem to get called.
      > >
      > > I've been struggling with this for too long now, I'm certain there's
      > > something I'm not getting. Here's the relevant code. Thanks in[/color][/color]
      advance![color=blue][color=green]
      > >[/color]
      >
      >[/color]


      Comment

      • Giorgio Parmeggiani

        #4
        Re: DataGrid ButtonColumn/BoundColumn problem

        Hi
        [color=blue]
        > Why do these need to be set in the Init exactly? Also, I
        > want to allow the user to show or hide the ButtonColumn, so I store a flag
        > in the ViewState to control this. I believe the ViewState is only loaded
        > after Init is called. How can I decide to show or hide the ButtonColumn
        > then? What's the solution to this problem?[/color]

        Asp.NET has a well defined Control Life Cycle
        (http://www.15seconds.com/issue/020102.htm)
        so new controls added at runtime at the control collection must be added in
        the OnInit.
        but it is enough that is initialized in the OnInit! you can modify it in
        other methods (for example in the Page_Load)
        For your problem you can:
        1 - Add the datagrdi column on the OnInit
        2 - Hide the column in the Page_load event: datagrid.Column s[0].Visible =
        false.

        Ciao
        Giorgio


        Comment

        Working...