FileUpload.HasFile() always null - c#

This is the code I use for my UpdatePanel.
The FileUploadAsync.HasFile() is always null.
I am wondering what is wrong with my asp.net page ...
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="Server" />
<asp:UpdatePanel ID="UpdatePanelAddFiles" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LkUpload" />
</Triggers>
<ContentTemplate>
<asp:LinkButton ID="LkUpload" runat="server" OnClick="LkUpload_Click" Visible="false">Upload</asp:LinkButton>
<ajaxToolkit:AsyncFileUpload runat="server" ID="FileUploadAsync" />
</ContentTemplate>
</asp:UpdatePanel>

Have you tried taking the fileupload outside the update panel? I've had that problem before.
I got this to work, try it out and let me know.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="futest.aspx.cs" Inherits="erpweb.futest" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="smTest" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upTest" runat="server">
<ContentTemplate>
<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" runat="server"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
codebehind:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsyncFileUpload1.HasFile)
{
//do save process here
}
}

Related

Upload file control not showing any file in the code behind

I have a very simple application. I am trying to upload the file using File upload control of ASP.net. Below is my entire .cs code and .aspx code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestFileUpload1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void uploadFile_Click(object sender, EventArgs e)
{
if (UploadImages.HasFiles)
{
foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
{
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), uploadedFile.FileName));
//listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
}
}
}
}
}
My .aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestFileUpload1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test web site</title>
<script src="Scripts/jquery-1.11.0.js"></script>
<script src="Scripts/jquery.mobile-1.4.5.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
<asp:Label ID="listofuploadedfiles" runat="server" />
</div>
</div>
</form>
</body>
</html>
whenever I try to upload a file, I get "False" for UploadImages.HasFiles.
Above is full working example.
As soon as I remove one of these script tags :
<script src="Scripts/jquery-1.11.0.js"></script>
<script src="Scripts/jquery.mobile-1.4.5.js"></script>
my code starts working and I get "true" for UploadImages.HasFiles when I try to upload a file.
I am using .net framework 4.7.2
I need to keep these two script tags in my code because of the GUI and this is an old application where these tags are used in all the pages.
I also tried to wrap the control in a update panel and that didn't work either. below is the changed .aspx page. although, I want my original code to work. I don't want to use ajax, but I just tried to use it because it is suggested as one of the solution
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestFileUpload1.WebForm1" %>
<%--<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>--%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test web site</title>
<script src="Scripts/jquery-1.11.0.js"></script>
<script src="Scripts/jquery.mobile-1.4.5.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="uploadedFile" />
</Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
<asp:Label ID="listofuploadedfiles" runat="server" />
</div>
</div>
</form>
</body>
</html>
below is the image of false value that I am getting in code behind:
Any help will be highly appreciated.
All I had to do is put data-ajax ="false" in form tag and that fixed the issue.
<form id="form1" runat="server" data-ajax ="false">
Use a update panel and wrap your controls inside it. Then add the button controlID as a trigger (be sure its a PostBackTrigger) to the update panel. To test be sure to put a break point on the uploadFile_Click event so you can step through and see the values..
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="btnSignaureOfRequestor" />
</Triggers>
<ContentTemplate>
<table>
<tr> <td class="tdText" colspan="4">
<asp:FileUpload ID="fileUpload" runat="server" Width="50%" />
</td>
</tr>
<tr>
<td> <asp:Button ID="btnSignaureOfRequestor" runat="server" Text="Submit Request" Visible="true" OnClientClick="return confirm('Are you sure you want to continue?');" OnClick="btnSignaureOfRequestor_Click" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Try the below, removed allow multiple and add update mode conditional (see confirm case/syntax of these changes as I'm just writing it in notepad)
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" updateMode="Conditional">
<ContentTemplate>
<asp:FileUpload runat="server" ID="UploadImages" />
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="uploadedFile" />
</Triggers>
</asp:UpdatePanel>
<asp:Label ID="listofuploadedfiles" runat="server" />
</div>
</div>

How to use Asp.Net Ajax UpdatePanel inside an usercontrol

I have a very simple user control like this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="tebimir.sections.WebUserControl1" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And I have a page ajax.aspx like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ajax.aspx.cs" Inherits="tebimir.ajax" %>
<%# Register Src="~/sections/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<uc1:WebUserControl1 runat="server" ID="WebUserControl1" />
</div>
</form>
</body>
</html>
And this is Button1 click code:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
I want to update my label text to current date time without refreshing page but every time i click on button page is refreshed. why?
Do you have any other triggers in your user control because I don't see a closing tag for </asp:UpdatePanel>.I've created a brand new ASP.NET web forms web application and created the following user control and it works just fine(without refresh):
User control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="StackOverflowClean.WebUserControl1" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Page that calls the user control:
<%# Register Src="~/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<uc1:WebUserControl1 runat="server" id="WebUserControl1" />
</div>
</form>
</body>
</html>

Update Panel not working properly causing full postback

I have moved to visual studio 4.5 and at first my update panels worked just fine but one day they just seemed to stop altogether even if I start a new project.
Code Behind:
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = "Post Back";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Testl.Text = DateTime.Now.ToString();
UpdatePanel1.Update();
}
}
Page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Label1" runat="server" ></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="Testl"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
even this code causes a post back and I cannot for the life of me figure out why. Here are my references in case I may be missing one I do not realize.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
I have it running in a scriptmanager in my masterpage.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="UFODataRepositoryWebApplication.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head id="Head1" runat="server">
<meta charset="utf-8" />
<title></title>
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server" id="MainForm">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<header>
<div class="content-wrapper">
<div class="float-right">
<div id="MainLogin_Div">
<div class="fb-login-button" data-show-faces="true" data- width="400" data-max-rows="1"></div>
<asp:UpdatePanel ID="MainLogin_UpdatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Panel ID="UnLoggedMain_Panel" runat="server" Visible="true">
<p>
<asp:LinkButton runat="server" PostBackUrl="Login.aspx" CssClass="NotLoggedMenu"
ID="MainLogin_Link">Login</asp:LinkButton>
<span style="color: #7E7F7F;">or </span>
<asp:LinkButton runat="server" PostBackUrl="Register.aspx" CssClass="NotLoggedMenu"
ID="MainRegister_Link">Become a member</asp:LinkButton>
</p>
</asp:Panel>
<asp:Panel ID="LoggedMain_Panel" ClientIDMode="Static" runat="server" Visible="false">
<span style="color: #CACCCB;">Welcome: </span>
<asp:LinkButton ID="Welcome_LinkButton" ClientIDMode="Static" runat="server" PostBackUrl="~/UserPanel.aspx"></asp:LinkButton>
<asp:LinkButton ID="MainLogout_LinkButton" runat="server" OnClick="MainLogout_User"> Logout</asp:LinkButton>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ul>
</div>
</div>
</header>
<div id="body">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</div>
</form>
</body>
</html>
So that should be literally all the code I have. I intentionally took out the inherits at the top of the page for this post.

Modal Popup Extender auto closing after .Show()

I am using ModalPopupExtender from AjaxToolkit for asp.net. I am trying to have a ModalPopupExtender triggered with different buttons. The problem is that unless I am using the TargetControlID the popup opens and quickly closes within under a second. I need this popup to be accessible
by several different buttons with the same panel being used everytime.
The code below should replicate the problem nicely, on my actual application it almost works fine. Even content being updated with the chosen panel of the popup except that it closes after about 1/2 sec when i call .show() from OnClientClick;
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
//Function to Hide ModalPopUp
function Hidepopup() {
$find('AjaxPopupHi').hide();
}
//Function to Show ModalPopUp
function Showpopup() {
$find('AjaxPopupHi').show();
}
</script>
</head>
<form id="form1" runat="server">
<asp:LinkButton ID="lnk" OnClientClick = "Showpopup()" runat="server" Text="hi"></asp:LinkButton>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:Button ID="Button_dummy" Style="display: none" runat="server" Text="Button" />
<ajaxToolKit:ModalPopupExtender ID="mpe" runat="server" BehaviorID="AjaxPopupHi" TargetControlID="Button_dummy" PopupControlID="pnl"
CancelControlID="close" />
<!--BELOW panel does not remain OPEN :/-->
<asp:Panel ID="pnl" runat="server" CssClass="popupPanel">
<div>
Hi!!!
</div>
<asp:Button ID="close" runat="server" Text="Close" />
</asp:Panel>
</form>
Thanks
You can use like this
OnClientClick = "return Showpopup()"
function Showpopup() {
$find('AjaxPopupHi').show();
return false;
}
You must use return in you OnClientClick combining your code
<asp:LinkButton ID="lnk" OnClientClick = "return Showpopup()" runat="server" Text="hi">
</asp:LinkButton>
and your javascript function should be like
function Showpopup() {
$find('AjaxPopupHi').show();
return false;
}
It is very late but lets hope this helps someone who is looking for the answer.
When you click, a postback occurs and the page reloads. If you use updatepanel then your popup message will remain after page load. See this example below:
Aspx :
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Popup.aspx.cs" Inherits="Popup_example_Popup" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
//Function to Hide ModalPopUp
function Hidepopup() {
$find('AjaxPopupHi').hide();
}
//Function to Show ModalPopUp
function Showpopup() {
$find('AjaxPopupHi').show();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="udpOutterUpdatePanel" runat="server">
<ContentTemplate>
<asp:LinkButton ID="lnk" OnClientClick="Showpopup()" runat="server" Text="hi"></asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Button ID="Button_dummy" Style="display: none" runat="server" Text="Button" />
<ajaxToolkit:ModalPopupExtender ID="mpe" runat="server" BehaviorID="AjaxPopupHi" TargetControlID="Button_dummy" PopupControlID="pnl"
CancelControlID="close" />
<asp:Panel ID="pnl" runat="server" CssClass="popupPanel">
<div>
Hi!!!
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="close" runat="server" Text="Close" OnClick="close_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</form>
</body>
</html>
Code behind :
using System;
public partial class Popup_example_Popup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void close_Click(object sender, EventArgs e)
{
mpe.Hide();
}
}

Can some one help me regarding progress bar

I used this example from here
http://www.asp.net/ajaxlibrary/jqueryui_progressbar.ashx
Every thing works fine except the progress
This is my design
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default15.aspx.cs" Inherits="Default15" %>
<!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></title>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css"
rel="Stylesheet" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js"></script>
<script type="text/javascript">
$("#progress").progressbar({
value: document.getElementById('<%=litprogress.ClientID%>').value
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Wizard progress</h1>
<div id="progress">
<asp:Literal ID="litprogress" runat="server"></asp:Literal>
</div>
<asp:Panel ID="step1" runat="server">
<h1>
Step 1</h1>
<asp:Button ID="GoToStep2" Text="Next" runat="server" OnClick="GoToStep2_Click" />
</asp:Panel>
<asp:Panel ID="step2" runat="server">
<h1>
Step 2</h1>
<asp:Button ID="GoToStep3" Text="Next" runat="server" OnClick="GoToStep3_Click" />
</asp:Panel>
<asp:Panel ID="step3" runat="server">
<h1>
Step 3</h1>
<asp:Button ID="GoToStep4" Text="Next" runat="server" OnClick="GoToStep4_Click" />
</asp:Panel>
<asp:Panel ID="step4" runat="server">
<h1>
Completed</h1>
</asp:Panel>
</div>
</form>
</body>
</html>
Instead of required I am getting this
So can any one tell where I went wrong
Getting some warnings regarding CSS and the error is as follows
Error: document.getElementById("litprogress") is null
I think that the progress bar init is happening before the DOM is fully loaded, put the code inside $(document).ready.

Categories

Resources