Click here to Skip to main content
15,888,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I use this code for Upload a Image and also for update it,But i found that During Autopostback my fileupload doesn't contain image,Is there any solution for that.

XML
<div class="form-group">
            <asp:Label for="inputEmail1" class="col-md-2 control-label" ID="lblimage"
                meta:resourcekey="lblBannerSignUp" Text="Image" runat="server"></asp:Label>

            <div class="col-md-3">
             <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="conditional">
                                    <Triggers>
                                        <asp:PostBackTrigger ControlID="btnSubmit" />
                                    </Triggers>
                                    <ContentTemplate>
                                         <asp:FileUpload  ID="fuimage" runat="server" meta:resourcekey="fuimage123" />
                                    </ContentTemplate>
                                </asp:UpdatePanel>

            </div>
             <div class="col-md-10 pull-right">
            <img id="imagezoom" runat="server" />

             <%--   <asp:Image ID="imagezoom" runat="server" />--%>
                <%-- <asp:PlaceHolder ID="pnll" runat="Server"></asp:PlaceHolder >--%>
            </div>
            </div>



code for .cs file
C#
EventManager_SuperAdmin m;
        protected void Page_Load(object sender, EventArgs e)
        {
            m = (EventManager_SuperAdmin)Page.Master;
            
            if (Session["FileUpload1"] == null && fuimage.HasFile)
            {
                Session["FileUpload1"] = fuimage;

            }
           
            else if (Session["FileUpload1"] != null && (!fuimage.HasFile))
            {
                fuimage = (FileUpload)Session["FileUpload1"];

            }
            
            else if (fuimage.HasFile)
            {
                Session["FileUpload1"] = fuimage;

            }
            if (Session["name"] == null)
            {
                Response.Redirect("~/SuperAdmin/Default.aspx");
            }

            if (!this.IsPostBack)
            {

                m.bussinesCollection.IDSecurity.Decrypt(Request.QueryString["id"]);

                if (Request.QueryString["id"] != null)
                {

                    var result1 = m.bussinesCollection.FeaturesBL.GetFeatureByFeatureID(Convert.ToInt32(m.bussinesCollection.IDSecurity.Decrypt(Request.QueryString["id"])));
                    txttitle.Text = result1.Title;
                    CKEditor1.Text = result1.Text;
                    fuimage.Attributes.Add("FileName", result1.Image);
                    imagezoom.Src = "/SuperAdmin/Images/" + result1.Image;

                    if (result1.Show_at_home == "Yes")
                    {
                        Chckshowfeatures.Checked = true;
                    }
                    else
                    {
                        Chckshowfeatures.Checked = false;
                    }

                }
            }



        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (Request.QueryString["id"] != null)
            {



                string image = fuimage.FileName;

                if (fuimage.HasFile)
                {

                    string Extention = System.IO.Path.GetExtension(fuimage.FileName);
                    if (Extention.ToLower() == ".jpg" || Extention.ToLower() == ".jpeg" || Extention.ToLower() == ".bmp" || Extention.ToLower() == ".png" || Extention.ToLower() == ".gif")
                    {


                        var path = Server.MapPath("Images/" + fuimage.FileName);
                        fuimage.SaveAs(path);

                    }
                }

                if (Chckshowfeatures.Checked == true)
                {
                    m.bussinesCollection.FeaturesBL.Show_at_home = "Yes";
                }
                else
                {
                    m.bussinesCollection.FeaturesBL.Show_at_home = "False";
                }
                m.bussinesCollection.FeaturesBL.UpdateFeatures(Convert.ToInt32(m.bussinesCollection.IDSecurity.Decrypt(Request.QueryString["id"])), txttitle.Text, CKEditor1.Text,image, m.bussinesCollection.FeaturesBL.Show_at_home);

                lblmsg.Visible = true;
                //  lblmsg.Text = "Records updated successfully.";
                Response.Redirect("ListOfFeatures.aspx");
            }
            else
            {
                if (fuimage.HasFile)
                {
                    string Extention = System.IO.Path.GetExtension(fuimage.FileName);
                    if (Extention.ToLower() == ".jpg" || Extention.ToLower() == ".jpeg" || Extention.ToLower() == ".bmp" || Extention.ToLower() == ".png" || Extention.ToLower() == ".gif")
                    {


                        var path = Server.MapPath("Images/" + fuimage.FileName);
                        fuimage.SaveAs(path);
                      // this.Session["fileuploadforupdate"] = fuimage;
                    }
                }

                m.bussinesCollection.FeaturesBL.Title = txttitle.Text;
                m.bussinesCollection.FeaturesBL.Text = CKEditor1.Text;
             //   m.bussinesCollection.FeaturesBL.Image = "~/SuperAdmin/Images/"+fuimage.FileName;
                m.bussinesCollection.FeaturesBL.Image =fuimage.FileName;
                if (Chckshowfeatures.Checked == true)
                {
                    m.bussinesCollection.FeaturesBL.Show_at_home = "Yes";
                }
                else
                {
                    m.bussinesCollection.FeaturesBL.Show_at_home = "False";
                }
                m.bussinesCollection.FeaturesBL.Insert();

                txttitle.Text = "";
                CKEditor1.Text = "";

                lblmsg.Visible = true;
                //  lblmsg.Text = "Records Inserted Successfully";
                Response.Redirect("ListOfFeatures.aspx");

            }
        }



I also try with session but it is not help me.

If there is any other solution ?

Thanks in advance.
Posted

1 solution

try this in your aspx:-
XML
<div class="col-md-3">
     <asp:UpdatePanel ID="UpdatePanel2" runat="server"                   UpdateMode="conditional">
    <ContentTemplate>
           <asp:FileUpload  ID="fuimage" runat="server" meta:resourcekey="fuimage123" />
          </ContentTemplate>
         <Triggers>
             <asp:PostBackTrigger ControlID="btnSubmit" />
         </Triggers>
     </asp:UpdatePanel>
 </div>
 
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