Saving web configuration elements with events on controls - c#

I've got some controls on a web page that surface configuration elements of my web application.
I've wired up to their "OnChange" events, to capture value changes and so on. For each on change, I would like to say Configuration.Save(), but apart from getting "access denied" exceptions on web.config, I suspect this could be some weirdness, in trying to save to the configuration file for each control's onchange.
Any suggestions for the best way to handle this?

Would you add some detail as to what type of settings you are trying to update? I really don't think you want to save changes to your web.config from the application. Does the application have a database you could tie your saves to? That seems more appropriate for a changes you would want to make regularly, and in a transaction safe manner.
I still think this is better done using a database. But, if you must, then I'd check out this guide to working with web.config.

From an architectural standpoint, it would be better to save the changes to the configuration file when the page is submitted. However, it is likely that the web.config file is locked when the application is started.
Consequently, if you have application-specific changes that you would like to save, it is better to find a method other than web.config for saving them.

You would want to find an alternative to modifying the web.config file directly. Doing so causes your application to restart. Either generate your own XML file or a database configuration set up.

Related

Is possible to save locally a view object in xamarin forms?

I need to save locally (on a file) a StackLayout object, because I need to save the current state of a page, so I was just wondering if could be possible.
EDIT: Solution
As I have seen, there is no solution!
This is because Views are not serializable, so it's neither possible to serialize the Views as a string (trust me, I have tried them all), the only way is to re-generate your page by saving and loading data of simple types such as int or string (because only simple data can be serialized).
According to your needs, I think you only need to save the user's data and restore it at startup.
You can use File Handling in Xamarin.Forms to save the user's data, and then rewrite the OnAppearing method every time it starts, and restore the data inside the method.
For more information about File Handling, please refer to:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=macos#saving-and-loading-files

Serve files dynamically instead of statically

Following this answer, I've enabled serving JavaScript and CSS from my Views folder which is helping my project's organization. Unfortunately, while I'm developing, I find I have to disable my browser cache in order to see my changes. I'd like to be able to develop without keeping DevTools open just to disable the cache.
I figure my issue is the System.Web.StaticFileHandler serving up the files in such a way that the browser caches them. I've tried using System.Web.Handlers.TransferRequestHandler like this answer suggests, but then I get 500 Server Errors instead.
Is my understanding of the problem correct? How can I resolve my issue?
Note: Not that it's relevant, but I intend to add a config transform to switch back to the static handler for releases.

How to change a website default document programmatically on the fly

Using ASP.NET 4.0, IIS 7.5.
I have a website engine, I have just implemented a way for this to tell if it's being loaded on mobile and instead of loading Controls\MyControl.ascx it loads Mobile\Controls\MyControl.ascx. This works well for my controls and also my MasterPage.Master file.
What I can't figure out however is how I can do the same with Default.aspx. This needs to be done on the fly programatically as I need to be able to check if it's mobile version. I was thinking of doing something on a pre-init event in globals but not sure if that's the best way.
Note: I don't want to use inline code on Default.aspx and just display different content base on my Mobile flag as my scenario goes one step further by basing the file on customer as well and this would mean having one huge Default.aspx for all customers which wouldn't be manageable.
Changing the default document on the fly is not possible in any practical sense.
Writing to the web.config on the fly to load a mobile version of a default page is quite frankly terrible and not an answer to the true context of your issue. I would feel irresponsible as a developer if I even proposed this as an answer to loading a mobile version of a default page.
I was trying to help you solve your problem and not just answer the base question in the title. As we all know, changing the web.config will restart your application and would not serve as a true solution, as you could not do this and achieve any kind of performance.
Here is the BEST alternative (IMHO) to dealing with mobile browsers.
http://51degrees.codeplex.com/
HTH!
For anyone else looking at this I have found a solution but I am not sure I will implement it as I don't like the idea of updating the web.config file at run time. Using the Microsoft.Web.Administration namespace you can update the server.webServer -> defaultDocument section programmatically. Doing this allows you to change the path to default.aspx and it will load based on the variables you set.
This link should provide more information: http://blogs.msdn.com/b/saurabh_singh/archive/2007/11/24/accessing-iis-7-0-features-programmatically-from-configuration-file-s-c.aspx
Also, the Microsoft.Web.Administration dll isn't available directly in VS so you need to add it from %windir%\syswow64\inetsrv (64bit version).

Windows Forms Saving controls Layout Information using XML

I have designed a form to create controls like textbox, panels buttons dynamically. I have an option to edit the size and position of the controls. I need to maintain the layout and the parent and child relation of the controls so that I can save the information and re-create it when loaded again. Is there any other better option other than XML to maintain the layout information?
Thanks in Advance.
Try using app.config file it'll provide you better operations and will save a lot of code that you'll be doing right now to iterate through your saved XML file.
For more info Go here
Do you have the option of saving it in the database (per user if required)? If you have to implement it file-based XML is probably the easiest option, whether you go the app.config route or write your own class to save and retrieve custom user settings from XML - a good place to save this might be the user's AppData folder.
Anything saved to app.config might be lost when you use something like ClickOnce to do version upgrades.

What can I use to asp.net store tooltip messages?

I have around 60 controls and I want to show a tooltip for each of them.
I could do this manually but I want something that would allow me to make changes to the tooltip messages or the control they are bound to without modyfing the code (I don't know what this would be called).
I thought about XML, but is there a better to store simple data like this?
I would recommend a custom configuration section. http://msdn.microsoft.com/en-us/library/2tw134k3.aspx. Then all you have to change are the values in the config file. You could even reference it as a separate file as to not clutter up your web.config.
If you wanted them to be user-configurable through an admin section I'd say put the text in a database.
Otherwise, I'd suggest putting it in resource files.

Categories