asp.net webform pattern re-use same page (view and edit) - asp.net

i'm working on a project which aim is to create a sort of social portal.
The matter is that i've an entity page detail where user can edit it or just view it.
I'm considering to create just one page and using that as view only and edit mode at the same time.
I had two ideas:
1) create an enumeration ControlBehaviour { EditMode, ViewMode } in Session and using that to understand if I have to set readonly every controls in my page;
2) use jquery to set readonly every controls in the page if i have to open it in viewmode (or not readonly if i have to open it in edit mode).
Does it exist any pattern to reach my aim?
best regards

Separating your concerns into read and edit forms is most suitable, in terms of maintaining your code afterwards, by yourself or for someone else.

The DetailsView control is exactely what you are looking for

Related

Web user control as masterpage with two child views

I'm building custom .NET web user control for Umbraco, which looks like single page, with a sidebar (which holds IDs of content items) on the left side of page, and two different content views(which have content by id displayed here) to the right of it.
The thing is, i don't know if this is possible to do something like masterpage/childs principle(in terms of this web user control), so i'll have something like "Master page" with this sidebar, and will render one of two child views to the right of it, depending on id selected from sidebar?
Or maybe there should be some workaround used here?
Will be much appreciated on any help here(like information about how that can be done, what information shall i seek for etc.), as i'm completely new in this kind of stuff.
From what I know, what you could possibly do is have your "master control" set up in your web user control, and then dynamically create one of two custom controls based on the values you choose from your master control. This is also known as a Master-Detail solution (provided a link).
Tutorial 10: Master/Detail Using a Selectable Master GridView with a Details DetailView
If each of these controls was a user control in itself nested inside another user control, you could for example have three web user controls: Master, Child1 and Child2.
Using code-behind, you could easily create dynamic controls based on those you select from your Master control using Events to the "container page". Using these events, you could dynamically create your user controls/pass them variables, and so on.
Here are some articles about events, if you are interested:
Events in User Controls
Events in ASP.NET Server Controls
If you didn't want to dynamically create your controls, it wouldn't be difficult to bind them on demand dependent on the variable from your Master control, and hide them if they are not being used/bound.
You could also use the public properties of a control you define yourself. So say you have your Child1 user control, you could define a public property in the code behind. You can access this in design-time as well.
public int SpecialID { get; set; }

how create shared web from in asp.net

I am working on asp.net application for reporting. I need to develop round about 50+ reports. On each report I need selection criteria that may contain start-date , end-date, name , company etc on almost every .aspx page. these controls can be of type like dropdown, textbox or calender etc .
Any idea to use one editable + shared (not 100% same) web form on every page.
Using ASP.NET custom controls will allow you to create a module that you can insert into all of your pages.
You can also check this out to get you started more quickly.
http://www.codeproject.com/Articles/1739/User-controls-in-ASP-NET
If you want to make it similar but not 100% same for all apps then just create public properties that you can use to adjust control properties on different pages. For example if some text box should be visible on some page and not visible on other just create a public property in your control named something like EnableTextBoxABC
You can create ASP.NET Custom Controls.

The best solution to customize page controls based on some roles and settings

I have several pages in asp.net each with lots of controls. I Also have some roles in my application that each has some setting options. Now I want to prepare my page based on these settings. Maybe it’s not too clear, so please take a look at my example.
Example: There are some buttons, some textboxes, some datetime picker, and a chart in a page, now what I want is when a user sees this page, the controls appear and disappear based on the users role. An important thing is that I don’t want to have only visible and invisible controls, in some scenarios I need to show controls with some customizations. For example change chart data source, limit selecting date time and so on.
The first solution that I can think of, is saving the settings in database and after visiting the page by user, the settings fetch from database and based on those, I can customize the controls with conditional phrases (if and else). But I suppose it is not a good approach and my page will get very messy.
Please help me with any better solutions and if you know good references about it, please let me know.
Please see this link...use of ControlAdapters may help you...
Role-based enabling/disabling of controls in asp.net
You must use Thread.CurrentPrincipal.
A. When user login to your application, you attach his identity to thread, for example
string[] rolesArray = .....; //Get roles from dataBase by identity.
Thread.CurrentPrincipal = new YourCustomPrincipal(new YourCustomIdentity("YouName", "..."), rolesArray);
B. And when you navige about your application you test Thread.CurrentPrincipal
IPrincipal threadPrincipal = Thread.CurrentPrincipal;
if(threadPrincipal.Roles.Contains("roleTest"))
{
//Adjust your control
}

How to transfer data to another page

Hai, I have an ASP.NET page with 150 controls and i want to transfer data of these controls to another ASP.NET page. what method would be best for this task? Number of controls may increase.
Thanks in advance
There are many ways:
Using a Query String (Might not work in your case, only good for transferring small amount of data)
Getting Post Information from the Source Page
Using Session State
Getting Public Property Values from the Source Page
Getting Control Information from the Source Page in the Same Application
Its always preferable to wrap the data you want to transfer in an object and pass it using pt. 3 or Pt. 4 , though in case you have arbitrary number of controls, Pt. 5 may work better for you.
This should cover it comprehensively:
MSDN: How to: Pass Values Between ASP.NET Web Pages
ASP.NET 2.0 : Accessing controls in Previous Page
You can use datatable , populate the contents in the row and send it using session
Another way is use generic class and transfer it using session.
You can also transfer it using below mentioned code
TextBox previouspagetextbox = (TextBox)PreviousPage.FindControl("currentpagetextbox");
the above mentioned code will be written in the another page where you will access the controls of previous page.
Multiviews is an another option. So you donot need to transfer the contents. It will facilitate you in same page.
As i could understand your need it will be possible through server side session or any other servers side storing mechanism like you can store the data in the database also and then fetch the control values on the next page by the Primary key or any other composite unique combination but at the cost of your page performance i will suggest you better to use ASP:Wizard control that is available from asp.net 2.0.
Most of the things will be taken care by the asp:wizard and it will be easy for the user of the page to fill up the information in the controls.
for details ion wizard control read on the following link
Hope it will be helpful.
Happy coding.
You can use Server.Transfer('NewPage.aspx', True) to redirect to a new page and that page will have access to all of the controls that were on the previous page.
MSDN Article about it

Whats a good way to trim the GUI of a ASP.NET website?

I've been trimming the UI of our website by doing the following in the onload event of that control:
btnDelete.isVisible = user.IsInRole("can delete");
This has become very tedious because there are so many controls to check again and again. As soon as I get it all working, designers request to change the UI and then it starts all over.
Any suggestions?
One simple suggestion would be to group controls into panels based on access rights
Something I have done before has been to create a custom page class (Actually, I do this part on every project) that each ASP.NET Page inherits.
This page class contains an IsAdmin property.
I then subclass the commonly used controls that may or may not be visible between modes into custom controls, and add code to check the Pages IsAdmin property.
All this is maybe an hour of work, but if you build pages using these controls, they manage their mode automatically.
Another fun timesaving tip is if you need to flip the page in and out of readonly mode. I added a property to the main base class, and then added a custom control that renders a textbox in one mode, and a label in the other.
Again, a little bit of time on the components, but then you can create a readonly version of the page in 2 lines of code...Very worth it.
You may be thinking of the situation in the wrong way. Instead of thinking of individual controls, think of it in terms of business roles and what they have the ability to do. This goes along with grouping controls into panels for access rights. For example, maybe only managers have the ability to delete and do other things, and you have a role for managers that you check. This way if there are changes, you can just move users into different roles. Business rules should not change drastically. There will always be tweaking as new positions gain more responsibility, but thinking of it in this way should minimize the number of changes to be made.
A quick and dirty option is using the asp:loginview controls, which can be wired up to user roles.
Not as elegant as the custom page class option suggested by Jonathan, and can be a bit of a performance hit if they are all over the page.

Resources