无法转换'system.web.ui.webcontrols.imagebutton'类型的对象来键入'system.web.ui.webcontrols.button'

时间:2018-01-24 17:20:12

标签: c# asp.net

运行gridViewTCOR10.DataBind();时抛出异常:

Unable to cast objects of type system.web.ui.webcontrols.imagebutton 'on the type' system.web.ui.webcontrols.button '.

如果我使用<asp: Button>代替<asp: ImageButton>则可以使用

private void loadTCOR10()
{
    try
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        //int i = 0;
        string sql = null;
        gridViewTCOR10.Visible = true;
        table1.Visible = false;
        string connectionString = SestanteWeb.Global.rCRVigServer.leggiStringaConnessioneSicurezzaSqlClient();
        sql = "select * FROM [PUMA2_FINANZIARIAFAMILIARE].[dbo].[TCOR10]";
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        adapter.Fill(ds);
        adapter.Dispose();
        command.Dispose();
        connection.Close();
        // gridView1.DataSource = ds.Tables[0];
        gridViewTCOR10.DataSource = ds;
        gridViewTCOR10.DataBind(); //here throw Exception Unable to cast o  bjects of type 
                                   //'system.web.ui.webcontrols.imagebutton' on the type 'system.web.ui.webcontrols.button'.

    }
    catch (Exception ex)
    {


    }
    finally
    {
        //adapter.Dispose();
        //command.Dispose();
        //connection.Close();

    }
}



en<asp:TemplateField>
                    <EditItemTemplate>
                        <asp:ImageButton ID="ButtonUpdate" runat="server" CommandName="Update" Text="Edit" ImageUrl="~/Images/Cancella.gif" />
                         <asp:ImageButton ID="ButtonCancel"  runat="server" ImageUrl="~/Images/Cancella.gif" CommandName="Cancel" Text="Delete" />
                        <%--<asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" />
                        <asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" />--%>
                    </EditItemTemplate>
                    <ItemTemplate>
                         <%--<asp:Button ID="ButtonEdit" runat="server" CommandName="Edit" Text="Edit" />--%>

                         <asp:ImageButton ID="ButtonEdit" runat="server" CommandName="Edit" Text="Edit" ImageUrl="~/Images/Cancella.gif"/>
                         <asp:ImageButton ID="ButtonDelete"  runat="server" ImageUrl="~/Images/Cancella.gif" CommandName="Delete" Text="Delete" />

                        <%--<asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete"  />--%>

                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:ImageButton ID="ButtonAdd" runat="server" ImageUrl="~/Images/Cancella.gif" CommandName="AddNew" Text="Add New Row" ValidationGroup="validaiton" />
                        <%--<asp:Button ID="ButtonAdd" runat="server" CommandName="AddNew" Text="Add New Row" ValidationGroup="validaiton" />--%>
                    </FooterTemplate>
                </asp:TemplateField>

0 个答案:

没有答案