页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><%@ Register Assembly="CuteEditor" Namespace="CuteEditor" TagPrefix="CE" %><!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>
    <style type="text/css">
    body{font-size:12px;color:#666;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="fld_id"
            DataSourceID="adsnews" Height="50px" Width="597px" OnItemInserting="DetailsView1_ItemInserting">
            <Fields>
                <asp:BoundField DataField="fld_id" HeaderText="编号:" InsertVisible="False" ReadOnly="True"
                    SortExpression="fld_id" />
                <asp:BoundField DataField="fld_title" HeaderText="标题:" SortExpression="fld_title" />
                <asp:TemplateField HeaderText="内容:" SortExpression="fld_content">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("fld_content") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <CE:Editor ID="Editor1" runat="server" AutoConfigure="Default" AutoParseClasses="True"
                            CodeViewTemplateItemList="Save,Print,Cut,Copy,Paste,Find,ToFullPage,FromFullPage,SelectAll,SelectNone"
                            EditorOnPaste="ConfirmWord" MaxHTMLLength="0" MaxTextLength="0" Text='<%# Bind("fld_content") %>'>
                            <FrameStyle BackColor="White" BorderColor="#DDDDDD" BorderStyle="Solid" BorderWidth="1px"
                                CssClass="CuteEditorFrame" Height="100%" Width="100%" />
                        </CE:Editor>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("fld_content") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="新闻图片:" SortExpression="fld_photo">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("fld_photo") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                     <asp:FileUpload ID="FileUpload1" runat="server" />      
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("fld_photo") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="fld_time" HeaderText="发布时间:" SortExpression="fld_time" />
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:AccessDataSource ID="adsnews" runat="server" DataFile="~/App_Data/text.mdb"
            DeleteCommand="DELETE FROM [tb_news] WHERE [fld_id] = ?" InsertCommand="INSERT INTO [tb_news] ( [fld_title], [fld_content], [fld_photo], [fld_time]) VALUES ( ?, ?, ?, ?)"
            SelectCommand="SELECT * FROM [tb_news]" UpdateCommand="UPDATE [tb_news] SET [fld_title] = ?, [fld_content] = ?, [fld_photo] = ?, [fld_time] = ? WHERE [fld_id] = ?">
            <DeleteParameters>
                <asp:Parameter Name="fld_id" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="fld_title" Type="String" />
                <asp:Parameter Name="fld_content" Type="String" />
                <asp:Parameter Name="fld_photo" Type="String" />
                <asp:Parameter Name="fld_time" Type="DateTime" />
                <asp:Parameter Name="fld_id" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:ControlParameter ControlID="DetailsView1" Name="fld_title" PropertyName="SelectedValue"
                    Type="String" />
                <asp:ControlParameter ControlID="DetailsView1" Name="fld_content" PropertyName="SelectedValue"
                    Type="String" />
                <asp:ControlParameter ControlID="DetailsView1" Name="fld_photo" PropertyName="SelectedValue"
                    Type="String" />
                <asp:ControlParameter ControlID="DetailsView1" Name="fld_time" PropertyName="SelectedValue"
                    Type="DateTime" />
            </InsertParameters>
        </asp:AccessDataSource>
    
    </div>
    </form>
</body>
</html>
 
C# 后台
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DetailsView1.DefaultMode = DetailsViewMode.Insert;
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        FileUpload file1;
        file1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
        string fileName;
        fileName = file1.FileName;
        string filePath;
        filePath = Server.MapPath("text/");
        filePath += fileName;        if (file1.PostedFile.ContentType == "image/pjpeg" | file1.PostedFile.ContentType == "image/gif")
        {
            if (file1.PostedFile.ContentLength < 102400)
            {
                file1.SaveAs(filePath);
                adsnews.InsertParameters["fld_photo"].DefaultValue = file1.FileName;
                e.Cancel = true;
            }
            else
            {
                Response.Write("<script>alert('文件太大!');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('文件类型不正确!');</script>");
        }
    }
}
这样的话上传上去数据库字段只显示TURE值,请问代码要怎么样该才能让数据库该字段里面显示上传图片的名称?

解决方案 »

  1.   

    拜托用到数据库了么?
    你只是用路径保存
    如果你像保存到数据库,当然缓存 字段类型是image
      

  2.   

    FileUpload是否获取到了
    保存saveas 的路径
      

  3.   

    貌似没有数据库啊....我这里有段上次做的代码正好给你参考下
    SqlConnection con = new SqlConnection("server=.;database=news;uid=sa;pwd=sa");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into 产品 (产品名称,产品价格,产品图片,产品类别,产品介绍)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + FileUpload1.FileName+ "','" + DropDownList1.Text + "','" + TextBox4.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
    我这里是实现了你说的这个功能具体怎么改看你自己了