asp.net通过ListView Control将图像插入数据库

时间:2014-02-02 16:59:20

标签: c# asp.net listview

我可以将图像插入数据库或在Image Control等中显示它们,但我想通过ListView将它们插入到数据库中。我部分实现了这一点。在我插入新项目期间,我通过FileUpload Control“FileUploadMedical”浏览图像并通过“MedicalUploadButton”将其插入数据库(您可以在下面看到它的代码)。我想摆脱那个MedicalUploadButton并使用默认的ListView“插入”和“编辑”按钮。我应该保留“FileUpload”控件以浏览图像。以下是我所做的事情。

通常我会通过ImageHandler.ashx在listview中显示图像,并在ItemTemplate上显示图像,如下所示;

   <ItemTemplate >
         <tr style="">
             <td>
                 <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
                 <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
             </td>
             <td>
                <asp:Image ID="MedicalImage" runat="server" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("MedicalID")+"&Entity=Medical"%>'/>
            </td>
         </tr>
    </ItemTemplate>

效果很好。

这是我将图像插入listview的方式;

        <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
            </td>
            <td>
                <asp:FileUpload ID="FileUploadMedical" runat="server" />
                <asp:Button ID="MedicalUploadButton" runat="server" text="Hoch Laden" OnClick="MedicalUploadButton_Click"/>
            </td>
        </tr>
    </InsertItemTemplate>

按钮MedicalUploadButton后面的代码

       protected void MedicalUploadButton_Click(object sender, EventArgs e)
    {
        FileUpload Fupload = (FileUpload)AdminListView.EditItem.FindControl("FileUploadMedical");
        if (Fupload.HasFile)
        {
            string FilePath = Fupload.PostedFile.FileName;
            string FileName = Path.GetFileName(FilePath);
            string Ext = Path.GetExtension(FileName);
            string ContentType = String.Empty;

            switch (Ext)
            {
                case ".jpg":
                    ContentType = "Image/jpg";
                    break;
                case ".jpeg":
                    ContentType = "Image/jpeg";
                    break;
                case ".png":
                    ContentType = "Image/png";
                    break;
                case ".bmp":
                    ContentType = "Image/bmp";
                    break;
            }

            if (ContentType != String.Empty)
            {
                Stream FileStream = Fupload.PostedFile.InputStream;
                BinaryReader FileReader = new BinaryReader(FileStream);
                Byte[] bytes = FileReader.ReadBytes((Int32)FileStream.Length);

                //double check and make sure that this is getting the correct item
                ListViewItem commentItem = ((Button)sender).NamingContainer as ListViewItem;

                if (commentItem != null)
                {
                    //instead of using the DisplayIndex use the DataItemIndex
                    int medID = (int)AdminListView.DataKeys[commentItem.DataItemIndex]["MedicalID"];

                    //insert the file into database
                    using (Entity.MedicalEntities emp = new Entity.MedicalEntities())
                    {

                        Entity.Medical medicals = (from h in emp.Medicals where h.MedicalID == medID select h).First();
                        medicals.MedicalImage = bytes;
                        emp.SaveChanges();

                    }
                }
            }
        }
    }

它实际上非常简单并且工作正常但是在InsertItemTemplate或EditItemTemplate我不想使用我的自定义MedicalUploadButton_Click函数但我只想使用“MedicalUpload”FileUpload控件,浏览图像然后单击插入或编辑应将图像插入数据库。

更确切地说,我想删除那个MedicalUploadButton_Click函数,并使用listview的默认“插入”和“编辑”按钮来插入图像。

1 个答案:

答案 0 :(得分:0)

经过一些研究后我解决了这个问题。首先,我们必须将以下事件添加到listview;

OnItemUpdating="DistrictList_ItemUpdating" OnItemInserted="DistrictList_ItemInserted"

在DistrictList_ItemUpdating后面的代码(对于编辑命令),您可以添加以下内容;

        protected void DistrictList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        FileUpload Fupload = (FileUpload)DistrictList.EditItem.FindControl("FileUploadDistrictEdit");
        if (Fupload.HasFile)
        {
            string FilePath = Fupload.PostedFile.FileName;
            string FileName = Path.GetFileName(FilePath);
            string Ext = Path.GetExtension(FileName);
            string ContentType = String.Empty;



            switch (Ext)
            {
                case ".jpg":
                    ContentType = "Image/jpg";
                    break;
                case ".jpeg":
                    ContentType = "Image/jpeg";
                    break;
                case ".png":
                    ContentType = "Image/png";
                    break;
                case ".bmp":
                    ContentType = "Image/bmp";
                    break;
            }

            if (ContentType != String.Empty)
            {
                Stream FileStream = Fupload.PostedFile.InputStream;
                BinaryReader FileReader = new BinaryReader(FileStream);
                Byte[] bytes = FileReader.ReadBytes((Int32)FileStream.Length);

                string disName = e.NewValues["DistrictName"].ToString(); 

                //insert the file into database
                using (Entity.MedicalEntities emp = new Entity.MedicalEntities())
                {

                    Entity.District districts = (from h in emp.Districts where h.DistrictName == disName select h).First();
                    districts.DistrictImage = bytes;
                    emp.SaveChanges();

                }
            }
        }
    }

对于我的更新部分,最重要的部分是以下行。获取listview的编辑行的DistrictName值;

 string disName = e.NewValues["DistrictName"].ToString(); 

对于Insert命令,您可以添加以下内容。我使用“插入”命令。获取新添加的“区域”记录的“区域名称”,并在linq的Where克劳中使用它。

protected void DistrictList_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        FileUpload Fupload = (FileUpload)DistrictList.InsertItem.FindControl("FileUploadDistrictInsert");
        if (Fupload.HasFile)
        {
            string FilePath = Fupload.PostedFile.FileName;
            string FileName = Path.GetFileName(FilePath);
            string Ext = Path.GetExtension(FileName);
            string ContentType = String.Empty;



            switch (Ext)
            {
                case ".jpg":
                    ContentType = "Image/jpg";
                    break;
                case ".JPG":
                    ContentType = "Image/jpg";
                    break;
                case ".jpeg":
                    ContentType = "Image/jpeg";
                    break;
                case ".JPEG":
                    ContentType = "Image/jpeg";
                    break;
                case ".png":
                    ContentType = "Image/png";
                    break;
                case ".PNG":
                    ContentType = "Image/png";
                    break;
                case ".bmp":
                    ContentType = "Image/bmp";
                    break;
                case ".BMP":
                    ContentType = "Image/bmp";
                    break;
            }

            if (ContentType != String.Empty)
            {
                Stream FileStream = Fupload.PostedFile.InputStream;
                BinaryReader FileReader = new BinaryReader(FileStream);
                Byte[] bytes = FileReader.ReadBytes((Int32)FileStream.Length);

                string disID = e.Values["DistrictName"].ToString();

                //insert the file into database
                using (Entity.MedicalEntities emp = new Entity.MedicalEntities())
                {

                    Entity.District districts = (from h in emp.Districts where h.DistrictName == disID select h).First();
                    districts.DistrictImage = bytes;
                    emp.SaveChanges();

                }
            }
        }
    }

对我而言,最重要的部分是获取新创建记录的“区域名称”的以下行;

 string disID = e.Values["DistrictName"].ToString();

如果您有任何建议或发现任何缺点,请告诉我。