0

我有以下代码在 datalist 中的服务器文件夹上显示图像。在cs中:

    if (!IsPostBack)
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("~/ajax _main/testpages/images/"));
        FileInfo[] files = dir.GetFiles();
        ArrayList list = new ArrayList();
        foreach (FileInfo oItem in files)
        {
            if (oItem.Extension == ".jpg" || oItem.Extension == ".jpeg" || oItem.Extension == ".gif")
                list.Add(oItem);
        }
        DataList1.DataSource = list;
        DataList1.DataBind();
    }

在 aspx 中:

<asp:DataList ID="DataList1" runat="server" RepeatColumns="10" CellPadding="5">
<ItemTemplate>
<asp:Image Width="20" Height="20" ID="Image1" ImageUrl='<%# Bind("Name", "~/ajax _main/testpages/images/{0}") %>' runat="server" />
<br />
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/ajax _main/testpages/images/{0}") %>' runat="server"/>
</ItemTemplate>
<ItemStyle BorderColor="silver" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>

我想在两个文件中动态地给出 url。我不能在 aspx 中做到这一点。

4

1 回答 1

0

您可以将更新面板与在修改文本框的值时调用的 JS 函数一起使用。首先在后面的代码中,还管理 AJAX 回发并修改绑定方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Bind("");
    }
    else
    {
        Bind(tb.Text);
    }
    SetFocus(tb);
}

private void Bind(string searchPattern)
{
    if (searchPattern == "")
        searchPattern = "*";
    DirectoryInfo dir = new DirectoryInfo(MapPath("~/ajax _main/testpages/images/"));
    FileInfo[] files = dir.GetFiles("*" + searchPattern + "*.*");
    ArrayList list = new ArrayList();
    foreach (FileInfo oItem in files)
    {
        if (oItem.Extension == ".jpg" || oItem.Extension == ".jpeg" || oItem.Extension == ".gif")
            list.Add(oItem);
    }
    DataList1.DataSource = list;
    DataList1.DataBind();
}

.aspx文件中,创建tbUpJS 函数,将内容包装在更新面板中,添加带有客户端事件处理程序OnKeyUp的文本框:OnFocus

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function tbUp() {
            __doPostBack('<%= up.UniqueID %>', '');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <div>
            <asp:UpdatePanel runat="server" ID="up">
                <ContentTemplate>
                    <asp:TextBox runat="server" ID="tb" 
                        OnKeyUp="javascript:tbUp();" 
                        OnFocus="this.value=this.value;">
                    </asp:TextBox>
                    <asp:DataList ID="DataList1" runat="server" 
                        RepeatColumns="10" CellPadding="5">
                        <ItemTemplate>
                            <asp:Image Width="20" Height="20" 
                                ID="Image1" 
                                ImageUrl='<%# Bind("Name", "~/ajax _main/testpages/images/{0}") %>' 
                                runat="server" />
                            <br />
                            <asp:HyperLink ID="HyperLink1" 
                                Text='<%# Bind("Name") %>' 
                                NavigateUrl='<%# Bind("Name", "~/ajax _main/testpages/images/{0}") %>' 
                                runat="server" />
                        </ItemTemplate>
                        <ItemStyle BorderColor="silver" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
                            VerticalAlign="Bottom" />
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
于 2013-05-27T10:05:15.083 回答