从ASP代码到javascript获取ASP页面中的文件路径

时间:2013-09-10 08:27:39

标签: c# javascript asp.net telerik

我在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并且工作正常。 我的问题是,出于各种原因,我现在需要创建&#34; Allegati&#34;的子文件夹。为每条记录单独存储文件。这些子文件夹以recordID值命名。 所以现在var&#34; targetfile&#34;应该是:

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();
    }
  }

如何传递子文件夹值&#34; recordID&#34;到asp页面中的javascript以获取包含文件的文件夹的完整路径?

2 个答案:

答案 0 :(得分:1)

在您的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();
    }
  }

答案 1 :(得分:1)

请尝试使用以下代码段。如果有任何疑虑,请告诉我。

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 + "');");

    }
}