2013-09-01 88 views
0

我有一個帶有「附件列」的Telerik RadGrid。 使用此代碼,我將上傳的文件保存到特定的文件夾。RadGrid attachmentcolumn在上傳之前獲取文件的名稱

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (e.Item.IsInEditMode && e.Item is GridEditableItem) 
    { GridEditableItem item = e.Item as GridEditableItem; 
     (item["AttachmentColumn"].Controls[0] as RadUpload).TargetFolder = "attachmentsFI"; 
    } 

我想在保存文件以進行某些操作之前也獲取文件名。我怎樣才能得到文件名?

回答

1

請嘗試使用下面的代碼片段。

客戶端

JS

<telerik:RadCodeBlock ID="telerikodeBlock1" runat="server"> 
    <script type="text/javascript"> 
     function ClientFileSelected(sender, args) { 
      var filePath = sender.getFileInputs()[0].value; 
      var fileName = filePath.substring(filePath.lastIndexOf("\\") + 1); 
      //Access your file name here 
     } 
    </script> 
</telerik:RadCodeBlock> 

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" 
    OnItemDataBound="RadGrid1_ItemDataBound"> 
    <MasterTableView> 
     <Columns> 
      <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"> 
      </telerik:GridBoundColumn> 
      <telerik:GridAttachmentColumn UniqueName="AttchColumn"> 
      </telerik:GridAttachmentColumn> 
      <telerik:GridEditCommandColumn> 
      </telerik:GridEditCommandColumn> 
     </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

ASPX.CS

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) 
{ 
    dynamic data1 = new[] { 
      new { ID = 1, Name ="Name_1"}, 
      new { ID = 2, Name = "Name_2"}, 
      new { ID = 3, Name = "Name_3"}, 
      new { ID = 4, Name = "Name_4"}, 
      new { ID = 5, Name = "Name_5"} 
     }; 

    RadGrid1.DataSource = data1; 
} 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (e.Item.IsInEditMode && e.Item is GridEditableItem) 
    { 
     GridEditableItem item = e.Item as GridEditableItem; 
     RadUpload ru = item["AttchColumn"].Controls[0] as RadUpload; 
     ru.OnClientFileSelected = "ClientFileSelected"; 
    } 
} 

服務器端

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" 
    OnItemDataBound="RadGrid1_ItemDataBound" 
    onupdatecommand="RadGrid1_UpdateCommand"> 
    <MasterTableView> 
     <Columns> 
      <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"> 
      </telerik:GridBoundColumn> 
      <telerik:GridAttachmentColumn UniqueName="AttchColumn"> 
      </telerik:GridAttachmentColumn> 
      <telerik:GridEditCommandColumn> 
      </telerik:GridEditCommandColumn> 
     </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

ASPX.CS

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) 
{ 
    dynamic data1 = new[] { 
      new { ID = 1, Name ="Name_1"}, 
      new { ID = 2, Name = "Name_2"}, 
      new { ID = 3, Name = "Name_3"}, 
      new { ID = 4, Name = "Name_4"}, 
      new { ID = 5, Name = "Name_5"} 
     }; 

    RadGrid1.DataSource = data1; 
} 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (e.Item.IsInEditMode && e.Item is GridEditableItem) 
    { 
     GridEditableItem item = e.Item as GridEditableItem; 
     RadUpload ru = item["AttchColumn"].Controls[0] as RadUpload; 
     ru.TargetFolder = "TempFiles"; 
    } 
} 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) 
{ 
    GridEditableItem item = e.Item as GridEditableItem; 
    RadUpload ru = item["AttchColumn"].Controls[0] as RadUpload; 
    string strFileName = ru.UploadedFiles[0].FileName; 
    // Access file name here 
} 
+0

感謝您的建議。我們根據您提供的建議找到了解決方案。 – FeliceM

相關問題