none
gridview 模版列获取上传控件问题,如果用UpdatePanel就报错,这是为什么? RRS feed

  • 问题

  • <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CSharp.WebForm1" %> <!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> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"> <Columns> <asp:TemplateField HeaderText="图片" SortExpression="Img"> <ItemTemplate> ss </ItemTemplate> <EditItemTemplate> <asp:FileUpload ID="FileUpload1" runat="server" /> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Columns> </asp:GridView>

    图片:<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>

    cs

    public partial class WebForm1 : System.Web.UI.Page { private static List<string> files = new List<string> {"a","n" }; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = files; GridView1.DataBind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; GridView1.DataSource = files; GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { 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;
        }

    }


    这段代码现在没什么问题,但我如果在gridview外层 添加个 UpdatePanel无刷新控件 ,那么在后台获取FileUpload1就会报错,这是为什么? 还有个问题,在gridview外放个上传控件,然后上传图片,OK没问题,但是在gridview里的那个上传控件 ,我如果上传图片,那么图片就是0KB,打开 提示“没有预览” 这又是为什么

    2012年7月17日 2:45

答案