62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<%-- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>--%>
<asp:GridView ID="gdvData" runat="server" AutoGenerateColumns="false" OnRowEditing="gdvData_RowEditing"
OnRowUpdating="gdvData_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="图片">
<ItemTemplate>
xxx</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload></EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
<%-- </ContentTemplate>
</asp:UpdatePanel>--%>
图片:<input type="file" runat="server" id="InputFile" style="width: 132px;" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
private static List<string> files = new List<string> { "a", "b" };
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BinData();
}
}
private void BinData()
{
gdvData.DataSource = files;
gdvData.DataBind();
}
protected void gdvData_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvData.EditIndex = e.NewEditIndex;
BinData();
}
protected void gdvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Response.Write((gdvData.Rows[e.RowIndex].FindControl("FileUpload1") as FileUpload).FileName + "<br/>");
string upFile = ((FileUpload)gdvData.Rows[e.RowIndex].FindControl("FileUpload1")).PostedFile.FileName;
UpLoad(upFile);
}
protected void Button1_Click(object sender, EventArgs e)
{
UpLoad(InputFile.Value);
}
private string UpLoad(string img)
{
string pictureName = "";
int idx = img.LastIndexOf('.');
string suffx = img.Substring(idx);
pictureName = DateTime.Now.Ticks.ToString() + suffx;
if (img != "")
{
string path = Server.MapPath("Upload/Image/");
InputFile.PostedFile.SaveAs(path + pictureName);
}
return pictureName;
}
}
<%@ Page Language="C#" EnableViewState="true" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private static List<string> files = new List<string> { "a", "b" };
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BinData();
}
}
private void BinData()
{
gdvData.DataSource = files;
gdvData.DataBind();
}
protected void gdvData_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvData.EditIndex = e.NewEditIndex;
BinData();
}
protected void gdvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
FileUpload upFile = gdvData.Rows[e.RowIndex].FindControl("FileUpload1") as FileUpload;
Image Image1 = gdvData.Rows[e.RowIndex].FindControl("Image1") as Image;
if (upFile != null)
{
Image1.Visible = true;
Image1.ImageUrl = "~/" + UpLoad(upFile);
}
else
{
Response.Write("Error");
}
}
private string UpLoad(FileUpload img)
{
string pictureName = "";
pictureName = DateTime.Now.Ticks.ToString() + System.IO.Path.GetFileName(img.FileName);
string path = Server.MapPath("~/" + pictureName);
img.SaveAs(path);
return pictureName;
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gdvData" runat="server" AutoGenerateColumns="false" OnRowEditing="gdvData_RowEditing"
OnRowUpdating="gdvData_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="图片">
<ItemTemplate>
xxx</ItemTemplate>
<EditItemTemplate>
<asp:Image ID="Image1" runat="server" Visible="false" Width="100" />
<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload></EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
</form>
</body>
</html>