Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I call postback if a file has been added to the fileupload

Posted on 2008-10-15
8
Medium Priority
?
543 Views
Last Modified: 2013-12-17
My code works when a drop downlist triggers postback on my page but the ddl is used before the fileupload. How can I call postback when a file has been browsed and put into the file upload. Or how could I do this differently? Basically I want to stop the user submiting the information they have inputted on my page if they have not attached the file. This is why I im making the button invisible if  the path is still present in the file upload. Any ideas would superb...thanks!
if (FileUploadControl.HasFile)
 {
  btnSubmit.Visible = false;
 }
 else
 {
  btnSubmit.Visible = true;
 }

Open in new window

0
Comment
Question by:dwezil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:johnaryan
ID: 22719650
2 ways of doing this
1. You could use javascript on the web page to verify that a file has been inputted.

2. Check the value of the FileUploadControlin the PageInit() event rather than the page load event.
function ValidateDisplayName()
{  
    if(document.all.FileUploadControl.value == "")
    {
        alert("You must select a file to upload before continuing.");
       return false; 
    }
    return true;  
}
 
 <input id="inputUploadFile" type="file" runat="server" size="50" onkeydown="return false;"
                                        onchange="ValidateDisplayName()" />

Open in new window

0
 
LVL 3

Expert Comment

by:sajain84
ID: 22719686
Hey!

What I gather is that you want to be informed when a file is selected or not.
The code you are using will not work because that gets fired only when there IS a postback.

You can do this using javascript.
You can detect whether a file has been selected or not by adding the following line of code on your Page_Load

Put the Submit button in a div and keep the div hidden by default.

Hope this helps. If not, ping me back.

// Put this on Page_Load
FileUpload1.Attributes.Add( "onchange", "return ShowButton()"); 
 
// Put this in the aspx page for the submit button
<div id="divButton" style="display:none">
<asp:Button ID="btnSubmit" runat="Server" Text="Submit">
</div>
 
// The javascript code which can toggle the button - Again needs to go in the aspx page
 
function ShowButton() {
var button = document.getElementById('divButton');
button.style.display = "";
}

Open in new window

0
 

Author Comment

by:dwezil
ID: 22719769
It doesnt like div button for some reason error is:

Too many characters in character literal

plus Ive put the java in my cs page because it wouldnt work in the aspx. You sure it goes there?

Thank you!
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 3

Expert Comment

by:sajain84
ID: 22719986
Okay.
My code is as follows which I have tested to be working.

Please check it out and let me know if it helps.
You will need to modify your code to look like mine.

// The ASPX file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testjs.aspx.cs" Inherits="testjs" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    
    <script type="text/javascript">
        function ShowButton() {
            var box = document.getElementById('divButton');
            box.style.display = "";
        }
            
    </script>
    
</head>
<body style="background-color:White">
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fupDemo1" runat="server" />
        
        <div id="divButton" style="display:none">
            <asp:Button ID="btnDemo" runat="server" Text="Click me" />
        </div>
    </div>
    </form>
</body>
</html>
 
 
// The CODE BEHIND FILE
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class testjs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        fupDemo1.Attributes.Add("onchange", "return ShowButton()");
    }
}

Open in new window

0
 

Author Comment

by:dwezil
ID: 22720580
Ok well that works but it does the opposite of what I want. If there is text in the box i want the button to be invisble. So when the file is attached and the text is cleared the button will appear. Plus I want the button there from load.

Thank you!
0
 
LVL 3

Accepted Solution

by:
sajain84 earned 2000 total points
ID: 22720639
Oh. I'm sorry - I misunderstood your question.

You could just reverse the code in that case.
I've attached the corrected code snippet below.

Do let me know if you need some more help.

// The ASPX file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testjs.aspx.cs" Inherits="testjs" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    
    <script type="text/javascript">
        function HideButton() {
            var box = document.getElementById('divButton');
            box.style.display = "none";
        }
            
    </script>
    
</head>
<body style="background-color:White">
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fupDemo1" runat="server" />
        
        <div id="divButton" style="display:">
            <asp:Button ID="btnDemo" runat="server" Text="Click me" />
        </div>
    </div>
    </form>
</body>
</html>
 
 
// The CODE BEHIND FILE
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class testjs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        fupDemo1.Attributes.Add("onchange", "return HideButton()");
    }
}

Open in new window

0
 

Author Comment

by:dwezil
ID: 22720683
Its all good I sorted it out. Thank you for you help
0
 

Author Comment

by:dwezil
ID: 22720696
Yeah i did the same as what youv'e just posted, cheers!
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question