0

我在 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 "targetfile" 应该是:

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 以获取包含文件的文件夹的完整路径?

4

2 回答 2

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();
    }
  }
于 2013-09-10T08:40:22.870 回答
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 + "');");

    }
}
于 2013-09-10T11:19:17.617 回答