Показать сообщение отдельно
Старый 27.01.2009, 17:01   #8  
Alir is offline
Alir
Участник
 
47 / 14 (1) ++
Регистрация: 02.08.2008
Адрес: Санкт-Петербург
Для записи в примечание файла (берется из FileUpload), соотвествующего нужному объекту:
Цитата:
annotation newAnnotation = new annotation();

newAnnotation.objectid = new Lookup();
//брал guid объекта из адресной строки
newAnnotation.objectid.Value = new Guid(Request.Params["id"].ToString());

newAnnotation.filename = FileUpload1.FileName;
newAnnotation.documentbody = Convert.ToBase64String(FileUpload1.FileBytes);

EntityNameReference currentEntityReference = new EntityNameReference();
currentEntityReference.Value = EntityName.new_dogovor_documents.ToString();
newAnnotation.objecttypecode = currentEntityReference;

service.Create((BusinessEntity)newAnnotation);
Хранить записи я бы посоветовал в DataList'e с с LinkButton'ами с названиями документов, при клике на любой документ скачивается в нужную папку:
Цитата:
//узнаем на какую ссылку кликнули
LinkButton currentlnkButton = ((LinkButton)e.Item.Controls[3]);

//получаем коллекцию текущих примечаний (см. след. код)
BusinessEntity[] curcollectAtt = GetAnnotations();

for (int i = 0; i < curcollectAtt.Length; i++)
{
if (((annotation)curcollectAtt[i]).filename == currentlnkButton.Text)
{
//получаем содержимое файла примечания в кодировке Base64
byte[] fileBuffer = Convert.FromBase64String(((annotation)curcollectAtt[i]).documentbody);

//создаем новый файл с контентов полученного файла из примечания
FileStream filestream = new FileStream(ConfigurationSettings.AppSettings["path"].ToString() + ((annotation)curcollectAtt[i]).filename,
FileMode.Create, FileAccess.Write);
for (int j = 0; j < fileBuffer.Length; j++)
{
filestream.WriteByte(fileBuffer[j]);
}
filestream.Close();
break;
}
}
, где функция GetAnnotations:
Цитата:
ColumnSet colsAtt = new ColumnSet();
colsAtt.Attributes = new string[] { "filename", "documentbody" };

ConditionExpression condAtt = new ConditionExpression();
condAtt.AttributeName = "objectid";
condAtt.Operator = ConditionOperator.Equal;
condAtt.Values = new object[] { Request.Params["id"].ToString() };

OrderExpression orderAtt = new OrderExpression();
orderAtt.AttributeName = "createdon";
orderAtt.OrderType = OrderType.Descending;

FilterExpression filterAtt = new FilterExpression();
filterAtt.Conditions = new ConditionExpression[] { condAtt };

QueryExpression queryAtt = new QueryExpression();
queryAtt.EntityName = EntityName.annotation.ToString();
queryAtt.Criteria = filterAtt;
queryAtt.ColumnSet = colsAtt;
queryAtt.Orders = new OrderExpression[] { orderAtt };

BusinessEntity[] collectAtt = service.RetrieveMultiple(queryAtt).BusinessEntities;
Вот. Это основная работа.

Билд DataList'a происходит так:
Цитата:
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("TextValue", typeof(String)));

BusinessEntity[] curcollectAtt = GetAnnotations();

for (int i = 0; i < curcollectAtt.Length; i++)
{
dr = dt.NewRow();
string currentName = ((annotation)curcollectAtt[i]).filename;
dr[0] = currentName;
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
Далее эта функция используется как источник для DataList'a :
Цитата:
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
Описание DataList'a (в aspx):
Цитата:
<aspataList ID="ItemsList" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TextValue") %>'/>
</ItemTemplate>
</aspataList>
За это сообщение автора поблагодарили: bstan (1), Dicora (1).