C#asp.net在gridview中显示图像 [英] c# asp.net displaying image in gridview

查看:69
本文介绍了C#asp.net在gridview中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在gridview中显示图像.

I wish to display an image in a gridview.

我有一个包含4列的数据库-ID,作者,注释,文件名(包含img路径)

I have a database with 4 columns - id, author, comment, filename (which holds the img path)

http://postimg.org/image/4loz8t9fb/

我有一个图像上传方法,因此当用户写评论,选择照片并单击提交时,他的名字,评论和照片路径将进入数据库(并且照片本身被上传到图片"文件夹中项目文件夹).

I have an image upload method so when the user writes his comment, chooses a photo and clicks submit- his name, the comment and the photo path goes to the database (and the photo itself is uploaded to 'Images' folder in the project folder).

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns = "False"
                   Font-Names = "Arial" Height="375px" Width="498px" DataSourceID="SqlDataSource2" >
                <Columns>

                    <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
                    <asp:BoundField DataField="Comment" HeaderText="Comment" SortExpression="Comment" />
                    <asp:BoundField DataField="FileName" HeaderText="FileName" SortExpression="FileName" />
                    <asp:TemplateField HeaderText="Image">

                      <ItemTemplate >

                        <asp:Image ID="Image1" runat="server" ImageUrl ='<%# Eval("FileName") %>' height="120px" Width="150px" />

                      </ItemTemplate>

                    </asp:TemplateField>
                </Columns>
                </asp:GridView>

这是c#代码-

protected void addcomment_Click(object sender, EventArgs e)
    {
        string FileName = "";
        if (fileupload.PostedFile != null)
        {
             FileName = Path.GetFileName(fileupload.PostedFile.FileName);
             //Save files to disk
             fileupload.SaveAs(Server.MapPath("Images/" + FileName));

        }




        if (TextBox1.Text.Length > 0)
        {
            string name = firstName + " " + lastName;
            SqlCommand cmd = new SqlCommand("insert into comments values(@name,@comment,@filename)", conn);
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@comment", TextBox1.Text);
            cmd.Parameters.AddWithValue("@filename", Server.MapPath("Images/" + FileName));
            conn.Open();
            cmd.ExecuteNonQuery();
            GridView1.DataBind();
            conn.Close();
            TextBox1.Text = "";
            Response.Redirect(Request.Url.AbsoluteUri);
        }
        else
        {
            TextBox1.Text = "You have to type something buddy!";
        }

    }

gridview数据源当然是sql.

The gridview datasource is the sql of course.

这是数据库和网站的图像

Here is an image of the database and the website

http://postimg.org/image/4loz8t9fb/

推荐答案

未设置完整路径,

 string savepath = "~/Images/" + Filename;
cmd.Parameters.AddWithValue("@filename", savepath));

只需更改此行即可使用

甚至改变

 fileupload.SaveAs(Server.MapPath("~/Images/" + FileName));

这篇关于C#asp.net在gridview中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆