DataItem is NULL, problem

Discussion in 'ASP .Net' started by Jeff, Oct 13, 2008.

  1. Jeff

    Jeff Guest

    hi

    asp.net 2.0

    I have a GridView on a webpage. This GridView has a TemplateField which
    again contain a LinkButton. When I click on that LinkButton I want some
    action to take place on that row the LinkButton exists. When I click on the
    LinkButton the lbCar_Command method is triggered. But in this line the
    variable car gets a NULL value:
    CarDetail car = (CarDetail)row.DataItem;
    I thought DataItem existed for every row, as I'm using DataItem in the
    RowDataBound

    protected void gvwCars_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    CarDetail car = (CarDetail)e.Row.DataItem;
    LinkButton button = (LinkButton)e.Row.FindControl("lbCar");
    button.CommandArgument = e.Row.RowIndex.ToString();
    }
    }

    protected void lbCar_Command(object sender, CommandEventArgs e)
    {
    int rowIndex = Int32.Parse(e.CommandArgument.ToString());
    GridView grid = (GridView)LoginView1.FindControl("gvwCars");
    GridViewRow row = grid.Rows[rowIndex];
    CarDetail car = (CarDetail)row.DataItem;
    }

    (I don't want to create a separate field in the GridView with ButtonField
    instead, as it will change the GUI of this GridView entirely, this GridView
    have only one column but it's a TemplateField containing serveral controls
    and divs)

    Any suggestions?
     
    Jeff, Oct 13, 2008
    #1
    1. Advertisements

  2. Jeff

    Jeff Guest

    okay I've kind of solved this problem. I made a workaround the problem.
    instead of having rownumber stored in commandargument, I instead placed a
    primary key value. Anyway it's this id I need in that command...
     
    Jeff, Oct 13, 2008
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kel Good
    Replies:
    2
    Views:
    13,949
    Kel Good
    Apr 25, 2004
  2. Mirek Endys

    DataItem in DataListItem is null.

    Mirek Endys, Nov 21, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    3,008
    Henning Eiben
    Dec 15, 2005
  3. Replies:
    5
    Views:
    30,421
    Mike Schilling
    Mar 29, 2006
  4. =?Utf-8?B?QXJ0?=

    DataItem property = NULL during paging, why??!

    =?Utf-8?B?QXJ0?=, May 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,832
    =?Utf-8?B?QXJ0?=
    May 4, 2006
  5. Jeff

    newbie: e.Item.DataItem = null

    Jeff, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    5,068
    Karl Seguin [MVP]
    Jul 11, 2006
  6. Dave Adams

    Repeater e.item.dataitem null on postback

    Dave Adams, Oct 13, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    714
    Dave Adams
    Oct 13, 2003
  7. Ryan Cromwell

    DataGrid bound to Array... Postback DataItem is *always* null

    Ryan Cromwell, Nov 9, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    273
    Ryan Cromwell
    Nov 9, 2004
  8. sgh
    Replies:
    2
    Views:
    3,355
    Weston Weems
    Dec 1, 2004
Loading...