【问题标题】:Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.Label'无法将类型“字符串”隐式转换为“System.Web.UI.WebControls.Label”
【发布时间】:2014-05-15 19:00:51
【问题描述】:

我将我的值从 gridview 按行传递到不同的页面。数据库中 gridview 的表如下:

 Create table Task
  (
    TaskId int Identity(1,1),
     Title varchar(100),
     Body varchar(500),
     Reward decimal(4,2),
    TimeAllotted int,
    PosterName varchar(100)
  )

详情页面的代码如下:

 protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
        GridViewRow row = GridView1.Rows[rowIndex];
        lblTaskId.Text = row.Cells[0].Text;
        lblTitle.Text = row.Cells[1].Text;
        lblBody.Text = row.Cells[2].Text;
        lblReward.Text = row.Cells[3].Text;
        lblTimeAllotted = row.Cells[4].Text;
        lblPosterName = row.Cells[5].Text;


    }
}

它会根据需要显示所有内容,但是当我在 gridview 的特定行中单击“查看任务”时,出现异常无法将类型“字符串”隐式转换为“System.Web.UI.WebControls.Label”。异常发生在最后两行,即

    lblTimeAllotted = row.Cells[4].Text;
    lblPosterName = row.Cells[5].Text;

我该如何纠正这个问题?

【问题讨论】:

    标签: c# asp.net sql-server gridview


    【解决方案1】:

    试试这个:

    lblTimeAllotted.text = row.Cells[4].Text;
    lblPosterName.text = row.Cells[5].Text;
    

    您不能为文本设置标签。你需要设置标签的text属性

    【讨论】:

      【解决方案2】:

      您不能将string 设置为Label。您必须将值设置为两个标签的属性Text

      lblTimeAllotted.Text = row.Cells[4].Text;
      lblPosterName.Text = row.Cells[5].Text;
      

      【讨论】:

        【解决方案3】:

        在您的代码中,您有:

            lblTaskId.Text = row.Cells[0].Text;
            lblTitle.Text = row.Cells[1].Text;
            lblBody.Text = row.Cells[2].Text;
            lblReward.Text = row.Cells[3].Text;
        

        后跟:

        lblTimeAllotted = row.Cells[4].Text;
        lblPosterName = row.Cells[5].Text;
        

        我猜你的实际意思是:

        lblTimeAllotted.Text = row.Cells[4].Text;
        lblPosterName.Text = row.Cells[5].Text;
        

        您看到异常的原因是 .NET 试图将您的标签对象更改为单元格的字符串值,这显然是荒谬的。看起来像一个简单的错字:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-09
          • 2014-02-04
          • 2011-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多