Error: Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.ButtonField'

Hello,

I am getting the following error during compiliation. Any suggestions to fix it. Thanks.

Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.ButtonField'

protected void gvEvents_RowCommand(Object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "Select")

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow selectedRow = gvEvents.Rows[index];

string sID;

ButtonField btn;

if (selectedRow.RowType == DataControlRowType.DataRow)

{

sID = ((ButtonField)selectedRow.FindControl("ID")).DataTextField;

}

GlobalVar.gMainEventID = Convert.ToInt32(sID);

Session["EventID"] = Convert.ToInt32(sID);

Response.Redirect("home.aspx");

}

}

Thank you.

P.S. Why is there a double line-spacing when entering text in this message field?

 

0
rajanam
11/30/2007 8:38:14 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

4 Replies
1246 Views

Similar Articles

[PageSpeed] 51

 Whats the aspx u have?

 


Thanks,
Karan

~ Remember To Mark The Post(s) That Helped You As The ANSWER ~
0
karan
11/30/2007 8:58:05 PM

sID = ((ButtonField)selectedRow.FindControl("ID")).DataTextField;

What ID are you really putting here, and are you sure it's a ButtonField? 


Please Mark As Answer posts that helped you.

"If we learn from our mistakes, I should be brilliant by now."

0
nikki_doer_of_things
11/30/2007 9:00:34 PM

Thank you both for your reply. 

Here is the aspx part of the code -  

 <asp:GridView ID="gvEvents" runat="server" SelectedIndex="0" SelectedRowStyle-BackColor="whiteSmoke"
DataKeyNames="ID" OnRowCommand="gvEvents_RowCommand"
OnSorting="gvEvents_Sort" OnPageIndexChanging="gvEvents_PageIndexChanging" AutoGenerateColumns="false"
AllowPaging="true" AllowSorting="true" >
<Columns>
<asp:ButtonField ButtonType="Link" CommandName="Select" DataTextField="ID" HeaderText="Event ID"
SortExpression="ID" />
<asp:ButtonField ButtonType="Link" CommandName="Select" DataTextField="Name" HeaderText="Event Name"
SortExpression="Name" />
</Columns>
</asp:GridView>

0
rajanam
11/30/2007 10:17:55 PM

I just realized that the BoundField does not have ID attribute and so using FindControl will not help.

Instead I am now using LinkButton within a TemplateField (although it would be nice to know how to be able to do it with BoundField).

Thanks. 

 

 

 

0
rajanam
11/30/2007 11:38:17 PM
Reply:

Similar Artilces:

ASPNET2 HELP! CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.FileUpload'.
Server Error in '/start-movieposters' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.FileUpload'. An explicit conversion exists (are you missing a cast?) Source Error: ...

Error Serializing Value 'System.Web.UI.WebCOntrols.TreeNode' of type 'System.Web.UI.WebCOntrols.TreeNode'
Hi, I am  getting the error stated in the subject when i am storing a TreeNode in a ViewState and then using it as follows if (ViewState["ProductCounty"]!=null) { TreeNode _productCountyNode = DataTree.FindNode(((TreeNode)ViewState["ProductCounty"]).ValuePath); _productCountyNode.Expand(); } what could be causing this error? Hi, I believe TreeNode isn't serializable in a way its required for objects which can be put to ViewState - as reference - (TreeNode implements IStateManager and is meant to work together with TreeView itself saving it...

Unable to cast object of type 'System.Web.UI.WebControls.FormView' to type 'System.Web.UI.WebControls.DropDownList'.
Hi,  I have a formview with a dropdownlist created dynamically. i want to determine whether the dropdownlist was clicked. the code is written in the FormView1_DataBound method. I get the following error in the line below that is in italics and underlined: Unable to cast object of type 'System.Web.UI.WebControls.FormView' to type 'System.Web.UI.WebControls.DropDownList'.   Here is my code: ddl_iFullname = New DropDownList ddl_iFullname.ID = "ddl_iFullname" ddl_iFullname.DataSource = ObjectDataSource3 ddl_iFullname.DataTextField = "fullname" ddl_iFullname.DataValueField = "...

Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox'
I am attempting to "Update" a field in a Datagrid, but no matter what I do (Using either Label or Text Boxes)  I keep getting the same error: Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox' The Error is highlighted below in the Sub... <==THE FIELD THAT CALLS THE ERROR  Any help would be greatly appreciated !!! Thanks !!   The code is as follows... <table border=0 height=210 bordercolor=olive ><tr valign=top><td> <asp:datagrid id="FacilityDataGrid"...

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.
Hi, any idea why I'm getting the following error?Line 45: protected void CartGrid_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e) Line 46: { Line 47: TextBox QuantityTextBox = (TextBox)CartGrid.Rows[e.RowIndex].Cells[2].Controls[0]; Line 48: int Quantity = Convert.ToInt32(QuantityTextBox.Text); Line 49: Are you sure that the control at CartGrid.Rows[e.RowIndex].Cells[2].Controls[0] isn't a LiteralControl?  How are you creating code this in your code?Brent JenkinsASP.NET, C#, Web Developerwww.valewebdesign.co.uk  ...

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'
Hello, Goal: enter a number in two fields HoursPerUnit and PeoplePerUnit and the value for the third field, TotalHoursPerUnit will populate after the the number in the two fields have been caculated.  Using the code below I receive the error message above.  Please tell me what am I doing wrong? Do I need to cast the text as a literal?  Please help? Thank you in advance, <%@ Page Language="VB" MasterPageFile="Default.master" AutoEventWireup="false" CodeFile="frmHR.aspx.vb" Inherits="frmHR"...

Value of type 'System.Web.UI.Control' cannot be converted to 'System.Web.SiteMap'.
 Hello,         I receive this error "System.Web.UI.Control' cannot be converted to 'System.Web.SiteMap" when I try to find the Sitemap control.  ...            <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />            <asp:ListView ID="ListView1" runat="server">            <LayoutTemplate>    ...

Error 1 The type 'System.Web.UI.WebControls.LinqDataSource' exists in both 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll' and 'c:\WINDOWS\asse
Once I build project. I encounter this error. Does someone experience this error?Error    1    The type 'System.Web.UI.WebControls.LinqDataSource' exists in both 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll' and 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.6.0.0__31bf3856ad364e35\System.Web.Extensions.dll'    D:\Source Code\Linq_Lesson5\Default.aspx    1    1    D:\Source Code\Linq_Lesson5\  It appears tha...

The base class includes the field 'MyList', but its type (System.Web.UI.WebControls.DataList) is not compatible with the type of control (System.Web.UI.WebControls.DataGrid).
Hi guys,I am very new to asp.net and have been playing with the commerce starter kit at - http://asp.net/CommerceStarterKit/ one of the first pages that lists the catalogue items "ProductsList.aspx" uses a datalist, I need to use a datagrid as I need to use pagination as my catalogue is so huge, I have changed the necessary attributes in the ProductsList.aspx page to datagrid and also in the codebehind file ProductsList.aspx.vb, when I run the page I get the Error message below. Description: An error occurred during the parsing of a resource required to ser...

The base class includes the field 'MyList', but its type (System.Web.UI.WebControls.DataList) is not compatible with the type of control (System.Web.UI.WebControls.DataGrid).
Hi guys,I am very new to asp.net and have been playing with the commerce starter kit at - http://asp.net/CommerceStarterKit/ one of the first pages that lists the catalogue items "ProductsList.aspx" uses a datalist, I need to use a datagrid as I need to use pagination as my catalogue is so huge, I have changed the necessary attributes in the ProductsList.aspx page to datagrid and also in the codebehind file ProductsList.aspx.vb, when I run the page I get the Error message below. Description: An error occurred during the parsing of a resource required to ser...

Error showing up: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.
Hi, I am trying to pass date values from two textboxes in a stored procedure from ASP.net page. However, it seems it is not passing the date properly. I am getting the following error at the following line: cmd_r.Parameters("@initial_est_date").Value = Convert.ToDateTime(Textbox1)  and the error is as stated in the title. I would appreciate any help to resolve this. Thanks  CODE:<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %><%@ Import Namespace="System.Data.Common" %> <%@ I...

BC30311: Value of type 'Control' cannot be converted to 'System.Web.UI.WebControls.CheckBox'
 Hi, can somebody help me on this problem. It works at first, but all of a sudden, it fails, and I can't find out why.    1 Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) 2 3 If Not CheckRequiredField() Then 4 MessageBox("Please complete the input data!") 5 Exit Sub 6 End If 7 8 Dim nRow As Integer 9 Dim Ctrl As Control 10 Dim chkDelete As Object 11 Dim LookupID, Name, Desc, Type As String 12 13 nRow = 0 14 1...

Error
Hi Friends, I am trying to limit the width of ImageButton control not to exceed 120 pixels. But neither of the code snippets below work. Any suggestions? // ------------------------ int iWidth = 120; if (this.ImageButton1.Width > Unit.Parse(iWidth.ToString())) { this.ImageButton1.Width = 120; } // ------------------------ if (this.ImageButton1.Width > 120) { this.ImageButton1.Width = 120; } // ------------------------ Thank you in advance for your feedback. Rajan this.ImageButton1.Width is of datatype "unit" where a...

Error 17 Argument '2': cannot convert from 'System.Web.UI.WebControls.FontSize' to 'float'
Error 17 Argument '2': cannot convert from 'System.Web.UI.WebControls.FontSize' to 'float' Error 16 The best overloaded method match for 'System.Drawing.Font.Font(string, float, System.Drawing.FontStyle)' has some invalid arguments Code:chart.DrawString(Convert.ToString(barValue[i]), new Font("arial", FontSize.Small, FontStyle.Regular), TEXT_COLOR, (columnSpacing + CHART_LEFT_MARGIN), (BMP_HEIGHT - (CHART_BOTTOM_MARGIN - (currentBarHeight - BAR_LABEL_SPACE)))); Any solution? I would be grateful for any help. new Font("...

Web resources about - Error: Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.ButtonField' - asp.net.getting-started

Resources last updated: 12/30/2015 10:48:25 AM