【问题标题】:Trying to use File Upload in Listview? Update not working尝试在 Listview 中使用文件上传?更新不起作用
【发布时间】:2020-03-28 21:04:00
【问题描述】:

这是我的列表视图。我正在使用 OnItemCommand 来引用代码隐藏中的代码。在这里,我尝试使用新的 FileUpload 控件更新数据库。

 <%--Listview--%>
<asp:ListView runat="server" ID="livLocation" class="container"
    DataKeyNames="LocationID"
    DataSourceID="sdsListViewLocation"
    EmptyDataText="No data to display"
    InsertItemPosition="FirstItem"
    OnItemInserted="livLocation_ItemInserted"
    OnItemUpdated="livLocation_ItemUpdated"
    OnItemDeleted="livLocation_ItemDeleted"
    OnItemCanceling="livLocation_ItemCanceling"
    OnItemCommand="livLocation_ItemCommand">
</asp:ListView>

插入工作得非常好。当执行 Update 时,会为所有 FindControls 引发 Null 错误。我相信出于某种原因,FindControls 不适用于 Update。我试过给每个控件自己的ID,但这仍然没有解决问题。我一直在参考这篇文章,但到目前为止没有任何帮助:Upload images with fileupload within a Listview asp.net

 protected void livLocation_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        //
        if (e.CommandName == "Insert")
        {
            // Find controls on insert.
            TextBox txtLocation = (TextBox)livLocation.InsertItem.FindControl("txtLocation");
            TextBox txtImage = (TextBox)livLocation.InsertItem.FindControl("txtImage");
            FileUpload fuiImage = (FileUpload)livLocation.InsertItem.FindControl("fuiImage");

            // Get today's date
            String strDate = DateTime.Now.ToString("MM-dd-yyyy-h-m-stt");

            // If file is there to upload.
            if (fuiImage.HasFile)
            {
                // Set path.
                String strFileName = txtLocation.Text + "-" + strDate + ".jpg";
                String strPath = Request.PhysicalApplicationPath + "Image\\Location\\" + strFileName;

                // Save file.
                fuiImage.SaveAs(strPath);

                // Fill Image textbox
                txtImage.Text = strFileName;
            }
            else
            {
                // Do nothing
            }
        }
        else if (e.CommandName == "Update")
        {
            // Find controls on insert.
            TextBox txtLocation = (TextBox)livLocation.InsertItem.FindControl("txtLocation");
            TextBox txtImage = (TextBox)livLocation.InsertItem.FindControl("txtImage");
            FileUpload fuiImage = (FileUpload)livLocation.InsertItem.FindControl("fuiImage");

            // Get today's date
            String strDate = DateTime.Now.ToString("MM-dd-yyyy-h-m-stt");

            // If file is there to upload.
            if (fuiImage.HasFile)
            {
                // Set path.
                String strFileName = txtLocation.Text + "-" + strDate + ".jpg";
                String strPath = Request.PhysicalApplicationPath + "Image\\Location\\" + strFileName;

                // Save file.
                fuiImage.SaveAs(strPath);

                // Fill Image textbox
                txtImage.Text = strFileName;
            }
            else
            {
                // Do nothing
            }
        }
        else if (e.CommandName == "Delete")
        {
            // Delete file.
            FileUpload fuiImage = (FileUpload)livLocation.InsertItem.FindControl("fuiImage");
            String strPath = Request.PhysicalApplicationPath + "Image\\Location\\" + fuiImage.FileName;

            System.IO.File.Delete("strPath");

        }
        else
        {
            // Do nothing
        }
    }
}

【问题讨论】:

  • 你能包含aspx文件的ListView的语法吗..
  • @HanyHabib 已更新。
  • 首先,您应该使用 sender 对象,以便以后更易于维护。其次,编辑索引和编辑项的值是什么?
  • @Hany Habib 好的,我会这样做。我不确定您所说的这些值是什么意思?

标签: c# sql asp.net


【解决方案1】:

正如所讨论的,使用 sender 对象来提高以后的可维护性总是很不错的。对于您的问题,请访问以下链接以获取已编辑的项目,因为您的语法不正确:https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview.itemediting?view=netframework-4.8

它显示了有关项目编辑以及如何使用新的编辑索引获取已编辑项目的更多详细信息。


(TextBox)livLocation.InsertItem 不正确,应该是EditItem 所以它将是(FileUpload)(sender as ListView).EditItem.FindControl.....

【讨论】:

  • 你必须使用 (FileUpload)livLocation.EditItem.FindControl
  • 将包括它:)
  • 使用 Delete 时有什么建议吗?
猜你喜欢
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多