請嘗試使用下面的代碼片段。
客戶端
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
}
感謝您的建議。我們根據您提供的建議找到了解決方案。 – FeliceM