传递动态生成链接的文件位置

时间:2012-06-21 04:16:11

标签: c# asp.net datagridview

使用ASP.net和c# 使用visual studio 2010

大家好,我希望有人可以帮助我。基本上我有一个页面,其网格视图在其中一列中有一个链接按钮。我想做的是;用户单击该链接,其引用的PDF文件将使用site.master加载到新页面中。以下是我目前的代码。

首页

SelectCommand="SELECT * FROM [Guides] WHERE (([Display] = ?) AND ([Media_Document] = ?))">

<SelectParameters>
    <asp:QueryStringParameter DefaultValue="true" Name="Display" 
    QueryStringField="checkbox" Type="Boolean" />
    <asp:QueryStringParameter DefaultValue="Document" Name="Media_Document" 
    QueryStringField="Media_Document" Type="String" />
</SelectParameters>

</asp:AccessDataSource>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
</asp:ObjectDataSource>


<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
DataKeyNames="ID"  CssClass="mGrid"  RowStyle-CssClass="pgr"  AlternatingRowStyle-CssClass="alt"
DataSourceID="AccessDataSource1" Width="225px" style="text-align:left" 
GridLines="None" >

<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>

<Columns>
    <asp:BoundField DataField="Guide" HeaderText="Guide" SortExpression="Guide" />
    <asp:HyperLinkField DataNavigateUrlFields="File_location" 
    DataNavigateUrlFormatString="Guides.aspx?File_location={0}" 
    Target="content" Text="Link" />
</Columns>

<RowStyle CssClass="pgr"></RowStyle>

</asp:GridView> 
</div>

此代码正确生成Guides.aspx?File_location=blahblahblah.pdf

链接

目标网页

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <object width="800" height="800" data="File_Location"></object>
</asp:Content>     

此剂量不起作用,因为无法识别变量File_location是有意义的。我可以在后面的代码中使用c#来获取位置,但是如何在页面中显示它?

任何想法?

代码背后

public partial class Guides : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string Location = Request.QueryString.ToString();

        File_location(Location);

    }


    public void File_location(string location)
    {

        string File_Location = location.Substring(location.IndexOf('=') + 1);
    }
}

1 个答案:

答案 0 :(得分:1)

一种方法是在名为File_Location的代码隐藏页面上有一个公共属性,然后你会像这样引用它:

<object width="800" height="800" data="<%=File_Location%>"></object>

在Page_load中设置值。

在代码隐藏中创建一个属性,如下所示:

public string File_location()
{
    get
    {
        string location = Request.QueryString;
        return = location.Substring(location.IndexOf('=') + 1);
    }
}
相关问题