Click here to Skip to main content
15,868,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've tried solving this out and researching for weeks and i still can't get it to work. My code is to simply upload images and then save it in the database without postback.

As of now, I used AjaxFileUpload to upload images. My plan was to get the uploaded filename in AjaxFileUpload using javascript and then store it in a hiddenfield. And then when the admin clicks submit, it will get the value that was stored in the hiddenfield and then save it in the database(using the query that i have created in my code-behind).

The problem is, it will always return empty. I hope you guys can really help me on this one.

What I have tried:

Here is the code for the aspx:

C#
<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="CreateBrands.aspx.cs" Inherits="Pages_CreateBrands" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" 
TagPrefix="asp"%>  

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Music Store</title>
<script src="../Javascript/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="~/Styles/jquery.bxslider.css"/>
<link rel="shortcut icon" type="image/png" href="~/Images/rockSign.png"/>
<script type="text/javascript">
    function abc() {
    var elem1 = document.getElementById('<%# itemFileUpload1.ID %>').value;
        document.getElementById('HiddenInput1') = elem1;
    var elem2 = document.getElementById('<%# itemFileUpload1.ID %>').value;
        document.getElementById('HiddenInput2') = elem2;
    }
</script>
</head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
     <div id="wrapper">

         <h1>Item Image1:</h2>
         <br />

         <asp:AjaxFileUpload ID="itemFileUpload1" runat="server"
         Width="300px" OnUploadComplete="itemUploadImage1"
         OnClientUploadComplete="abc"/>
       <input type="hidden" id="HiddenInput1" name="HiddenInput" value="" />

        <h1>Item Image2:</h2>
        <br />

        <asp:AjaxFileUpload ID="itemFileUpload2" runat="server" 
         Width="300px" OnUploadComplete="itemUploadImage2" 
        OnClientUploadComplete="abc"/>
       <input type="hidden" id="HiddenInput2" name="HiddenInput" value="" />
        <br/>

      <asp:Label ID="lblResult2" runat="server" Text=""></asp:Label>
        <br />

      <asp:Button ID="Button1" runat="server" CssClass="submitButton" 
      Text="Save Item" OnClick="Button1_Click"/>

    </div>
   </form>
 </body>
 </html>


And here is the code for the aspx.cs:

C#
protected void itemUploadImage1(object sender, AjaxFileUploadEventArgs e)
{

        string filename = Path.GetFileName(e.FileName);
        itemFileUpload1.SaveAs(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/") + filename);
}

protected void itemUploadImage2(object sender, AjaxFileUploadEventArgs e)
{
        string filename = Path.GetFileName(e.FileName);
        itemFileUpload2.SaveAs(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/") + filename);

}

 protected void Button1_Click(object sender, EventArgs e)
 {
        try {

            string item_image1 = Request.Form["HiddenInput1"];
            string item_image2 = Request.Form["HiddenInput2"];

            ConnectionClassGuitarItems.AddStringInstrumentItems(item_image1,item_image2);

            lblResult2.Text = "Upload successful!" + item_image1 + " and " + item_image2;

            ClearTextFields2();
        }
        catch (Exception ex)
        {
            lblResult2.Text = ex.Message;
        }
}


Notice the the Button1_Click, I try to access the value of HiddenInput1 and HiddenInput2 but it seems like they are empty.
Posted
Comments
ZurdoDev 14-Aug-17 15:15pm    
Debug your javascript. Is the function abc() being called? If so, are the values being passed properly there?

Also, why do you need the filenames after button click? You already used them in the upload.
BebeSaiyan 14-Aug-17 23:28pm    
I need those filenames so that I can save it in my database. I have already tried passing the filename from the upload(which is in ItemUploadImage1 and 2), but the value is always empty. Probably it doesn't retain the value due to postback. I just tried out getting the filename through clientside to see if I will be able to retrieve the filename, but it turns out that its also not working.
Karthik_Mahalingam 16-Aug-17 3:18am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Kornfeld Eliyahu Peter 15-Aug-17 1:00am    
document.getElementById('<%# itemFileUpload1.ID %>')
This one probable gets you a big 'undefined'... Debug it... and use ClientID instead of ID...

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