对于一般的编码,我是一个相当新的人,如果我的一些行话有误,那就很抱歉。
我有一个数据库,用于存储我主页的文件图像名称,以便管理员可以上传新图像以替换数据库中的先前图像,以便将新上传的图像发布到主页。
我目前遇到的问题是,当我只是想更新其中一个图像时,它会上传该图像,但其他图像会被替换为空值,我只希望我上传图像的字段更新
这是我背后的代码,我很清楚我做错了什么,但是,我现在还不知道如何解决我的问题。任何帮助将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class admin_updatehomepage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void displayedit_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
info.Text = "Item Updated";
FileUpload fileupdate1 = displayedit.EditItem.FindControl("imageupdate1") as FileUpload;
FileUpload fileupdate2 = displayedit.EditItem.FindControl("imageupdate2") as FileUpload;
FileUpload fileupdate3 = displayedit.EditItem.FindControl("imageupdate3") as FileUpload;
FileUpload fileupdate4 = displayedit.EditItem.FindControl("imageupdate4") as FileUpload;
FileUpload fileupdate5 = displayedit.EditItem.FindControl("imageupdate5") as FileUpload;
FileUpload fileupdate6 = displayedit.EditItem.FindControl("imageupdate6") as FileUpload;
FileUpload fileupdate7 = displayedit.EditItem.FindControl("imageupdate7") as FileUpload;
FileUpload fileupdate8 = displayedit.EditItem.FindControl("imageupdate8") as FileUpload;
FileUpload fileupdate9 = displayedit.EditItem.FindControl("imageupdate9") as FileUpload;
Label recordid = displayedit.EditItem.FindControl("idlabel1") as Label;
Int32 id = Convert.ToInt32(recordid.Text);
if (fileupdate1.HasFile)
{
String fupload = fileupdate1.FileName;
Random r = new Random();
int rInt = r.Next(0, 10000);
String imgpath = "../images/" + rInt + fupload;
fileupdate1.SaveAs(Server.MapPath(imgpath));
String newimage = rInt + fupload;
string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(newsconnection);
//myConnection.ConnectionString is now set to connectionString.
myConnection.Open();
String query = "UPDATE homepage SET fballimg ='" + newimage + "' WHERE id='" + id + "'";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
if (fileupdate2.HasFile)
{
String fupload = fileupdate2.FileName;
Random r = new Random();
int rInt = r.Next(0, 10000);
String imgpath = "../images/" + rInt + fupload;
fileupdate2.SaveAs(Server.MapPath(imgpath));
String newimage = rInt + fupload;
string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(newsconnection);
//myConnection.ConnectionString is now set to connectionString.
myConnection.Open();
String query = "UPDATE homepage SET rugbyimg ='" + newimage + "' WHERE id='" + id + "'";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
if (fileupdate3.HasFile)
{
String fupload = fileupdate3.FileName;
Random r = new Random();
int rInt = r.Next(0, 10000);
String imgpath = "../images/" + rInt + fupload;
fileupdate3.SaveAs(Server.MapPath(imgpath));
String newimage = rInt + fupload;
string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(newsconnection);
//myConnection.ConnectionString is now set to connectionString.
myConnection.Open();
String query = "UPDATE homepage SET bballimg ='" + newimage + "' WHERE id='" + id + "'";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
protected void displayedit_ItemEditing(object sender, ListViewEditEventArgs e)
{
info.Text = "<b>*Recommend images are 490px x 190px(or of similar ratio) to avoid image distortion</b>";
}
protected void displayedit_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
info.Text = "Not Updating";
}
}
答案 0 :(得分:0)
试试这个......
将每个图像的名称存储在不同的隐藏字段中....
现在更新时检查:
if (fileupdateId.HasFile)
{
//then new file name
}
else
{
//else hiddenfield value
}
如果有任何疑问,请告诉我..