用户控件属性

时间:2012-11-16 16:14:35

标签: asp.net linq

我有一个带有以下标记的详细视图,此详细视图的数据源来自存储过程“spDocResult”,如下所示:

select DocId,TransId,FileId,Filename,ContentType,Data,DocumentNo,Title,TRANSMITTAL
from DocumentSum2
where (DocId=@Docid)AND(Transid=@Transid)

此详细视图的一个字段应显示Efile名称,因此我为该

创建了1个用户控件
public partial class FileTemp : System.Web.UI.UserControl
{
    private EDMSDataContext _DataContext;
private IEnumerable<tblFile> _Efiles;

public IEnumerable<tblFile> Efiles
{
    set { _Efiles = value; }
}
protected void Page_Load(object sender, EventArgs e)
{ 
}
    protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
         _DataContext = new EDMSDataContext();
        //you can get your command argument values as follows
        string FileId = e.CommandArgument.ToString();
       int _FileId = Convert.ToInt32(FileId);
       tblFile Efile = (from ef in _DataContext.tblFiles
                    where ef.FileId == _FileId
                    select ef).Single();
       if (Efile != null)
       {

           download(Efile);

       }}}

        private void download ( tblFile Efile)

{

Byte[] bytes = (Byte[])Efile.Data.ToArray();
Response.Clear();

Response.Buffer = true;

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = Efile.ContentType.ToString();

Response.AddHeader("content-disposition", "attachment;filename="

+ Efile.FileName.ToString());

Response.BinaryWrite(bytes);

Response.Flush();

Response.End();

}

public override void DataBind()
        {
            base.DataBind();
            GridViewEfile.DataSource = _Efiles;
            GridViewEfile.DataBind();

        }

}

现在我有问题,因为detailview的数据源来自存储过程,用户控件的属性来自tblFile,所以当我使用DetailsView1_DataBound时,我不知道如何获取用户控件属性。当我使用下面的代码时,我有错误

can not implicity convert type string to system.collection.generic.iEnumerable<tblfile>

我有这条线的错误

fileList.Efiles = docresult.Filename;

protected void DetailsView1_DataBound(object sender, EventArgs e)
{

    spDocResultResult docresult = (spDocResultResult)DetailsView1.DataItem;
    FileTemp fileList = (FileTemp)DetailsView1.FindControl("FileTemp1");

    fileList.Efiles = docresult.Filename;
    fileList.DataBind();
}

1 个答案:

答案 0 :(得分:1)

这可能根本不是数据绑定问题。从上下文中收集起来有点难,但是......

FileTemp fileList = (FileTemp)DetailsView1.FindControl("FileTemp1");    
fileList.Efiles = docresult.Filename;

fileList.Efiles是您只想为其分配文件名的项目列表?如果是这样,你可能只需要通过它们进行预告。

foreach (var file in fileList.Efiles)
{
    file.FileName = docresult.Filename;
}

此外,将此行添加到您的Efiles声明中以解决Get accessor错误。

public IEnumerable<tblFile> Efiles
{
    get { return _Efiles; } // <- here
    set { _Efiles = value; }
}