Thursday, March 22, 2012

Strange Page Behavior

Hello to you all.
I have an aspx page with an update panel, and outside that update panel i have a htmlinputfile for uploading some files. It is outside because i understand that htmlinput will not work correctly with an update panel. Near the htmlinputfile i have a Add button and a cancel button. After i add a file, and want to sort a gridview in wich the file is after upload, when the page refreshes the lien execution goes threw the Add button's click event, adding the file again.

Thank you for your time, hope someone can help me with this.

Where are you placing or calling your Add method code? Let us see your code please!

Regards


Here is the markup in the aspx:
<asp:UpdatePanel runat="server" ID="updatePanel" EnableViewState="true">
<ContentTemplate>
<asp:Panel ID="pnlFiles" runat="server">
<asp:GridView ID="grdvFiles" runat="server" AutoGenerateColumns="false" DataKeyNames="FileName" OnRowCommand="grdvFileName_RowCommand" OnRowCreated="grdvFileName_RowCreated" CssClass="datatable" Width="100%" CellPadding="0">
<Columns>
<asp:TemplateField HeaderText="File name">
<ItemTemplate>
<asp:HyperLink Target="_blank" ID="hypAttachment" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateModified" HeaderText="Date" />
<asp:ButtonField ButtonType="Image" CommandName="_delete" ImageUrl="~/images/delete.gif" />
</Columns>
</asp:GridView>
</asp:Panel>
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" DropShadow="true" PopupControlID="pnlModalUpload" TargetControlID="btnModalUpload" CancelControlID="btnCancelAttach" BackgroundCssClass="modal_bg">
</cc1:ModalPopupExtender>
</ContentTemplate>
</asp:UpdatePanel>

So this i sthe content of the update panel. The modal popup extender is used to bring up the following panel:

<asp:Panel ID="pnlModalUpload" runat="server" BackColor="#E0E0E0">
<table style="width: 168px">
<tr>
<td>
<input id="fileBrowse" type="file" runat="server" enableviewstate="false" />
</td>
</tr>
<tr>
<td align="center">
<asp:ButtonID="btnAddFile" runat="server" CausesValidation="False" Height="20px" OnClick="btnAddFile_Click" Text="Add" />
<asp:Button ID="btnCancelAttach" runat="server" Text="Cancel" Height="20px" /></td>
</tr>
</table>
</asp:Panel>

And the btnAddFile onclick event is the following:

protected void btnAddFile_Click(object sender, EventArgs e)
{
if (fileBrowse.PostedFile != null & fileBrowse.PostedFile.FileName != "")
{
string fileName = fileBrowse.Value;
fileName = System.IO.Path.GetFileName(fileName);

.........

///here is a work arround the problem i've mentioned

bool addFile = true;
DataSet existingFiles = tvWS.GetFiles(Convert.ToInt32(ViewState["taskID"]));
foreach (DataRow dr in existingFiles.Tables[0].Rows)
if (dr["FileName"].ToString() == fileName)
{
addFile = false;
break;
}
else
addFile = true;

if (addFile)
{
fileBrowse.PostedFile.SaveAs(ConfigurationManager.AppSettings["AttachmentsWrite"] + fileName);
.........

Thank you for your time!

0 comments:

Post a Comment