获取 ASP 页中从代码隐藏到 javascript 的文件路径
本文关键字:javascript 文件 路径 隐藏 代码 ASP 获取 | 更新日期: 2023-09-27 18:30:49
我在ASP页面中有一个radgrid和一个javascript。这是脚本:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function ViewCheck(filename) {
var targetfile = "Allegati/"+ filename;
var openWnd = radopen(targetfile, "RadWindowDetails");
}
</script>
上面的脚本应该将文件的路径传递给 Radwindow,并且工作正常。我的问题是,由于各种原因,我现在需要创建"Allegati"的子文件夹来分别存储每条记录的文件。此类子文件夹以 recordID 值命名。所以现在var"目标文件"应该是:
var targetfile = "Allegati/"+ recordID + filename;
在代码隐藏中,我得到的记录 ID 如下:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
string recordID = editedItem.GetDataKeyValue("TransazioneID").ToString();
}
}
如何将子文件夹值"recordID"传递给 asp 页面中的 javascript,以获取包含文件的文件夹的完整路径?
请尝试使用以下代码片段。如果有任何疑虑,请告诉我。
.JS
<script type="text/javascript">
function OPenPopuP(id, name, serverPath) {
var targetfile = serverPath + "/Allegati/" + id + "/" + name;
var openWnd = radopen(targetfile, "RadWindowDetails");
return false;
}
</script>
.ASPX
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView CommandItemDisplay="Top" DataKeyNames="ID,FileName">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<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",FileName = "jayesh-Softweb.jpg"},
new { ID = 2, Name = "Name_2",FileName = "jayesh-Softweb.jpg"},
new { ID = 3, Name = "Name_3",FileName = "jayesh-Softweb.jpg"},
new { ID = 4, Name = "Name_4",FileName = "jayesh-Softweb.jpg"},
new { ID = 5, Name = "Name_5",FileName = "jayesh-Softweb.jpg"}
};
RadGrid1.DataSource = data1;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
string strID = item.GetDataKeyValue("ID").ToString();
string strFileName = item.GetDataKeyValue("FileName").ToString();
Button Button1 = item.FindControl("Button1") as Button;
Button1.Attributes.Add("onclick", "return OPenPopuP('" + strID + "','" + strFileName + "','" + Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "');");
}
else if (e.Item.IsInEditMode && e.Item is GridEditableItem)
{
GridEditableItem item = e.Item as GridEditableItem;
string strID = item.GetDataKeyValue("ID").ToString();
string strFileName = item.GetDataKeyValue("FileName").ToString();
Button Button1 = item.FindControl("Button1") as Button;
Button1.Attributes.Add("onclick", "return OPenPopuP('" + strID + "','" + strFileName + "','" + Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "');");
}
}
向 aspx 页添加一个隐藏字段。
<asp:HiddenField ID="hfrecordID" runat="server" />
并在 ItemDataBound 事件中为其分配 recodId,并在 aspx 页中使用它。
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function ViewCheck(filename) {
var targetfile = "Allegati/" + <%= hfrecordID.value %> + filename;
var openWnd = radopen(targetfile, "RadWindowDetails");
}
</script>
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
hfrecordID= editedItem.GetDataKeyValue("TransazioneID").ToString();
}
}