我正在从数据库中检索我的toyName,并希望将其连接到下一页。我的会话没有错,但我有这个错误,如下所示,我不知道我哪里出错了。我的.aspx页面中有2个链接按钮,这个有错误的链接按钮是我创建的第二个linkButton。
错误讯息:
Unable to cast object of type 'ASP.Catalogue_aspx' to type 'System.Web.UI.WebControls.LinkButton'.
来源错误:
Line 13: protected void Page_Load(object sender, EventArgs e)
Line 14: {
Line 15: LinkButton LinkButton = (LinkButton)sender;
Line 16: int toyID = Convert.ToInt32(LinkButton.CommandName);
Line 17:
这是我的page_load .cs代码:
protected void Page_Load(object sender, EventArgs e)
{
LinkButton LinkButton = (LinkButton)sender;
int toyID = Convert.ToInt32(LinkButton.CommandName);
string strConnectionStr = ConfigurationManager.ConnectionStrings["ASPNETConnectionString"].ConnectionString;
string sql = "SELECT toyID, toyName FROM Toys WHERE toyID=@toyID";
SqlConnection myConnect = new SqlConnection(strConnectionStr);
myConnect.Open();
SqlCommand command = new SqlCommand(sql, myConnect);
command.Parameters.AddWithValue("@toyID", toyID);
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
Label1.Text = dr["toyName"].ToString();
}
dr.Close();
myConnect.Close();
}
答案 0 :(得分:1)
那是完全错误的。
sender
Page_Load
是页面,而不是LinkButton
。
答案 1 :(得分:0)
您无法将页面转换为链接按钮..这永远不会起作用。 当您在页面加载中转换发件人时,您在技术上正在转换页面
如果你想使用会话,你需要把
session["something"]= what ever you are trying to save
然后检索它
var myVariable = session["something"] (maybe cast this as something)
除此之外,我不确定你要做什么