How to display dynamic images from a Listview?

Asked

Viewed 539 times

-1

I created a div with a Listview where a Checkbox and a Imagery which are loaded into the Page_load.

I need that by clicking on one of the Checkbox by selecting from the image, it is created dynamically in another Div beside, being that at most the user can select 4 images from the list and after the choice of the images they are recorded the folder path so that again when the user enters the same screen the previously selected images are loaded.

Down with what I already have.

<asp:Panel runat="server" ID="panelObjeto">
    <div style="position: relative; height: 300px; width: 200px; overflow-x: scroll;">
        <asp:ListView ID="livImagens" runat="server" OnSelectedIndexChanging="livImagens_OnSelectedIndexChanged">
            <LayoutTemplate>
                <table>
                    <tr>
                        <th></th>
                    </tr>
                    <tr id="itemplaceholder" runat="server">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr style="border: solid 1px">
                    <td style="border: solid 1px">
                        <asp:CheckBox ID="cbxImagem" runat="server" />
                    </td>
                    <td>
                        <asp:Image ID="Image1" runat="server" Height="100px" Width="100px" ImageUrl='<%# Eval("Value") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>

    </div>
    <div id="divimagens" runat="server" style="position: absolute; left: 35%; top: 23%; height: 300px; width: 710px;">
        <p>Aqui será carregado as imagens</p> 
    </div>
</asp:Panel>

Code Behind.

if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Imagens/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            string fileName = Path.GetFileName(filePath);
            files.Add(new ListItem(fileName, "~/Imagens/" + fileName));
        }
        livImagens.DataSource = files;
        livImagens.DataBind();
    }

inserir a descrição da imagem aqui

  • Using Javascript is an option?

  • @Ciganomorrisonmendez yes, it is an option, I’m seeing if I can also calling a method through the Oncheckedchanged event

  • Why don’t you use jQuery to copy the HTML of the selected images and play these images as content of the div that represents the repeated images?

  • @Ulyssesalves I don’t know how to do it.

1 answer

2


You can do in the same way that loads the main list of images. In this block of code, you can leave so:

<div id="divimagens" runat="server" style="position: absolute; left: 35%; top: 23%; height: 300px; width: 710px;">
       <asp:ListView ID="livImagensSelecionadas" runat="server">
            <LayoutTemplate>
                <table>
                    <tr>
                        <th></th>
                    </tr>
                    <tr id="itemplaceholder" runat="server">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr style="border: solid 1px">
                    <td>
                        <asp:Image ID="Image2" runat="server" Height="100px" Width="100px" ImageUrl='<%# Eval("Value") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
</div>

and in the Behind:

List<ListItem> imgSelecionadas = new List<ListItem>();

protected void livImagens_OnSelectedIndexChanged(object sender, EventArgs e)
{
   string imageUrl = ((sender as Control).FindControl("Image1") as Image).ImageUrl;
   imgSelecionadas.Add(new ListItem(imageUrl.replace("~/Imagens/",""), imageUrl));
   livImagensSelecionadas.DataSource = imgSelecionadas;
   livImagensSelecionadas.DataBind();
}

Something along those lines.

  • how could I upload the images after saved in the bank ?

  • After saved in the database, it depends on how you saved the image. If you saved the image as an IMAGE or VARBINARY field, take a careful look at this link, it will help you: http://www.dotnetcurry.com/ShowArticle.aspx?ID=175 Here’s how to upload the images too, just adapt them for you

  • You can save the image as a Base64 string as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.