Error while Button click event in Custom Control (Specified cast is not valid)

M

mangesh

hi
i have got two custom controls on my form.
i am getting error while i click on button of one of custom control .
If i have only one custom contol the code run correctly.
but if i have more than one control , on click event of button i got
the following error.
===============================********=======================
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not
valid.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,
Boolean fBeforeLoad) +633
System.Web.UI.Page.ProcessRequestMain() +616

============================****************===============================
i am posting full source code what i did.
1) Base class for Custom Control : (skinBase)
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SimpleControlSamples
{
/// <summary>
/// Summary description for skinBase.
/// </summary>
///

public abstract class skinBase:Control ,INamingContainer
{
string _skinFileName;
public skinBase()
{
//
// TODO: Add constructor logic here
//
}

protected override void CreateChildControls()
{
Control ctrl=Page.LoadControl (_skinFileName);
Controls.Add (ctrl);
InitializeSkin(ctrl);

}
protected abstract void InitializeSkin(Control skin);

protected Control GetControl(Control skin,string ControlID)
{
Control ctrlFindControl=skin.FindControl (ControlID );
if(ctrlFindControl==null)
throw new Exception ("Now found");
return ctrlFindControl;
}
public string SkinFileName
{
get
{
return _skinFileName;
}
set
{
_skinFileName=value;
}
}
override public ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}


}
}

===============================================================
2) first custom control class file : (Composition2)
using System;
using System.Web ;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SimpleControlSamples
{
/// <summary>
/// Summary description for Composition2.
/// </summary>
public class Composition2:Control, INamingContainer
{
public Composition2()
{
//
// TODO: Add constructor logic here
//
}
public int abc
{
get
{
this.EnsureChildControls();

return Int32.Parse (((TextBox)Controls[1]).Text);



//this.EnsureChildControls();
//return Int32.Parse(((TextBox)Controls[1]).Text);


}
set
{
this.EnsureChildControls();

((TextBox)Controls[1]).Text = value.ToString();


//this.EnsureChildControls();
//((TextBox)Controls[1]).Text = value.ToString();
}
}

protected override void CreateChildControls()
{

this.Controls.Add (new LiteralControl ("<h3>" + "Value: "));
TextBox box=new TextBox ();
box.Text ="0";
this.Controls.Add (box);
this.Controls.Add (new LiteralControl ("</h3>"));
this.Controls.Add (new LiteralControl ("</br>"));
Button btn1=new Button ();
btn1.Text ="Add";
btn1.Click += new EventHandler(this.addbtn_click);
this.Controls.Add (btn1);
this.Controls.Add(new LiteralControl ("|"));
Button btn2=new Button ();
btn2.Text ="Subtract";
btn2.Click +=new EventHandler (this.subbtn_click);
this.Controls.Add (btn2 );


}
private void addbtn_click(Object sender,EventArgs e)
{
this.abc ++;
}
private void subbtn_click(Object sender,EventArgs e)
{
this.abc --;
}




}
}
==================================================================
3)second custom control class file : (clsTopSongs)
using System;
using System.Web ;
using System.Web.UI;
using System.Web.UI.WebControls ;
using System.Web.UI.HtmlControls ;
using System.Configuration ;
using System.Data;
using System.Data.SqlClient ;


namespace SimpleControlSamples
{
/// <summary>
/// Summary description for clsTopSongs.
/// </summary>
public class clsTopSongs:skinBase
{
DataGrid dgTopSongs=new DataGrid ();
LinkButton lnkProfile=new LinkButton ();
Label lblTopFive=new Label ();
string _skinFileName="UCTopSongs.ascx";

public clsTopSongs():base()
{
//
// TODO: Add constructor logic here
//
if (SkinFileName ==null)
{
SkinFileName =_skinFileName;
}


}
public string Name
{
get
{
this.EnsureChildControls ();
return lnkProfile.Text.ToString ();
}
set
{
this.EnsureChildControls ();
lnkProfile.Text =value.ToString ();
}
}
protected override void InitializeSkin(Control skin)
{

dgTopSongs=(DataGrid )GetControl (skin,"dgTopSongs");
lnkProfile=(LinkButton)GetControl(skin,"lnkProfile");
lblTopFive=(Label )GetControl(skin,"lblTopFive");
this.lnkProfile.Click +=new EventHandler (this.lnkprofile_click);
Name ="ONe Number";
//Initialize();

}
private void lnkprofile_click(object Sender,EventArgs e)
{

}
private void Initialize()
{
string strConn=ConfigurationSettings.AppSettings["ConnectionString"].ToString
();
SqlConnection oConn=new SqlConnection (strConn);
oConn.Open ();
//drpSupport.Items.Clear ();
string strQuery="select Top_Title,Top_SongID,
Top_SongTitle,Top_SongArtist from topSongs where person_ID=1";
SqlCommand oCommand =new SqlCommand (strQuery,oConn);
SqlDataAdapter oDataAdapter=new SqlDataAdapter (oCommand);
DataTable oDTable=new DataTable ("TopSongs");
oDataAdapter.Fill (oDTable);
dgTopSongs.DataSource =oDTable;
dgTopSongs.DataBind ();
//lnkProfile.Text =oDTable.Rows[1]["Top_Title"].ToString ();
Name="TExt";

}

}
}
==========================================================
4) Second custom controls UC file.(UCtopSong.ascx)

<%@ Control%>
<table cellSpacing="0" cellPadding="0" width="200" bgColor="#3e3f3f"
border="0">
<tr>
<td align="right" height="30"><IMG src="Image/topfive.gif">
</td>
<td width="10">
</td>
</tr>
<tr>
<td align="right">
<asp:LinkButton ID="lnkProfile" Runat="server"
CssClass="a"></asp:LinkButton>
<asp:label id="lblTopFive" text="top five" Runat="server"
CssClass="text"></asp:label>
</td>
</tr>
<tr>
<td align="right"><span class="darkgrey">Songs # </span>
<br>
<span class="darkgrey">artist</span>
</td>
</tr>
<tr>
<td align="right">
<asp:datagrid id="dgTopSongs" Runat="server"
AutoGenerateColumns="False" Width="200px" GridLines="None">
<AlternatingItemStyle
CssClass="SiteBGColor"></AlternatingItemStyle>
<Columns>
<asp:TemplateColumn>
<ItemStyle CssClass="text"></ItemStyle>
<ItemTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr align="right">
<td align="right" class="text">
<asp:Label ID="lblSongTitle" Runat="server" text='<%#
DataBinder.Eval(Container.DataItem,"Top_SongTitle") %>'>
</asp:Label>
&nbsp;&nbsp;
<asp:Label ID="lblSongId" Runat ="server" text='<%#
DataBinder.Eval(Container.DataItem,"Top_SongID") %>' >
</asp:Label>
<br>
<asp:Label id="lblSongArtist" Runat="server" text='<%#
DataBinder.Eval(Container.DataItem,"Top_SongArtist") %>'>
</asp:Label>
&nbsp;&nbsp;
<asp:Label ID="Label1" Runat ="server" text='<%#
DataBinder.Eval(Container.DataItem,"Top_SongID") %>' Visible ="False">
</asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</td>
</tr>
</table>
=========================================================
5) the form where these two custom controls are included : (
WebForm2.aspx)
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs"
AutoEventWireup="false" Inherits="SimpleControlSamples.WebForm2" %>
<%@ Register TagPrefix="UC" Namespace="SimpleControlSamples"
Assembly="SimpleControlSamples" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<LINK id="stylesheet" href="StyleSheet/djforum.css"
rel="stylesheet">
<!--<link id="stylesheet1" href="StyleSheet/Default.css"
rel="stylesheet"> !-->
<script language="javascript">
/*function loadControl()
{
alert("mangesh");
window.open ("www.yahoo.com","_self");
}*/

</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm2" method="post" runat="server">
<br>
<UC:clsSupport id="mySupport" runat="server"></UC:clsSupport>
<br>
<br>
<br>
<table cellpadding="0" cellspacing="0" border="0" width="800">
<tr>
<td align="right">
<UC:clsTopSongs id="MyTopSongs" runat="server"></UC:clsTopSongs>
</td>
</tr>
<tr>
<td>
<UC:Composition2 id="MyComposition"
runat="server"></UC:Composition2>
</td>
</tr>
</table>
</form>
</body>
</HTML>
====================================================================
Please check this
thanks in advance
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top