Click here to Skip to main content
15,887,241 members
Please Sign up or sign in to vote.
4.14/5 (4 votes)
See more:
hi all..
i have gridview in this i placed file upload control and a button called upload
when i click upload button i need to get file path in upload control save it in desired location
here i tried in this way
XML
<asp:GridView ID="GridView1" CellPadding="4" runat="server" OnRowCommand="GridView1_RowCommand"
                                                AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" PageSize="5"
                                                Width="95%" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting">
                                                <AlternatingRowStyle BackColor="aliceBlue" />
                                                <HeaderStyle HorizontalAlign="Left" />
                                                <Columns>
                                                    <asp:TemplateField Visible="false">
                                                        <ItemTemplate>
                                                            <asp:Label ID="eid" runat="server" Text='<%# Eval("uid") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:BoundField DataField="ccode" HeaderText="Company Code" />
                                                    <asp:BoundField DataField="cname" HeaderText="Company Name" />
                                                    <asp:TemplateField HeaderText="Company Logo">
                                                        <ItemTemplate>
                                                            <asp:Image ID="LogoImage" ImageUrl="~/images/logo-not-available.jpg" runat="server"
                                                                Height="80px" Width="150px" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField Visible="false">
                                                        <ItemTemplate>
                                                            <asp:Label ID="logopathlbl" runat="server" Text='<%# Eval("logopath") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Logo Upload">
                                                        <ItemTemplate>
                                                            <asp:FileUpload ID="FileUpload4" runat="server" />
                                                            <asp:Button ID="bt_upload" runat="server" EnableViewState="False" Text="Upload" CommandName="Upload" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                            </asp:GridView>

when am calling from code behind in row command like
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Button bts = e.CommandSource as Button;
        Response.Write(bts.Parent.Parent.GetType().ToString());
        if (e.CommandName.ToLower() == "upload")
        {
            FileUpload fu = bts.Parent.Parent.FindControl("FileUpload4") as FileUpload;//here it is detecting file upload4 

            if (fu.HasFile)//here it is showing no file in file upload control
            {
                Response.Write(fu.PostedFile.FileName);
            }
        }
    }

or else show me any other ways..
this is my bigggg problem can any one help me please
thanks in advance
Posted
Updated 18-Jul-11 23:43pm
v3

Hi

It should be working...
I try it...

See the entire code that I had made and run:

In Client code:
    <body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td>
                    <asp:GridView ID="GridView1" CellPadding="4" runat="server" OnRowCommand="GridView1_RowCommand"
                        AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" PageSize="5"
                        Width="95%" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" EmptyDataText="No record in Company Table">
                        <AlternatingRowStyle BackColor="aliceBlue" />
                        <HeaderStyle HorizontalAlign="Left" />
                        <Columns>
                            <asp:TemplateField Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="eid" runat="server" Text='<%# Eval("uid") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ccode" HeaderText="Company Code" />
                            <asp:BoundField DataField="cname" HeaderText="Company Name" />
                            <asp:TemplateField HeaderText="Company Logo">
                                <ItemTemplate>
                                    <asp:Image ID="LogoImage" ImageUrl="~/Images/mcts.JPG" runat="server" Height="80px"
                                        Width="150px" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="logopathlbl" runat="server" Text='<%# Eval("logopath") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Logo Upload">
                                <ItemTemplate>
                                    <asp:FileUpload ID="FileUpload4" runat="server" />
                                    <asp:Button ID="bt_upload" runat="server" EnableViewState="False" Text="Upload" CommandName="Upload" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>



In my code behind:
   public class Company
{
    private string uid;
    private string ccode;
    private string cname;
    private string logopath;
    
    public string Uid { get { return uid; } set { uid = value; } }
    public string Ccode { get { return ccode; } set { ccode = value; } }
    public string Cname { get { return cname; } set { cname = value; } }
    public string Logopath{get { return logopath; }set { logopath = value; }}
    public Company()
    { }
}

public partial class Default2 : System.Web.UI.Page
{
    string myConn = ConfigurationManager.ConnectionStrings["TestConn"].ConnectionString;
    SqlConnection conn;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Company> coLst = new List<Company>();
            using (conn = new SqlConnection(myConn))
            {
                conn.Open();
                StringBuilder sbQry = new StringBuilder();
                sbQry.Append("SELECT * from Company ");
                using (cmd = new SqlCommand(sbQry.ToString(), conn))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader odr = cmd.ExecuteReader())
                    {
                        while (odr.Read())
                        {
                            Company company = new Company();
                            {
                                company.Ccode = odr["uid"].ToString();
                                company.Ccode = odr["ccode"].ToString();
                                company.Cname = odr["cname"].ToString();
                                company.Logopath = odr["logopath"].ToString();
                            }
                            coLst.Add(company);
                        }
                    }
                }
            }
            GridView1.DataSource = coLst;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Button bts = e.CommandSource as Button;
        Response.Write(bts.Parent.Parent.GetType().ToString());
        if (e.CommandName.ToLower() != "upload")
        {
            return;
        }
        FileUpload fu = bts.FindControl("FileUpload4") as FileUpload;//here 
        if (fu.HasFile)
        {
            bool upload = true;
            string fleUpload = Path.GetExtension(fu.FileName.ToString());
            if (fleUpload.Trim().ToLower() == ".xls" | fleUpload.Trim().ToLower() == ".xlsx")
            {
                fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
                string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
                //Someting to do?...
            }
            else
            {
                upload = false;
                // Something to do?...
            }
            if (upload)
            {
                // somthing to do?...
            }
        }
        else
        {
            //Something to do?...
        }
    }


Just revised this to fit in your needs..

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem
 
Share this answer
 
v3
Hi,

Try this if it could help ...
Example in your code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Button bts = e.CommandSource as Button;
    Response.Write(bts.Parent.Parent.GetType().ToString());
    if (e.CommandName.ToLower() != "upload")
    {
        return;
    }
    FileUpload fu = bts.FindControl("FileUpload4") as FileUpload;//here
    if (fu.HasFile)
    {
        bool upload = true;
        string fleUpload = Path.GetExtension(fu.FileName.ToString());
        if (fleUpload.Trim().ToLower() == ".xlsx")
        {
            fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
            string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));

            //Someting to do?...
        }
        else
        {
            upload = false;
            // Something to do?...
        }
        if (upload)
        {
              // somthing to do?...
        }
    }
    else
    {
        //Something to do?...
    }
}



You must have a Sub directory UpLoadPath in you apps domain rather in you
project solution to copy such file, to be visible in your apps. Otherwise this could
run in your local, but when it published you may encountered file not found error...

Hope this could help...

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem
 
Share this answer
 
v2
Comments
tulasiram3975 19-Jul-11 6:54am    
NO Here it is going to else Block of fu.HasFile.file upload controler is not having file
JustShahzad 15-Mar-12 5:19am    
Its solved ma problem. thx!
Hi,

Check this[^]


and


This[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900