Asp.Net - 使用Repeater创建图像

时间:2017-04-30 12:30:24

标签: asp.net repeater

我从数据库中获取用户关系图像并将它们放入像df.drop_duplicates(['name', 'id'], keep='first', inplace=True) df = df.groupby('name').sum().reset_index() 这样的类列表中,我只想在aspx页面中生成与列表项一样多的图像。 我可以使用有用的链接或代码吗?

1 个答案:

答案 0 :(得分:0)

试试这个例子:

.Aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ADD_Retrieve_IMAGES_repeater._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
       <table>
         <tr>
           <td>Name</td>
           <td>
              <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
              <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_name"
                ErrorMessage="Please enter the name">
              </asp:RequiredFieldValidator>
           </td>
         </tr>
         <tr>
           <td>Picture</td>
           <td>
             <asp:FileUpload ID="FileUpload1" runat="server" />
             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="FileUpload1"
                ErrorMessage="Please browse the image">
             </asp:RequiredFieldValidator>
           </td>
         </tr>
         <tr>
           <td></td>
           <td>
             <asp:Button ID="btn_insert" runat="server" OnClick="btn_insert_Click" Text="Insert" />
           </td>
         </tr>
       </table>
       <div>
         <asp:Repeater ID="Repeater1" runat="server">
           <HeaderTemplate>
              <center><b>Photo Details</b></center>
           </HeaderTemplate>
           <ItemTemplate>
             <table border="1">
               <tr>
                 <th>Name</th>
                 <th>Picture</th>
               </tr>
               <tr>
                 <td><%# Eval("name") %></td>
                 <td>
                   <img alt="" src='images/<%#Eval("pic") %>' height="50px" width="50px" />
                 </td>
               </tr>
             </table>
           </ItemTemplate>
         </asp:Repeater>
       </div>
    </form>

.CS代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

namespace ADD_Retrieve_IMAGES_repeater
{
public partial class _Default : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    string s1;
    string path;
    SqlConnection cnn = new SqlConnection();
    SqlCommand com = new SqlCommand();
    SqlDataAdapter sqlda;
    DataTable dt;
    protected void btn_insert_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.ContentLength > 0)
        {
            s1 = Path.GetFileName(FileUpload1.FileName);
            path = Server.MapPath("images") + "/" + s1;
            FileUpload1.SaveAs(path);
        }

        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        SqlCommand com = new SqlCommand("insert_test", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Connection = con;
        com.Parameters.AddWithValue("@name", txt_name.Text);
        com.Parameters.AddWithValue("@pic", s1);
        com.ExecuteNonQuery();
        com.Dispose();
        bindrepeater();
        con.Close();
        clear();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindrepeater();
        }
    }

    private void clear()
    {
        txt_name.Text = "";
    }
    private void bindrepeater()
    {
        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        sqlda = new SqlDataAdapter("SELECT * FROM test ", con);
        dt = new DataTable();
        sqlda.Fill(dt);
        sqlda.Dispose();
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
        con.Close();
    }
}
} 

链接1: http://www.c-sharpcorner.com/blogs/insert-and-retrieve-image-from-database-and-show-in-repeater1

链接2: https://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx

链接3 GridView http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html