C#asp.net图库Querystring

时间:2013-08-17 19:01:14

标签: c# asp.net

现在我正在使用这个代码正在工作但是一旦Image.aspx与404中没有工作的图像不在同一个目录中。我想普遍使用image.aspx用于所有图像如果可能的话,将它保存在顶层目录中。

Listview页面:

   <div class="datapager">
   <asp:DataPager ID="DataPager1" runat="server" PageSize="20"  
   PagedControlID="Listview1" QueryStringField="PageID">
        <Fields>
            <asp:NextPreviousPagerField ButtonType="Button"

   ShowFirstPageButton="False" 
                ShowNextPageButton="False" ShowPreviousPageButton="True"   
   ShowLastPageButton="False" ButtonCssClass="PagerField" PreviousPageText="&#171;
   Previous" />
            <asp:NumericPagerField CurrentPageLabelCssClass="NumericPagerField" 
   ButtonCount="10" NextPageText="More" PreviousPageText="More" />
            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="False" 
                ShowNextPageButton="True" ShowPreviousPageButton="False" 
   ShowFirstPageButton="False" ButtonCssClass="PagerField" NextPageText="Next&#187;"/>
        </Fields>
    </asp:DataPager>    
    </div>
    <div class="topadcell">Amazon Ad</div>
    <asp:listview ID="Listview1" runat="server" 
        onpagepropertieschanging="Listview1_PagePropertiesChanging" >
        <ItemTemplate>
        <li>
        <asp:HyperLink ID="lnkImage" Tooltip='<%# Eval("Name") %>' runat="server" 
    NavigateUrl='<%# Eval("Name","~/Image.aspx?Name=/Images/yeah/a/directory/{0}")%>' >
        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Name", "~/Images/yeah/a 
        /directory/{0}") %> ' alt='<%# Eval("Name") %>' 
    Width="125" CssClass="picborder">
        </asp:Image></asp:HyperLink></li>
        </ItemTemplate>
        <LayoutTemplate>
      <ul ID="itemPlaceholderContainer" runat="server" class="gallery" >
        <li id="itemPlaceholder" runat="server" ></li>
      </ul>           
     </LayoutTemplate>
    </asp:listview>
         <div class="datapager">
     <asp:DataPager ID="DataPager2" runat="server" PageSize="20"  
     PagedControlID="Listview1" QueryStringField="PageID">
        <Fields>
            <asp:NextPreviousPagerField ButtonType="Button" 
     ShowFirstPageButton="False" 
                ShowNextPageButton="False" ShowPreviousPageButton="True" 
     ShowLastPageButton="False" ButtonCssClass="PagerField" PreviousPageText="&#171;
     Previous"/>
            <asp:NumericPagerField CurrentPageLabelCssClass="NumericPagerField" 
     ButtonCount="10" NextPageText="More" PreviousPageText="More" />
            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="False" 
                ShowNextPageButton="True" ShowPreviousPageButton="False"  
     ShowFirstPageButton="False" ButtonCssClass="PagerField" NextPageText="Next&#187;"/>
        </Fields>
    </asp:DataPager>

.cs页面是:

        private void BindListImages()
{
    int i = 0;
    DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images/yeah/a/directory/"));
    FileInfo[] file = dir.GetFiles();
    System.Collections.ArrayList list = new System.Collections.ArrayList();
    foreach (FileInfo file2 in file)
    {
        if (i <= 6000)
        {
            if (file2.Extension == ".jpg" || file2.Extension == ".jpeg" || 
file2.Extension == ".JPG" || file2.Extension == ".gif")
            {
                list.Add(file2);
                i++;
            }
        }

    }
    Listview1.DataSource = list;
    Listview1.DataBind();

}

public class Person
{
    public string Name { get; set; }
}
    protected void Listview1_PagePropertiesChanging(object sender, 
PagePropertiesChangingEventArgs e)
{

    DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    BindListImages();


}

    protected void Page_Init(object sender, EventArgs e)
    {

        BindListImages();
    }

image.aspx页面是:

        <span class="imagewarpper">
<img id="fullsize" src='<%# Request.QueryString["Name"]%>'   
alt='<%=Request.QueryString["Name"] %>' title="<%= Request.QueryString["Name"] %>" 
class="picture"/>
</span>

2 个答案:

答案 0 :(得分:0)

快速而肮脏的解决方案是更改image.aspx文件,以使其绝对指向图像文件。

假设图像存储在 / resources / img 文件夹中;代码看起来像这样:

<span class="imagewarpper">
<img id="fullsize" src='/resources/img/<%# Request.QueryString["Name"]%>'   
alt='/resources/img/<%=Request.QueryString["Name"] %>'
title="<%= Request.QueryString["Name"] %>" class="picture"/>
</span>

请注意 src alt 参数如何收到一些与参数名称一起组合和呈现的其他代码。

有更好的方法可以实现这一点,但我认为这足以向您展示必要的更改。

答案 1 :(得分:0)

得知这一点,并认为我会把这个放在任何正在阅读这个并需要帮助的人身上。后面的代码保持不变我只在每页上更改了一些内容。

Listview页面:

        <ItemTemplate>
    <li>
    <asp:HyperLink ID="lnkImage" Tooltip='<%# Eval("Name") %>' runat="server" NavigateUrl='<%# Eval("Name", "~/Image.aspx?Image1=Images/yeah/a/directory/{0}&Name={0}") %>' >
    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Name", "~Images/yeah/a/directory/{0}") %> ' alt='<%# Eval("Name") %>' Width="125" CssClass="picborder">
    </asp:Image></asp:HyperLink></li>
    </ItemTemplate>

Image.aspx页面:

    <img id="fullsize" src='<%= Request.QueryString["Image1"]%>' alt='<%= Request.QueryString["Name"]%>' title='<%= Request.QueryString["Name"]%>' class="picture"/>