在编辑模式下在详细信息视图中上传文件

发布于 2024-12-06 07:09:39 字数 2324 浏览 3 评论 0原文

您好,我尝试在详细信息视图中添加文件上传,我在此处附加代码中的一些部分:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="586px" 
        DefaultMode="Edit" AutoGenerateRows="False" BorderColor="White" 
        BorderStyle="None" DataSourceID="EntityDataSource1" GridLines="None" DataKeyNames="UserName" OnItemUpdated="DetailsView1_ItemUpdated" ONItemEditing="DetailsView1_ItemEditing">

然后文件上传控件放置在模板字段内:

 <asp:TemplateField HeaderText="Foto">
                      <EditItemTemplate>


<asp:FileUpload ID="FileUpload1" runat="server" />
                         </EditItemTemplate>
     </asp:TemplateField>

数据源是:

 <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=mesteriEntities" DefaultContainerName="mesteriEntities" 
        EnableFlattening="False" EntitySetName="Users" 
         EnableUpdate="True" AutoGenerateWhereClause="True" 
    EnableInsert="True">
         <WhereParameters>
        <asp:SessionParameter Name="UserName" SessionField="New" Type="String" />
         </WhereParameters>
    </asp:EntityDataSource>

背后的代码:

 protected void DetailsView1_ItemEditing(object sender, DetailsViewInsertEventArgs e)
    {
        FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
        if (fu1 == null)
            e.Cancel = true;
        if (fu1.HasFile)
        {
            try
            {
                string fileName = Guid.NewGuid().ToString();
                string virtualFolder = "~/UserPics/";
                string physicalFolder = Server.MapPath(virtualFolder);
               // StatusLabel.Text = "Upload status: File uploaded!";
                string extension = System.IO.Path.GetExtension(fu1.FileName);
                fu1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
                e.Values["foto"] = System.IO.Path.Combine(physicalFolder, fileName + extension);
            }
            catch (Exception ex)
            {
              Response.Write(ex.Message);
            }
        }
        else
            e.Cancel = true;



    }

我不确定为什么不起作用。它不会将文件上传到服务器上,也不在文件的数据库内添加引用。我在这里做错了什么?

谢谢

Hello i try to add a fileupload inside of a detailsview i attach here some parts from my code:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="586px" 
        DefaultMode="Edit" AutoGenerateRows="False" BorderColor="White" 
        BorderStyle="None" DataSourceID="EntityDataSource1" GridLines="None" DataKeyNames="UserName" OnItemUpdated="DetailsView1_ItemUpdated" ONItemEditing="DetailsView1_ItemEditing">

then the fileupload control is placed inside of template field:

 <asp:TemplateField HeaderText="Foto">
                      <EditItemTemplate>


<asp:FileUpload ID="FileUpload1" runat="server" />
                         </EditItemTemplate>
     </asp:TemplateField>

and the datasource is :

 <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=mesteriEntities" DefaultContainerName="mesteriEntities" 
        EnableFlattening="False" EntitySetName="Users" 
         EnableUpdate="True" AutoGenerateWhereClause="True" 
    EnableInsert="True">
         <WhereParameters>
        <asp:SessionParameter Name="UserName" SessionField="New" Type="String" />
         </WhereParameters>
    </asp:EntityDataSource>

The code behind:

 protected void DetailsView1_ItemEditing(object sender, DetailsViewInsertEventArgs e)
    {
        FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
        if (fu1 == null)
            e.Cancel = true;
        if (fu1.HasFile)
        {
            try
            {
                string fileName = Guid.NewGuid().ToString();
                string virtualFolder = "~/UserPics/";
                string physicalFolder = Server.MapPath(virtualFolder);
               // StatusLabel.Text = "Upload status: File uploaded!";
                string extension = System.IO.Path.GetExtension(fu1.FileName);
                fu1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
                e.Values["foto"] = System.IO.Path.Combine(physicalFolder, fileName + extension);
            }
            catch (Exception ex)
            {
              Response.Write(ex.Message);
            }
        }
        else
            e.Cancel = true;



    }

I'm not sure why doesn't work. It doesn't upload the file on the server and doesn't add reference inside database of the file . Whay i did wrong here?

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2024-12-13 07:09:39

据我所知(通过查看类文档: DetailsView 类) 没有 OnItemEditing 事件来处理?

但是有一个 DetailsView.ItemUpdating 事件看起来可以解决这个问题:

单击“DetailsView”控件中的“更新”按钮时发生,
但在更新操作之前。

另外,我认为无法找到 FileUpload 控件,因为 FindControl 方法没有搜索它包含的控件的完整层次结构。

尝试使用以下方法并修改您的代码,如下所示:

FileUpload fu1 = (FileUpload)FindControl(DetailsView1, "FileUpload1");

...

private Control FindControl(Control parent, string id)
{
    foreach (Control child in parent.Controls)
    {
        string childId = string.Empty;
        if (child.ID != null)
        {
            childId = child.ID;
        }

        if (childId.ToLower() == id.ToLower())
        {
            return child;
        }
        else
        {
            if (child.HasControls())
            {
                Control response = FindControl(child, id);
                if (response != null)
                    return response;
            }
        }
    }

    return null;
}

As far as I can tell (from looking at the class documentation: DetailsView Class) there is no OnItemEditing event to handle?

There is however a DetailsView.ItemUpdating event which looks like it could do the trick:

Occurs when an Update button within a DetailsView control is clicked,
but before the update operation.

Also I think the FileUpload control cannot be found because the FindControl method is not searching the full hierarchy of controls it contains.

Try using the following method and modifying your code like so:

FileUpload fu1 = (FileUpload)FindControl(DetailsView1, "FileUpload1");

...

private Control FindControl(Control parent, string id)
{
    foreach (Control child in parent.Controls)
    {
        string childId = string.Empty;
        if (child.ID != null)
        {
            childId = child.ID;
        }

        if (childId.ToLower() == id.ToLower())
        {
            return child;
        }
        else
        {
            if (child.HasControls())
            {
                Control response = FindControl(child, id);
                if (response != null)
                    return response;
            }
        }
    }

    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文