File upload control inside update panel - c#

I have an update panel, in that update panel I have a repeater control and in that repeater control I have file upload control where I am attaching file on each row.
I have another update panel, in this I have a save button, whenever I am trying to click this save button and looping through the above mentioned repeater to check file exists in file upload control it always give me false i.e the file upload control is cleared.
I want to know how can I preserve the file in fileupload control with the existing scenario.
Thank you

You need to register the Button for an PostBack. So add a Trigger to the UpdatePanel containing that Button.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
Now you can process the files on the Button click.
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
FileUpload fu = item.FindControl("FileUpload1") as FileUpload;
if (fu.HasFile)
{
//process file here
}
}
}

Related

How to take fileuploader value in button click function .button in update panel file uploader in outside of update panel

I have put asp button in update panel and asp file uploader in outside of update panel in server side button click function i am geting file uploader empty.give me any idea..my code is below
<asp:FileUpload runat="server" ID="DecFormUpload"/>
<asp:UpdatePanel runat="server" ID="UpdateDecForm" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="DecFormUploadClick" OnClick="DecFormUploadClick_Click" OnClientClick="return DecFormUploadClick_Save();" runat="server" Text="Upload" />
</ContentTemplate>
</asp:UpdatePanel>
c# code
protected void DecFormUploadClick_Click(object sender, EventArgs e)
{
if(DecFormUpload.HasFile)//my problem is getting false here
{
}
}
This issue is due to that FileUpload needs a full postback, add the following just after your :
<Triggers>
<asp:PostBackTrigger ControlID="DecFormUploadClick" />
</Triggers>
Hope it helps

ASP.NET - Enabling button by reading data in Grid View inside Update Panel

I have two Buttons and GridView which are:
<asp:Button ID="btn-search" runat="server" OnClick=btn_search_Click />
<asp:Button ID="btn-export" runat="server" OnClick=btn_export_Click />
<asp:GridView ID="gridview1" runat="server" />
btn-search is a control to bind data to the gridview1 from an SqlDataSource. btn-export is disabled at Page_Load and will be enabled when gridview1 has at least one row (btn-export will stay disabled if there is no data/row in gridview1).
At first I wrote this in code behind:
protected void btn_search_Click(object sender, EventArgs e)
{
/* Binding data to GridView */
if (this.gridview1.Rows.Count > 0)
{
this.btn_export.Enabled = true;
}
}
and it worked.
But, then I have to wrap the gridview1 in an UpdatePanel so it wont refresh the whole page when data binding in GridView, using btn-search as AsyncPostBackTrigger.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btn-search" runat="server" OnClick=btn_search_Click />
<asp:Button ID="btn-export" runat="server" OnClick=btn_export_Click />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gridview1" runat="server">
<Columns>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_search" />
</Triggers>
</asp:UpdatePanel>
When the data was bound to gridview1, the btn-search still disabled.
I try to write this.btn_export.Enabled = true in GridView and UpdatePanel event such as OnDataBound, OnDataBinding, OnRowDataBound but it still not worked.
Grayfield, it doesnt work because when you click the search button only the update panel content is updated.
Try adding the buttons inside the ContentTemplate of the update panel and it should work.

Unable to upload photo asynchronously with FileUpload control using UpdatePanel

In my ASP.NET Web forms application, I am adding a feature to upload files but I don't want to do a full postback. So to make it asynchronous I thought of using UpdatePanel.
This UpdatePanel contains a FileUpload control and a button to upload the selected photo. When I was debuggin my code to detect if file is actually selected or not, I found FileUpload's HasFile property to be false.
It works when I remove UpdatePanel but I don't understand why.
To help you understand, here is the code:
ASPX markups:
<asp:UpdatePanel ID="UPProf" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUDP" runat="server" />
<asp:Button ID="BUDP" runat="server" OnClick="BUDP_Click" Text="Upload your photo" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BUDP" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Here's its CS code:
protected void BUDP_Click(object sender, EventArgs e)
{
try
{
if (FUDP.HasFile) // code doesn't pass this if condition
{
FUDP.SaveAs("D:/Pictures/" + count + ".jpeg"); //it doesn't work since there is no file here
}
else
{
//Set some message for user
}
}
catch (Exception ex)
{
//log the error
}
}
Upload requires full page request. This is a limitation of the XmlHttpRequest component used in all AJAX frameworks for asynchronous calls to the application.
Remove the UpdatePanel or make BUDP button to perform postbacks.
<asp:UpdatePanel ID="UPProf" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUDP" runat="server" />
<asp:Button ID="BUDP" runat="server" OnClick="BUDP_Click"
Text="Upload your photo" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="BUDP" />
</Triggers>
</asp:UpdatePanel>

FileUpload component empty on AsyncPostBack

I have an AJAX UpdatePanel and a GridView inside it. A FileUpload component outside the updatepanel.
<asp:FileUpload ID="documentUpload" runat="server" />
<asp:Button ID="btnUploadDocumentDetails" runat="server" OnClick="btnUploadDocumentDetails_Click" />
<asp:UpdatePanel ID="UpdatePanel_DocumentDetails" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnuploaddocumentdetails" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gvDocuments" runat="server"></asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
On button click I have to upload the file on the server and update the GridView (gridview contains the list of all files).
Problem:
When I click on the button to upload the file, the FileUpload component gets empty (HasFile attribute returns false).
How can I resolve this issue to asynchronously update the GridView?

button OnClick event doesn't fire at the first time in UpdatePanel

I have an issue on button OnClick event.
There is a refresh button in a user control ("header") which will refresh a place holder ("phContent") that generates some other user controls at run time. However, the button OnClick event doesn't fire until the whole place holder content loads.
Page.aspx
<ext:ContentHeader ID="header" runat="server" Visible="false" />
<asp:UpdatePanel ID="upControl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:PlaceHolder ID="phContent" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="header" EventName="OnFormSubmit" />
</Triggers>
</asp:UpdatePanel>
UserControl.ascx
<div>
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_OnClick" />
</div>
UserControl.cs
protected void btnRefresh_OnClick(object sender, EventArgs e)
{
//some code
OnFormSubmit(this, e);
}
public delegate void UserControlFormSubmit(object sender, EventArgs e);
public event UserControlFormSubmit OnFormSubmit;
Not sure what exactly the page and source code is.
But from front-end, take for jQuery as example.
You can try something like:
jQuery(function(){
jQuery('#btnRefresh').on('click',function(){
//load the content by ajax
jQuery('#phContent').load('#chart');
});
});
You have to place the button in updatepanel as well.
<ext:ContentHeader ID="header" runat="server" Visible="false" />
<asp:UpdatePanel ID="upControl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_OnClick" />
</ContentTemplate>
</asp:UpdatePanel>

Categories