RadGrid attachmentcolumn在上传之前获取文件名

时间:2013-09-01 12:10:38

标签: c# telerik radgrid

我有一个带有“附件列”的Telerik RadGrid。 使用此代码,我将上传的文件保存到特定文件夹。

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 个答案:

答案 0 :(得分: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
}