Storing ascx in database - asp.net

I would like to store the ascx control code into a database. Then rather then load the control from a filepath location, I would like to retrieve it from the database and load it into the UserControl. The UserControl.LoadControl only has two overload options. Without saving the control from a database to a temporary file and then load from the temporary file, is it possible do this direct from the database?

Can you use ParseControl instead? Link to MSDN.
The method accepts a string that is then compiled on the fly as a Control object.
You could load your control markup from the database and then hand it off to ParseControl to get an instance of your control back.
Hope this helps!

The only problem is that the ParseControl does not cause compilation, so if there are any Codes in your ascx, they will not get executed.
So far the only option for me has been to write the ascx to file (either permanently or temporarily) and then use LoadControl method to load the ascx.

Related

Making Ajax for Fileuploads in ASP.NET, while keeping track of files in Memory

I need to make a Asp.net control to upload multiple files that sends the files through Ajax.
This control could be used to store the files on a Folder or in the Database, the persistense is going to be decided by the Classes that will use my control, not the Control itself. So i need to achieve a level of flexibility here.
The uploaded files will be stored my Control Class like a List of Files, until the class using my control execute the "official post" which will consume and then finally release the class.
In a simple example a Form using my control would consume the List generated by all the uploads made through my control and upong Save, the form would read my list and persist it the way is best.
The problem here is the approach:
I can't use a updatePanel because it doesn't allow for fileinputs and (afaik) won't keep the state of purelly javascript generated input=files.
It's not possible to use the WebMethod property to send the data since it requires my Method to be static which makes it impossible to feed a in memory object using this method.
The other option would be to use handmade Ajax, which is not that difficult, but I'm not sure how Asp.net will behave, does it keep states between Ajax Calls? Can I access the same in memory object from different calls? will i have to keep track of viewstates or use sessions? is it viable to use sessions or the viewstate(!) to keep uploaded files?
Reforcing: The problem is NOT Uploading the files with ajax, I'm using blueimp.github.com/jQuery-File-Upload , the problem is keeping track of them in the serverside without persisting.
Example
Let me try to explain a little better:
0) I have a control, this control has a property that is a List of the files - List<files> FilesUploaded;
1) On each ajax upload , I add the coming File into the list FilesUploaded.Add(FileComingFromAjax)
2) My controls goes into a form (any form), and when I save the Form BtSave.Click(), the Form class will need to get the list of files and persist so: they access it like this AnyonesForm.MyControl.FilesUploaded. Notice that the files are in Memory and not in the disk or the database.
My doubt is, Will the files be kept after each request? namelly:
Will the method call through ajax, use the Same List?
When i submit the main form, Will the List be accessible by the form class with the files I just uploaded?
Well Actually , I was thinking asp.net would be able to keep the objects between ajax requests, but this is not the case.
I'm going to use sessions, I'm not afraid to add objects to sessions anymore , ASP.net, adding objects to a session variable
Combine jQuery and AJAX like so:
jQuery Ajax File Upload
How can I upload files asynchronously?
to achieve your result.

FileUpload in Autogenerate Form postback

I have a problem. I have a Dynamic form created in asp.net. Generally it has fileupload companent . In This time page postback is fire than form generated again and selected file is disappear. My question is how to hold this file in memory?
Am I hold this folder in fileselecting?
Have you any idea for this?
This is due to security in asp.net. You do not have the ability to specify the value of the file upload and a postback will clear it. You could take care of this by doing thing asynchronously. Try using the AsyncFileUpload control in the AjaxToolKit. You can read about how to use it at http://www.kruegerwebdesign.com/blog/async-file-uploading-using-asp-net.

.aspx works but .ascx does not... why and how to fix?

I want to put a ASTreeView web control in a custom web control, ASTreeView sample code is like:
<ct:ASTreeView ID="astvMyTree"
runat="server"
...
LoadNodesProvider="~/ASTreeViewDemo5.aspx"
.../>
LoadNodesProvider is the page ajax called when loading a node...however if I changed the provider to my .ascx file, it does not work:
LoadNodesProvider="~/ASTreeViewDemo5.ascx"
it did not even go through the Page_Load part of the .ascx file
Though this might be related with astreeview itself, I'm wondering what the problem could be? anything I can do to fix it?
Thanks!
It is because ascx must have a container ie Page. You can't use it same way as Page.
ASPX is a page and ASCX is a usercontrol. You cannot ajax call a control, so you probably want it to be a page with the control on it.
While it isnt entirely clear to me what LoadNodesProvider is supposed to do, if you want to encapsulate some code or run a process via AJAX you have a couple of options. One would be to create a web service (you could use WCF for this) that the AJAX method could call. Another option would be to create an http handler (ASHX extension typically denotes this). Using an ASPX or ASCX for this doesn't make a whole lot of sense to me. Proco and Tomas are correct regarding the ASCX file, these are Usercontrols and are not stand-alone objects.
If you really, really want to use an ASPX page/ASCX control, then I suppose it would be best to create a blank ASPX page that has one placeholder, and then attach your user control (based on query string parameters or something I guess) to the placeholder to render out the content for your AJAX control

Is there a way to get an httppostedfile WITHOUT using the fileuplaod control?

I have a class that needs an System.Web.HttpPostedFile object.
In most cases this is obtained when the user uploads a pic. However, in cases where they don't I need to pass a default image to the class.
But how do I populate the HttpPostedFile property if I'm not using the fileupload control?
You cannot select a file from the users machine for them. The ONLY way for a file to be selected and transferred from a client computer to your web app is if you use the input type="file" tag (which is what the file control wraps) and let THEM select a file.
What you CAN do is detect on post back whether they transferred a file to you or not. If they haven't then you can use a file local to the SERVER to use instead; however, this isn't going to come through the posted file area.
As far as reasons why not: security, plain and simple.

how to use codebeside in ASP.NET Web Application

I'm using VS2008 and want to create a web application (not a web site) with Code-Beside
but, the default mode of aspx is Code-Behind.
I have tried to change the CodeBehind=ClassFile.cs to CodeFile=ClassFile.cs in the header of aspx's <%#Page%> part, and deleted the aspx.designer.cs file,but if I added a server control to the page, the compiler is also send me an error of no member defined.the cs file is the orinal file of codebehind, it is partial class.
You don't want to delete aspx.designer.cs you want to delete the aspx.cs file, then place a similar file next to it and declare it as a partial class. designer.aspx.cs is still required to provide you direct access to controls placed within the page, rather than going through FindControl.
You definitely don't want to delete the .designer.cs file, as this is where the server control definitions will be placed.
In general the codebehind model is much better as it makes the code easier to find, use and maintain.

Resources