使用c#在asp.net中上传图片 [英] image uploading in asp.net using c#

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

问题描述

图片未在网格视图中显示

ASP代码

image not showing in grid view
ASP code

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .auto-style15 {
            width: 91px;

        }
        .auto-style16 {
            width: 91px;
            height: 23px;

        }
        .auto-style17 {
             height: 23px;

             border: 2px solid #a1a1a1;
             border-radius:5px;
             margin-top:50px;
             margin-left:300px;
             padding-left:0;
        }
        .auto-style18 {
            width: 235px;
        }
        .auto-style19 {
            width: 235px;

            height: 23px;
        }
        .auto-style20 {
            width: 91px;
            height: 26px;

        }
        .auto-style21 {
            width: 235px;

            height: 26px;
        }
        .auto-style22 {
            width: 91px;
            height: 37px;
        }
        .auto-style23 {
            width: 235px;
            height: 37px;
            margin-left: 40px;
        }
        .grid{
          margin-left:150px;
          margin-top:50px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <table class="auto-style17">
        <tr>
            <td class="auto-style15" align="center">username</td>
            <td class="auto-style18" >
                <asp:TextBox ID="TextBox2" runat="server" Width="171px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style20" align="center">password</td>
            <td class="auto-style21">
                <asp:TextBox ID="TextBox3" runat="server" Width="171px" TextMode="Password"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style16" align="center">confirm mail</td>
            <td class="auto-style19">
                <asp:TextBox ID="TextBox4" runat="server" Width="171px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style15" align="center">upload image</td>
            <td class="auto-style18">
                <asp:FileUpload ID="FileUpload1" runat="server"/>
            </td>
        </tr>
        <tr>
            <td class="auto-style15">&nbsp;</td>
            <td class="auto-style18">&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style22"></td>
            <td class="auto-style23">
                <asp:Button ID="Button1" runat="server" Text="Submit"  Width="65px" OnClick="Button1_Click1" />
                <asp:Button ID="Button2" runat="server" style="margin-left: 14px" Text="Reset" />
            </td>
        </tr>
        <tr>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style23">
                <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
    </table>


        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="userid" DataSourceID="SqlDataSource1" Width="608px" CssClass="grid" AllowPaging="True" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
            <Columns>
                <asp:BoundField DataField="userid" HeaderText="userid" InsertVisible="False" ReadOnly="True" SortExpression="userid" />
                <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
                <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                <asp:BoundField DataField="mail" HeaderText="mail" SortExpression="mail" />
                <asp:BoundField DataField="imagename" HeaderText="imagename" SortExpression="imagename" />
                <asp:ImageField DataImageUrlField="uploadimage" HeaderText="uploadimage">
                    <ControlStyle Width="70%" Height="70%"/>
                </asp:ImageField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:prakashConnectionString %>" SelectCommand="SELECT * FROM [getezeeregister]"></asp:SqlDataSource>




</asp:Content>









//_________________C# code________________







//_________________C# code________________

public void GvBind()
        {
                
            SqlCommand cmd = new SqlCommand("select * from getezeeregister", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            
        }




    protected void Button1_Click1(object sender, EventArgs e)
    {
        

     if (FileUpload1.HasFile)
        {
            
            string pathName = "userimage/" + Path.GetFileName(FileUpload1.PostedFile.FileName);
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
         SqlCommand cmd = new SqlCommand("insert into getezeeregister(username,password,mail,imagename,uploadimage) values ('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + filename + "','"+pathName+"')", con);
          con.Open();
          cmd.ExecuteNonQuery();
           con.Close();
           FileUpload1.SaveAs(Server.MapPath("~/userimage/"+FileUpload1.FileName));
          ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Registered successfully..!! ');", true);
         

                    
        }
        else
        {
 
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Please select a file to upload ');", true);
        }
    }

推荐答案

ConnectionStrings:prakashConnectionString %>\" SelectCommand=\"SELECT * FROM [getezeeregister]\"></asp:SqlDataSource>




</asp:Content>
ConnectionStrings:prakashConnectionString %>" SelectCommand="SELECT * FROM [getezeeregister]"></asp:SqlDataSource> </asp:Content>









//_________________C# code________________







//_________________C# code________________

public void GvBind()
        {
                
            SqlCommand cmd = new SqlCommand("select * from getezeeregister", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            
        }




    protected void Button1_Click1(object sender, EventArgs e)
    {
        

     if (FileUpload1.HasFile)
        {
            
            string pathName = "userimage/" + Path.GetFileName(FileUpload1.PostedFile.FileName);
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
         SqlCommand cmd = new SqlCommand("insert into getezeeregister(username,password,mail,imagename,uploadimage) values ('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + filename + "','"+pathName+"')", con);
          con.Open();
          cmd.ExecuteNonQuery();
           con.Close();
           FileUpload1.SaveAs(Server.MapPath("~/userimage/"+FileUpload1.FileName));
          ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Registered successfully..!! ');", true);
         

                    
        }
        else
        {
 
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Please select a file to upload ');", true);
        }
    }


Hi,



Just check the below url to diplay the image in a gridview fetching the data from sql server database.

http://aspsnippets.com/Articles/Display-images-from-SQL-Server-Database-in-ASP.Net-GridView-control.aspx


Check on below link this will help you.

http://www.aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html[^]
Check on below link this will help you.
http://www.aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html[^]


这篇关于使用c#在asp.net中上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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