FileUpload Null reference error - c#

I want to use FileUpload Control to save Image to Database. The HasFiles return always false after I click on BtnSave. I tried with and without UpdatePanel but could not find the solution.
My code is:
protected void BtnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
var ChNumber = txtNumber.Text;
string img = this.FileUpload1.PostedFile.FileName;
//Save image to database function
}
else
{
lblStatus.Text="Image not Uploaded";
}
}

Try with a simple form without UpdatePanel. Here's the markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="BtnSave" runat="server" Text="Button" OnClick="BtnSave_Click" />
<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:Label ID="lblStatus" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
And here's the code:
using System;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
var ChNumber = txtNumber.Text;
string img = this.FileUpload1.PostedFile.FileName;
//Save image to database function
}
else
{
lblStatus.Text = "Image not Uploaded";
}
}
}
I set a brekapoint at if (FileUpload1.HasFile == true) and see the uploaded file is there.

Related

I can't refer to textBox checkBox etc

I am trying to refer to object from .cshtml file in .cs file but it doesn't work. It says the name doesn't exist.
cshtml.cs file
public class ParticipateModel : PageModel
{
public string Message { get; set; }
public void OnGet()
{
Message = "Your application description page.";
}
protected void sendDataToKonkurs(object sender, EventArgs e)
{
string name = txtName.Text;
}
cshtml file
<input type="text" class="form-control" id="txtName" maxlength="20" formmethod="post" style="margin-bottom:20px">
this example might help you front end code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="DefaultControls.aspx.cs"
Inherits="ControlsMix.DefaultControls" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="CbClientName" runat="server" Width="300px">
</asp:DropDownList>
</div>
</form>
backend c# code which loads the values into combo box
protected void Page_Load(object sender, EventArgs e)
{
CbClientName.Items.Add("John");
CbClientName.Items.Add("Smith");
CbClientName.Items.Add("Dylan");
CbClientName.Items.Add("Ben");
}
just ensure your id="whatevername" is the same in the backend of the code this binds the control in asp.net

How Am i supposed to bind my variable to a img attribute in aspx page

<%=Eval(src) %> is not working.
<img id="Img1" src="<%=Eval( src) %>" runat="server" alt="" />
Code Behind
public partial class image_from_cb : System.Web.UI.Page
{
public string src="";
protected void Page_Load(object sender, EventArgs e)
{
src = "~/final.jpg";
}
}
I know i can do this with Attribute.Add or with ASPIMAGE , but i was just wondering why <% %> is not working
As You are using runat="server", You can use
protected void Page_Load(object sender, EventArgs e)
{
src = "~/final.jpg";
Img1.src = src;
}
Or if you want to use your src variable
<img id="Img1" src='<%= src >%' alt="" />
Try it
You can do this way
html
<img id="Img1" runat="server" alt="" />
C#
protected void Page_Load(object sender, EventArgs e)
{
Img1.src= "~/final.jpg";
}
And Another way is
try this Page.DataBind(); in Page load event
Look at my sample code html
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img id="Img1" src="<%# src.ToString() %>" runat="server" alt="" />
<asp:Image runat="server" ID="myImage" ImageUrl="<%# src %>" />
</div>
</form>
</body>
</html>
and code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public string src = "";
protected void Page_Load(object sender, EventArgs e)
{
src = "~/1455094_265560366930522_537876922_n.jpg";
//Add this
Page.DataBind();
}
}
}
Easy coding !!The both ways working in my side

MultiView + UpdatePanel = NotWorking

I am implementing a multiview using a RadioButtonList to flip its view and also AJAX.
My problem is if I set my default RadioButton (NavigateRadioButtonList.Items[0].Selected = true;), that RadioButton would become dead, ie. when I focus on it, the MultiView doesn't response at all.
This only happens in AJAX. Normal asp page doesn't have that problem.
I am at my wit's end. Can some expert help??
The code is as follow
<%# Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Init(object sender, EventArgs e)
{
for (int i = 1; i < 8; i++)
{
string RadioText = "View" + i.ToString();
NavigateRadioButtonList.Items.Add(RadioText);
Literal DescriptionTag = new Literal();
DescriptionTag.Text = "Text" + i.ToString();
View NewView = new View();
NewView.Controls.Add(DescriptionTag);
MultiViewMenu.Views.Add(NewView);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
NavigateRadioButtonList.Items[0].Selected = true;
MultiViewMenu.ActiveViewIndex = 0;
}
}
protected void ChangeView(object sender, EventArgs e)
{
MultiViewMenu.ActiveViewIndex = NavigateRadioButtonList.SelectedIndex;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:RadioButtonList ID="NavigateRadioButtonList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ChangeView"></asp:RadioButtonList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:MultiView ID="MultiViewMenu" runat="server">
</asp:MultiView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="NavigateRadioButtonList" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>

Cannot Unregister UpdatePanel exception with Cross Page Posting

I am getting the dreaded exception "Cannot unregister UpdatePanel with ID 'UpdatePanel' since it was not registered with the ScriptManager" when trying to combine a popup window and some Cross-Page Posting. I've been able to reproduce the problem with a small example.
I have seen other questions that handle the Unload event for the UpdatePanel, but that doesn't seem to work with the addition of the Cross Page Posting.
How do I get around this issue?
To duplicate the issue with the code below:
Start the Setup page
Press the View Popup link
Close the popup
Press the Next button to transfer to the Target page
Press the Back button to transfer to the Setup page
Press the View link and close the popup
Press the Next button, and see the failure
Here are the highlights (forgive the length, but it should be cut-and-pastable):
Setup.aspx
<head runat="server">
<title>Setup </title>
<script type="text/javascript">
function openCenteredWindow(url, height, width, name, parms) {
//Snip setting up window location stuff
var win = window.open(url, name, winParms);
return win;
}
</script>
</head>
<body>
<h1>Setup Page</h1>
<form id="aspnetForm" runat="server">
<div>
<asp:TextBox ID="txtData" runat="server" Width="80%" Text="<%# Information %>" />
<br />
<asp:LinkButton ID="lbViewData" runat="server"
OnClientClick="aspnetForm.target='ViewDataPopup';"
Text="View In Popup" /> <br />
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" />
</div>
<div>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:updatepanel ID="UpdatePanel" runat="server" OnUnload="UpdatePanel_Unload"></asp:updatepanel>
</div>
</form>
</body>
Setup.Aspx.cs
public partial class Setup : System.Web.UI.Page
{
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (this.ViewState["Information"] != null)
_Information = this.ViewState["Information"].ToString();
}
protected override object SaveViewState()
{
this.ViewState["Information"] = _Information;
return base.SaveViewState();
}
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage is TargetPage)
{
_Information = ((TargetPage)PreviousPage).SetupData;
}
else if (!Page.IsPostBack)
{
_Information = String.Format("This test started at {0}", DateTime.Now);
}
Page.DataBind();
lbViewData.PostBackUrl = "ViewData.aspx?u=0";
lbViewData.Attributes.Add("onclick", "JavaScript:openCenteredWindow(\"ViewData.aspx?u=0\", 400, 300, \"ViewDataPopup\", 'scrollbars=yes');");
}
private string _Information;
public string Information
{
get { return _Information; }
}
protected void btnNext_Click(object sender, EventArgs e)
{
HttpContext.Current.Server.Transfer("~/TargetPage.aspx");
}
}
ViewData.aspx
<%# PreviousPageType VirtualPath="~/Setup.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
<title>View Data</title>
<script type="text/javascript">
function fixForm() {
opener.document.getElementById("aspnetForm").target = "";
opener.document.getElementById("aspnetForm").action = "";
}
</script>
</head>
<body onload="fixForm()">
<form id="aspnetForm" runat="server">
<div>
<h2 >View Data</h2>
</div>
<asp:Label ID="lblData" runat="server" Text="<%# SetupData %>" />
</form>
</body>
ViewData.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
Setup setup = (Setup)PreviousPage;
this.SetupData = setup.Information ?? "Data not set in Setup Page";
}
Page.DataBind();
}
private string _setupData = "Did not get updated data";
protected string SetupData
{
get { return _setupData; }
set { _setupData = value; }
}
TargetPage.aspx
<%# PreviousPageType VirtualPath="~/Setup.aspx" %>
<body style="background-color: #9999BB">
<h1>Target Page</h1>
<form id="aspnetForm" runat="server">
<div>
<asp:Label ID="lblData" runat="server" Text="<%# SetupData %>" /><br />
<asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
</div>
</form>
</body>
TargetPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (this.PreviousPage == null)
Response.Redirect("Setup.aspx");
this.SetupData = PreviousPage.Information;
}
Page.DataBind();
}
public string SetupData
{
get { return ViewState["SetupData"].ToString(); }
set { ViewState["SetupData"] = value; }
}
protected void btnBack_Click(object sender, EventArgs e)
{
HttpContext.Current.Server.Transfer("~/Setup.aspx");
}
As you can see, it's enough to have the UpdatePanel just sitting on the page and doing nothing for this error to occur.

clear FileUpload object on c#

i"m using asp.net FileUpload , after user input file he click on save button
in the c# i have this function
protected void btnUploadImages_Click(object sender, EventArgs e)
{
SaveImages(FileUpload1, "", returnAlbumId, out returnPhotoId);
}
this function save the image from the FileUpload1 so far all work as it"s should
but after the postback when i push the refresh button on the page i"m go to this function again , SaveImages function save the same image again .
the FileUpload1 didn't clear after the postback
thanks
Even i got the Same Problem I have resolved like Below.
After uploading the File If you Redirect to same page or some other page in your project. After Redirection Response will not be there once you redirected.
In My ASPX
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebApplication.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
In My Code Behind
public partial class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~");
path = path + FileUpload1.FileName;
FileUpload1.SaveAs(path);
Response.Redirect("WebForm.aspx"); // Responce will be cleared. This Redirection will do the Trick
//Put the debugger and check it will work
}
}
Here, to show the success and error messages try to use sessions.

Categories

Resources