Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2008, 09:11 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default error casting button in datagrid

this line of code gives me the error( CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Button'. An explicit conversion exists (are you missing a cast?))

Button deleteButton = e.Item.Cells[0].Controls[0];

I then change to cast :
 Button deleteButton = (Button)e.Item.Cells[0].Controls[0];

and get the following error:
Specified argument was out of the range of valid values.
Parameter name: index

this is a handler on a datagrid to delete files, here is the entire code:
 void articleList_ItemDataBound(object sender, DataGridItemEventArgs e) {
        // First, make sure we're NOT dealing with a Header or Footer row
        if (((e.Item.ItemType != ListItemType.Header)
                    && (e.Item.ItemType != ListItemType.Footer))) {
            // Now, reference the Button control that the Delete ButtonColumn
            // has been rendered to
            Button deleteButton = e.Item.Cells[0].Controls[0];
            // We can now add the onclick event handler
            deleteButton.Attributes["onclick"] = ("javascript:return " + ("confirm(\'Are you sure you want to delete the file "
                        + (DataBinder.Eval(e.Item.DataItem, "Name") + "?\')")));
        }
    }

any ideas?
__________________
Thank You
 
Old May 1st, 2008, 09:21 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Often you will have additional 'invisible' controls in a cell, usually just LiteralControl instances for padding.

Double check how many controls are in the e.Item.Cells[0].Controls.Count, and if its 3 - then pick the middle one (i.e. e.Item.Cells[0].Controls[1]).

Otherwise you'll have to loop through until e.Item.Cells[0].Controls[i] is Button is true.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 2nd, 2008, 08:04 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Also, do you have a pager active? You are not filtering out that item type. I find it easier to do something like this (please excuse incorrect syntax, doing this from memory):
Code:
switch(e.Item.ItemType){
  case ListItemType.Item:
  case ListItemType.AlternateItem:
    //do work here
    break;
}
Regarding getting at the control itself, I find it less problematic to use the ID of the control. So inside the item template you have a control:
Code:
    <asp:button runat="server" id="btnMyButton" />
Then in your codebehind:
Code:
    Button myButton = (Button)e.Item.FindControl("btnMyButton");
This will find the button anywhere in the item, regardless of cell. This greatly reduces bugs and maintenance issues if you want to move things around.

-Peter
peterlanoie.blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Button on a DataGrid Ric_H ADO.NET 1 March 23rd, 2006 06:33 PM
Select Button on a DataGrid Ric_H C# 0 March 22nd, 2006 04:14 PM
Button in Datagrid rodmcleay C# 1 September 25th, 2005 06:13 AM
Access cell in DataGrid - wrong casting? drasko ASP.NET 1.0 and 1.1 Basics 4 December 22nd, 2003 04:45 AM
button on a datagrid narbones ASP.NET 1.x and 2.0 Application Design 0 June 17th, 2003 07:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.