0

我正在从数据库中检索我的 toyName 并希望将其会话到下一页。我的会话没有任何问题,但我有这个错误,如下所示,我不确定我哪里出错了。我的 .aspx 页面中有 2 个链接按钮,这个有错误的链接按钮是我创建的第二个链接按钮。

错误信息:

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();
}
4

2 回答 2

1

这是完全错误的。

senderfor是页面,而Page_Load不是LinkButton.

于 2012-07-30T14:57:46.277 回答
0

您不能将页面转换为链接按钮..这永远不会起作用。当您在页面加载中投射发件人时,您在技术上投射页面

如果你想使用会话,你需要把

session["something"]= what ever you are trying to save

然后检索它

var myVariable = session["something"] (maybe cast this as something)

除此之外,我不确定您要做什么

于 2012-07-30T15:07:29.613 回答