前台
- C# code
<%@ 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>
后台
- C# code
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; }}