LinkButton Casting error

Discussion in 'ASP .Net Web Controls' started by Jim McGivney, Dec 10, 2007.

  1. Jim McGivney

    Jim McGivney Guest

    I am trying to determine the row of a Button on a DataView an. I need to
    set the CommandArgument when the row is first created.
    I copied the following from Visual Studio 2005 documentation

    public void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
    {// The GridViewCommandEventArgs class does not contain a
    // property that indicates which row's command button was
    // clicked. To identify which row's button was clicked, use
    // the button's CommandArgument property by setting it to the
    // row's index.
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    // Retrieve the LinkButton control from the first column.
    LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
    //Button addButton = (Button)e.Row.Cells[0].Controls[0];
    // Set the LinkButton's CommandArgument property with the
    // row's index.
    addButton.CommandArgument = e.Row.RowIndex.ToString();
    }
    }

    When I run it I get the following message: Unable to cast object of type
    'System.Web.UI.LiteralControl' to type
    'System.Web.UI.WebControls.LinkButton'.

    This is an error for the line "LinkButton addButton =
    (LinkButton)e.Row.Cells[0].Controls[0];"

    Any suggestions would be welcome.

    Thanks,

    Jim
     
    Jim McGivney, Dec 10, 2007
    #1
    1. Advertising

  2. When controls are added to cells, there are often LiteralControls added as
    well. These LiteralControls are used for extra text that has no real meaning
    (for example, a space or tab used in the *.aspx file to indent lines). The
    best solution is to iterate through the controls and check the type of each
    one before casting. Hopefully this will help.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Jim McGivney" <> wrote in message
    news:O1yl$...
    >I am trying to determine the row of a Button on a DataView an. I need to
    >set the CommandArgument when the row is first created.
    > I copied the following from Visual Studio 2005 documentation
    >
    > public void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
    > {// The GridViewCommandEventArgs class does not contain a
    > // property that indicates which row's command button was
    > // clicked. To identify which row's button was clicked, use
    > // the button's CommandArgument property by setting it to the
    > // row's index.
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    > // Retrieve the LinkButton control from the first column.
    > LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
    > //Button addButton = (Button)e.Row.Cells[0].Controls[0];
    > // Set the LinkButton's CommandArgument property with the
    > // row's index.
    > addButton.CommandArgument = e.Row.RowIndex.ToString();
    > }
    > }
    >
    > When I run it I get the following message: Unable to cast object of type
    > 'System.Web.UI.LiteralControl' to type
    > 'System.Web.UI.WebControls.LinkButton'.
    >
    > This is an error for the line "LinkButton addButton =
    > (LinkButton)e.Row.Cells[0].Controls[0];"
    >
    > Any suggestions would be welcome.
    >
    > Thanks,
    >
    > Jim
    >
    >
    >
    >
    >
    >
     
    Nathan Sokalski, Dec 21, 2007
    #2
    1. Advertising

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. Sathyaish
    Replies:
    3
    Views:
    3,134
    Sathyaish
    Sep 8, 2005
  2. kevin
    Replies:
    11
    Views:
    5,817
    Andrew McDonagh
    Jan 8, 2005
  3. Wally Barnes
    Replies:
    3
    Views:
    534
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    578
    James Kanze
    Dec 20, 2009
  5. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    203
    Sathyaish
    Sep 8, 2005
Loading...

Share This Page