FileUpload in FormView inside UpdatePanel

Scenario:
I have an ASP.Net webpage that I intend to use in order to allow a user (not real users, but mostly a content manager) to insert and edit records in a table using FormView. This FormView is inside the UpdatePanel, since I also use cascading dropdownlists so that the user can select some values.

Now this FormView also contains 4 FileUpload controls, and as you know, these file upload controls require full postback because most browsers don't allow Javascript to access the disk. So, this problem would be solved if something like:

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:PostBackTrigger ControlID="InsertButton" /> <asp:PostBackTrigger ControlID="UpdateButton" /> </Triggers> <ContentTemplate>....</ContentTemplate> </asp:UpdatePanel> 

Edit: Forgot to add that file loading occurs in the OnUpdating and OnInserting events of the SqlDataSource source.

Problem:
Since InsertButton and UpdateButton are inside Formview, I cannot directly access their identifier via markup. And MSDN says that:

Programmatically adding PostBackTrigger controls is not supported.

Please suggest some solution for this work. Any understanding of this issue is much appreciated. Thanks.

PS- A workable solution for me was to install UpdatePanel PostBackTrigger as the whole FormView itself:

 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:PostBackTrigger ControlID="FormView1" /> </Triggers> <ContentTemplate>....</ContentTemplate> </asp:UpdatePanel> 

But now, due to a slight change in requirements, this solution (if you call it a solution) is unacceptable.

+4
source share
3 answers

Yay !! Finally, it will work!

Here's How:

Well, contrary to what MSDN says, we can actually add PostBack triggers programmatically. Not necessarily for UpdatePanel , but for ScriptManager .

After hours of play, this worked:

We cannot access the controls inside FormView until the template is displayed, so we can only add postback triggers after the OnDataBound Event form.

 protected void FormView1_DataBound(object sender, EventArgs e) { if (FormView1.CurrentMode == FormViewMode.Edit) { LinkButton lb = (LinkButton)FormView1.FindControl("UpdateButton"); ScriptManager.GetCurrent(Page).RegisterPostBackControl(lb); } //Similarily you can put register the Insert LinkButton as well. } 

And now, if your UpdatePanel calls ConditionalUpdate , you can do something like this to make it work:

Markup:

 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate>.. <EditItemTemplate> ... <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" OnClick="Cause_PostBack"CommandName="Update">Update</asp:LinkButton> ... </EditItemTemplate> ..</ContentTemplate> </asp:UpdatePanel> 

CodeBehind:

 //call this function as the OnClick Event Handler for the Controls you want to register as //triggers. protected void Cause_PostBack() { UpdatePanel1.Update(); } 

Otherwise, if your situation allows (like mine), just set UpdatePanel UpdateMode="Always"

+2
source

Have you at least talked about using iframes to perform postbacks? sort of:

 <iframe name="uploader" id=uploader src="uploaderSender.aspx?AllowedExtension=<%= AllowedExtension %>&StoringPath=<%= StoringPath %>&StoringFileName=<%= StoringFileName %>&OldFileName=<%= OldFileName %>&MaximumSize=<%= MaximumSize %>" width="450" height="50" frameborder=0 scrolling=no > </iframe> 

with uploaderSender.aspx like:

 <form action="UploaderReceiver.aspx" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file" onchange="document.getElementById('IsFileUploading').style.visibility = 'visible'; document.forms[0].submit()"/> <span id="IsFileUploading" style="visibility: hidden"> <asp:Image ID="Image1" runat="server" ImageUrl="~/immagini/Ajax-loader.gif" /> </span> </form> 

and UploaderReceiver.aspx like:

 protected void Page_Load(object sender, EventArgs e) { //if there is one file to process if (Request.Files.Count > 0) //create the folder if it does'nt exists and returns the local path to get it string StoringPathToBeSaved = StoringPath.GetFolderPath(); // append the name of the file to upload to the path. StoringPathToBeSaved = StoringPathToBeSaved + StoringFileName + Extension; Request.Files[0].SaveAs(StoringPathToBeSaved); } 

these are just bits of code so you can understand if you are interested in this way of working with the download, I can give you more if you want.

see you, and good luck with your code,

+3
source

This is old, but he tried to solve another problem and ran into this. This was not my problem, but here is an alternative for anyone new who also comes across this.

You indicated your problem as:

Since InsertButton and UpdateButton are inside Formview, I cannot directly access their ID through markup

In fact, you can access their identifier using markup to use it as a ControlID in a PostBackTrigger. You just need to use the name button, which is created in the html of the page as a ControlID. You can find the name created when viewing the source of the page when viewing the page in the browser. This is usually the name of the button FormView + $ +.

For example, let's say you have a FormView with the name "FormView1" that contains the "Insert" button in which you specified the identifier "btnInsert" during design. If you open your page in a browser to view it live and then look at the source of the page, you will notice that the html button of the button will actually be named "FormView1 $ btnInsert".

Use this name as ControlID in PostBackTrigger and your update panel will work.

 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:PostBackTrigger ControlID="FormView1$btnInsert" /> </Triggers> <ContentTemplate>....</ContentTemplate> </asp:UpdatePanel> 
0
source

Source: https://habr.com/ru/post/900454/


All Articles