asp.net FileUpload doesn't work properly - c#

I have asp:FileUpload , two asp:Button , and GridView in UpldatePanel.
<asp:FileUpload runat="server" ID="fileExcelUpload" />
<asp:Button ID="btnShow" runat="server" Width="80px" OnClick="btnShow_Clicked"
AutoPostBack="true" Text="Save"/>
<asp:Button ID="btnImport" runat="server" Width="80px" OnClick="btnImport_Clicked"
AutoPostBack="true" Text="Save"/>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
.........GridView...........
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnShow" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnImport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Firstly , I retrieve some data from database and bind to gridView using btnShow.It works perfectly.
But when I upload Excel file with asp:FileUpload and click btnImport ,
protected void btnImport_Clicked(object sender, EventArgs e)
{
if (fileUExcelUpload.HasFile)
{
.....
}
}
the boolean value fileUExcelUpload.HasFile return False .
But when I remove <asp:AsyncPostBackTrigger ControlID="btnImport" EventName="Click" /> from UpldatePanel's triggers , it return True .
(The reason why I add btnImport's Click event to UpdatePanel's Triggers is , I want to persist Uploaded File of asp:FileUpload after postback . )
Is there any right way to do it ? Kindly help me Please :) Thanks !

Add this line in the
<Triggers> </Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
here btnUpload is the id of the fileupload control

Related

update panel not working in where upload excel sheet

If i am using update panel then PostedFile not giving file path some error are coming and if update panel are removed the page then excel file successfully upload how to solve
your code should be as shown below
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload runat="server" ID="flFile" />
<asp:Button runat="server" ID="btnSubmit" Text="Upload" OnClick="btnSubmit_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
You need to put postback trigger if you are uploading any files.
A FileUpload inside UpdatePanel won't work. You have to post the whole page. You do this by adding a PostBackTrigger to the button you use for uploading files.
Something like this (see Triggers):
<asp:UpdatePanel ID="upnlMain" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="fileUpload" runat="server" Width="400px" />
<asp:Button ID="btnUploadFiles" runat="server" Text="Upload files" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUploadFiles" />
</Triggers>
</asp:UpdatePanel>
If you have problems with not posting the first time a button is clicked then add this to Page Load event:
Page.Form.Attributes.Add("enctype", "multipart/form-data");

FileUpload.hasFile returning false inside update panel

I have been through several links but didn't find anything helpful. I know it has been asked several times here.
Here is my frontend code
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fuItemImage" Width="370px" TabIndex="12" />
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
Here is the backend code
if (fuItemImage.HasFile)
{
MyFunction.UploadThisFile(anything)
}
When I upload any image and click on the save button it shows false in FileUpload.HasFile. I am stuck and finding no solution for it.
Any help would be appreciated.
you can try this
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fuItemImage" Width="370px" TabIndex="12" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID = "Button1" />
</Triggers>
</asp:UpdatePanel>
add the button id in triggers to upload the file which will do postback

How to active AsyncPostBackTrigger in Update Panel?

I created a updatePanel, is include some dropdownlist and a few button and I use AsyncPostBackTrigger for not update all page when the click button or select dropdownlist.
My page structure that an aspx page and also use user control page. I use updatePanel on UserControlPage.
Here is My code and I wish you help me:
<asp:UpdatePanel ID="UpdMonthly" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlSenetIslemleriAlinanUrun1" AutoPostBack="true" OnSelectedIndexChanged="ddlSenetIslemleriAlinanUrun1_SelectedIndexChanged" runat="server"></asp:DropDownList>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnKaydet" style="margin:10px ;" Width="100" Height="50" runat="server" Text="Kaydet" OnClick="btnKaydet_Click"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlSenetIslemleriAlinanUrun1" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnKaydet" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
I have some knowledge on placeholder but when the click dropdown or button, page is postback and placeholder knowledge dissappear.
how to fix postback problem?
Thanks for your answers.

How to fire an event of a page in Async mode a button inside an UpdatePanel inside a UserControl?

EDITED...
I got an ASP.NET Button inside an UpdatePanel, inside a UserControl.
I want to make the IMAGE visible by CLICKing on the UserControl’s Button (btnSubmit).
Using the following js function I’m able to do it in normal postback mode, but it’s not working in Async mode.
The question is, how can I do this in an Async mode or what’s the best way to do this?
User Control (Categories):
<asp:UpdatePanel ID="upItems" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="btnSubmitPostBack();"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
ASP.NET Page
<script>
function btnSubmitPostBack() {
__doPostBack('<%= btnDoSomething.ClientID %>', '');
}
</script>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc1:Categories ID="Categories1" runat="server" CatRootName="Products" />
<asp:Button ID="btnDoSomething" CssClass="hidden" runat="server" Text=" Do Something" OnClick=" btnDoSomething_Click" />
<asp:Image ID="Image1" runat="server" Visible="false" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnDoSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void btnDoSomething_Click(object sender, EventArgs e)
{
Image1.Visible = true;
up.Update();
}
Thanks for your attention and help in advance!
OK here we go :( Just compiled and ran this successfully) and I think that's the right way to do it: ( the key here is to find the other user control and drill inside it for your control to update:
You user control code :
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Categories.ascx.cs" Inherits="WebApplication1.Categories" %>
<asp:UpdatePanel ID="upItems" runat=*emphasized text*"server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
User control code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
UpdatePanel ImageUpdatePanel = (UpdatePanel)this.Parent.FindControl("up");
Image _img = (Image)ImageUpdatePanel.FindControl("Image1");
_img.Visible = true;
//Updating UpdatePanel
ImageUpdatePanel.Update();
}
and then your page code
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc1:Categories ID="Categories1" runat="server" CatRootName="Products" />
<asp:Button ID="btnDoSomething" CssClass="hidden" runat="server" Text=" Do Something"/>
<asp:Image ID="Image1" runat="server" Visible="false" ImageUrl="~/Images/heroAccent.png" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnDoSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
this is 100% working solution, if u need I can send u the code too :)

Refresh gridview inside update panels

i have a page which is inside update panel.it contains two gridview which are also inside update panels.i m binding them in page load inside !ispostback as well as other parts of code.when i bind only first grid it works fine but when i bind the second grid it refreshes the first one and it shows no data.is this the problem of improper usage of update panel usage or any other issue.. how can i use the triggers here.
my code is as follows..
<asp:UpdatePanel ID="updgrd1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" Width="85%"ShowHeaderWhenEmpty="true"EmptyDataText="No Records Found" AutoGenerateColumns="false"OnRowCancelingEdit="GridView1_RowCancelingEdit"OnRowEditing="G dView1_RowEditing">
<Columns>
//my code here
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpdate" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="updgrd2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" Width="85%" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false" EmptyDataText="No Records Found">
<Columns>
//my code here
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpdate" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
LoadGrid1();
LoadGrid2();
}
}
Here I'm doing exactly what you are. Here I have a textbox and a gridview, when clicking add whatever is in the textbox gets added to the gridview.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger controlid="LnkAddTrack" eventname="Click" />
</Triggers>
<ContentTemplate>
<div id="EventTrack" >
<asp:Label ID="lblEventTracks" runat="server" Text="Event Tracks"></asp:Label>
<asp:TextBox ID="txtEventTracks" CssClass="EventTextbox" runat="server"></asp:TextBox>
<asp:LinkButton ID="LnkAddTrack" ClientIDMode="Static" runat="server" OnClick="LnkAddTrack_Click">Add Track</asp:LinkButton>
</div>
<asp:GridView ID="dgTracks" runat="server" >
<Columns>
<asp:TemplateField HeaderText="TrackName">
<ItemTemplate>
<asp:Label ID="Control" runat="server" Text='<%# Eval("TrackName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Then make sure you on your button event to add new data you are adding the data correctly
and remember to rebind the gridview.
protected void LnkAddTrack_Click(object sender, EventArgs e)
{
InsertTrack();
DgPopTracks();
}
Ask any questions you may have, if you want to see my Insert Track and DgPopTracks events
i can post them as well for you

Categories