2014-04-03 202 views
1

我正在将我的值从gridview传递到不同的页面rowwise wise.The数据库中的gridview表如下所示:不能隐式地将类型'字符串'转换为'System.Web.UI.WebControls.Label'

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

代码-behind的详细信息页面,如下:

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; 

有人知道为什么吗?我怎样才能纠正这一点?请帮忙。

回答

3

试试这个:

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

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

+0

愚蠢的错误。非常感谢你指出。 – Mash

+0

它发生在每个人身上!乐意效劳! – logixologist

1

您不能将string设置为Label。你必须将值设置为两个标签的财产Text

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

在您的代码,您有:

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试图将标签对象改为单元格的字符串值,这显然是无意义的。看起来像一个简单的错字:)

相关问题