1个回答
2013-11-09
展开全部
可以直接用DataGrid控件与数据库绑定,数据库中存图片的绝对或者相对路径。
我这边也有一个图片绑定上传和删除的代码,你可以看一下,控件自己拖。
public partial class _Default : System.Web.UI.Page
{
private void setBind() {
DirectoryInfo info = new DirectoryInfo(Server.MapPath("images"));
this.DataListpic.DataSource = info.GetFiles();
this.DataListpic.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
setBind();
}
}
protected void btn_delete_Click(object sender, EventArgs e)
{
foreach (DataListItem it in this.DataListpic.Items)
{
CheckBox cb = it.FindControl("cbx_delete") as CheckBox;
if (cb != null) {
if (cb.Checked) {
File.Delete(Server.MapPath("images/")+cb.Text);
}
}
}
setBind();
}
protected void btn_upload_Click(object sender, EventArgs e) {
if (this.FileUpload1.HasFile)
{
string name = this.FileUpload1.FileName;
this.FileUpload1.SaveAs(Server.MapPath("images/" + name));
Response.Write("上传成功!!");
setBind();
}
else
{
Response.Write("没有选中文件!!");
}
}
protected void DataListpic_SelectedIndexChanged(object sender, EventArgs e)
{
setBind();
}
}
我这边也有一个图片绑定上传和删除的代码,你可以看一下,控件自己拖。
public partial class _Default : System.Web.UI.Page
{
private void setBind() {
DirectoryInfo info = new DirectoryInfo(Server.MapPath("images"));
this.DataListpic.DataSource = info.GetFiles();
this.DataListpic.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
setBind();
}
}
protected void btn_delete_Click(object sender, EventArgs e)
{
foreach (DataListItem it in this.DataListpic.Items)
{
CheckBox cb = it.FindControl("cbx_delete") as CheckBox;
if (cb != null) {
if (cb.Checked) {
File.Delete(Server.MapPath("images/")+cb.Text);
}
}
}
setBind();
}
protected void btn_upload_Click(object sender, EventArgs e) {
if (this.FileUpload1.HasFile)
{
string name = this.FileUpload1.FileName;
this.FileUpload1.SaveAs(Server.MapPath("images/" + name));
Response.Write("上传成功!!");
setBind();
}
else
{
Response.Write("没有选中文件!!");
}
}
protected void DataListpic_SelectedIndexChanged(object sender, EventArgs e)
{
setBind();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询