where error? if i dont use ajax, then form is valid, and file is uploading, but now page reload and...file not load in path, why?
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<p>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Upload" Text="downdoad..."
BackColor="#0099CC" Font-Size="Small" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
downdoad...
</ProgressTemplate>
</asp:UpdateProgress>
Fileupload doesn't work in an updatepanel,
you have a workaround on this url
http://www.codeproject.com/useritems/simpleajaxupload.asp
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/Uploads" + FileUpload1.FileName));
Label1.Text = "Saved Successfully";
}
else
{
Label1.Text = "File Not found";
}
}
In the above code file does not Add in Upload Folder
Related
I want to create a C# WebForm with a ASP.NET FileUpload Control and some Textboxes and a submit button. Idea: user selects a file, enters some data, on submit the form checks the data and if valid, it saves the file on the server otherwise an error message is displayed. There are so many posts about UpdatePanel Triggers etc. but not a working solution.
Here my code behind:
protected void Page_Load(object sender, EventArgs e) {
// for FileUpload-Control outside UpdatePanel
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
protected void Button1_Click(object sender, EventArgs e) {
bool valid = true;
string errorMessage = DateTime.Now.ToLongTimeString() + ": ";
if (this.TextBox1.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox1<br/>";
}
if (this.TextBox2.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox2<br/>";
}
if (this.TextBox3.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox3<br/>";
}
if (!this.FileUpload3.HasFile) {
// is alway false!
errorMessage += "Missing FileUpload3<br/>";
}
if (valid) {
// never fires, because .HasFile is always false
this.Label1.Text = "valid!";
// do upload stuff
this.FileUpload3.SaveAs("foobar");
} else {
this.Label1.Text = errorMessage;
}
}
And here is my sample ASPX:
<form id="Upload" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Does anyone see, why my FileUpload is always empty, although it's outside the UpdatePanel and I do have the required line in the Page_Load() event? If, could you adjust the code?
Thank you
SiS
<form id="Upload" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
I am trying to display multiple videos. The videos are stored in a blob, but anytime i run the code it only display the first video from the blob where the videos are stored. Could anyone help me with this issue.
<div>
<asp:UpdatePanel ID="_video" runat="server">
<ContentTemplate>
<asp:ListView ID="VideoDisplayControl" runat="server">
<ItemTemplate>
<video width="320" height="240" controls="">
<source ID="videoDisplay" src='<%# Eval("Url") %>' type="video/mp4">
</video>
<asp:Literal ID="label" Text='<%# Eval("Title") %>' runat="server" />
</ItemTemplate>
</asp:ListView>
<asp:Timer ID="timer1" runat="server" Interval="20000" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
This method is the code behind rendering. The videogenerator is the name of the blob folder that contains the vidoes.
protected void Page_PreRender(object sender, EventArgs e)
{
try
{
VideoDisplayControl.DataSource = from o in GetVideoGalleryContainer().GetDirectoryReference("videogenerator").ListBlobs()
select new { Url = o.Uri };
VideoDisplayControl.DataBind();
}
catch (Exception)
{
}
}
I have used ajax control in asp.net c# application
<div class="uploadfiles">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="file_upload" class="multi" runat="server" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click"/>
<asp:Label ID="lblMessage" runat="server" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
in .cs file
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpFileCollection fileCollection = Request.Files;
for (int i = 0; i < fileCollection.Count; i++)
{
HttpPostedFile uploadfile = fileCollection[i];
string fileName = Path.GetFileName(uploadfile.FileName);
if (uploadfile.ContentLength > 0)
{
uploadfile.SaveAs(Server.MapPath("~/UploadFiles/") + fileName);
lblMessage.Text += fileName + "Saved Successfully<br>";
HyperLink dynamHyperLink = new HyperLink();
dynamHyperLink.Text = fileName.ToString() + "Saved Successfully<br>";
// hyperlnk.Attributes.Add("href", Server.MapPath("/UploadFiles/") + fileName);
dynamHyperLink.NavigateUrl = "~/UploadFiles/" + fileName;
Panel1.Controls.Add(dynamHyperLink);
//lblMessage.Text= ""+fileName+"";
}
}
}
When I Click on upload button it's not executing the btnupload_Click() event and doesn't display uploaded files in Panel1 control.
Please help me!!!
use <asp:PostBackTrigger ControlID="btnUpload" /> instead of AsyncPostBackTrigger on your code.
Take FileUpload
<asp:FileUpload ID="file_upload" class="multi" runat="server" />
outside the update panel and add EventName parameter to this
<asp:AsyncPostBackTrigger ControlID="btnUpload" />
like this
<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />
I have a form where users upload their picture to the registration form.. everything worked fine until i added an updatepanel and an Image control.
I placed an update panel, inside it i place a table with Fileupload control and an image control. once the button is clicked the image is uploaded to the images folder and the preview is displayed in image control.
Before making the modification it worked fine.. but now im unable to read the FileUpload1.Filename .. below is the code
Reg.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table> <tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload1" runat="server" onclick="btnUpload1_Click"
Text="Upload" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<Td>
<asp:Image ID="Image1" runat="server" Width="147px" /></td></tr>
</table>
Reg.aspx.cs
protected void btnUpload1_Click(object sender, EventArgs e)
{
Label1.Text = FileUpload1.FileName;
try
{
var folder = Server.MapPath("~/UploadImages/");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
Label1.Text = Label1.Text + " Directory Created <br>";
}
var picUrl = Server.MapPath("~/UploadImages/"+ FileUpload1.FileName.ToString());
if (CheckFileName(picUrl) == true)
{
FileUpload1.SaveAs(picUrl);
Image1.ImageUrl = picUrl.ToString();
Label1.Text = Label1.Text + "Image Uploaded";
}
else
Label1.Text = Label1.Text +"File already exists";
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
private bool CheckFileName(string picUrl) {
if (File.Exists(picUrl))
return false;
return (true);
}
Now im not able to read the filename from fileupload1 control..
Please help
Add Postback Trigger in your update panel. Have a look at below code sample
ASPX:
<asp:UpdatePanel runat="server" ID="updatePanel1">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fileUploader"/>
<asp:Button runat="server" ID="buttonSubmit" OnClick="buttonSubmit_OnClick" Text="Submit"/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="buttonSubmit"/>
</Triggers>
</asp:UpdatePanel>
ASPX.cs
protected void buttonSubmit_OnClick(object sender, EventArgs e)
{
var fileName = this.fileUploader.FileName;
}
My code works fine when used in a normal webform. but when I use it in a webform using masterpages, It doesn't work.
page header : ~/Manager/BaseManager.master
and some nested master pages : Base.master > Pages.master > BaseManager.master
ASP
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger EventName="Click"
ControlID="btnUpdateEditPage" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnUpdateEditPage" CssClass="btnUpdateEditPage" runat="server"
Text="Button" OnClick="btnUpdateEditPage_Click" />
C#
protected void btnUpdateEditPage_Click(object sender, EventArgs e)
{
lblTest.Text += "**";
}
Do the following please:
1- Add UpdatePanel1.Update(); like the following:
protected void btnUpdateEditPage_Click(object sender, EventArgs e)
{
lblTest.Text += "**";
UpdatePanel1.Update();
//Your UpdatePanel should be UpdateMode="Conditional" as what you have now..
}
2- Put the button inside the update Panel
3- Remove the Trigger to not fire a post back, so your code has to be like:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnUpdateEditPage" CssClass="btnUpdateEditPage" runat="server"
Text="Button" OnClick="btnUpdateEditPage_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Place the button inside UpdatePanel.
SOLVED.
I change
<form id="form1" runat="server" action="post">
to
<form id="form1" runat="server">
and my problem solved.
But why?!