Asked by:
Am I able to create a custom View to be used as a template?

Question
-
User1351072825 posted
First off, I apologize if this question is obvious or has been asked before. Half the trouble of finding the answer to your problem is knowing the proper terminology to yield an answer. So far, every google result has brought me to something else.
The gist of this is, I have a multiview with 4 different views. It could be 2 or 10. The point is that it's more than 1 and these views are exactly the same.
A lot of the examples of multiviews has a few controls that are the same for each view, but with different IDs.
But if my controls take up several hundred lines of code, that really stacks up; especially in the codebehind with the saving and loading from the DB.
I've read that I can use PartialViews, but that seems to use just html, and I need asp:control(s)
When it comes to loading, I want to query for the data on these four different profiles, reusing the same query four different times, filling each view one by one, targeting the control by ID on the current view.
Then doing the same thing on the save. It seems unnecessary to write duplicate load/save functions for each view when they're all doing the exact same thing, essentially.
Saturday, December 22, 2018 2:28 AM
All replies
-
User1120430333 posted
Then doing the same thing on the save. It seems unnecessary to write duplicate load/save functions for each view when they're all doing the exact same thing, essentially.
Have you ever heard of SoC?
https://en.wikipedia.org/wiki/Separation_of_concerns
<copied>
The value of separation of concerns is simplifying development and maintenance of computer programs. When concerns are well-separated, individual sections can be reused, as well as developed and updated independently. Of special value is the ability to later improve or modify one section of code without having to know the details of other sections, and without having to make corresponding changes to those sections.
<end>
The load/save should be segregated as an individual section of code that is useable by multiple views perhaps.
But if my controls take up several hundred lines of code, that really stacks up; especially in the codebehind with the saving and loading from the DB.
You should also consider loose vs tight coupling
http://www.dotnet-stuff.com/tutorials/c-sharp/understanding-loose-coupling-and-tight-coupling
You could also consider using the MVP UI design pattern, speration of duty (SoD) and shift code away from the codebehind file.
https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP
http://polymorphicpodcast.com/shows/mv-patterns/
Finally, you should consider an Architectural Pattern such a Layered that is much like N-tier.
https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)
Also consider this too.
https://dzone.com/articles/reasons-move-datatables
https://en.wikipedia.org/wiki/Data_transfer_object
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
Anyone can write some code. But can you architect a solution from the frontend UI to the backend database and do it effectively?
You may also want to look into user control, and you can use MVP with it too.
https://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-user-control-in-Asp-Net/
.
Saturday, December 22, 2018 7:29 AM -
User1351072825 posted
Thank you for taking the time to direct me to all these helpful resources, but it's not quite bringing me an answer to my questions.
Right now the code is like this<view id=1>
<textbox id=tb1/>
..(several other controls)..
</view>
<view id=2>
<textbox id=tb2/>
..(several other controls)..
</view>
I have not yet written the load/save functions, but they're meant to work interchangeably on each view.
My dilemma, is that I cannot target tb1 with the same code as tb2, in the load or save function. I must specifically delcare their id.
I would rather use the same function however many different times like, activeview.controls.findcontrol(tb)
or something to that effect.
I want to create a separate file that I can instantiate
CustomView view1 = new CustomView()
CustomView view2 = new CustomView()
and then just add
MyMultiView.add(view1)
MyMultiView.add(view2)
at the page startup.
I hope I explained that clearly.
Saturday, December 22, 2018 3:47 PM -
User1120430333 posted
MyMultiView.add(view1)
MyMultiView.add(view2)For the above, I would simply use a usercontrol, which you can have as many HTML controls in the usercontrol as you wish. A usercontrol is like a mini form within an ASP.NET Webform being a container. The whole Webform can have several usercontrols on the Webfrom with each usercontrol with its controls acting independently from any other usercontrol and its controls. The usercontrol will act independently from the Webform that is hosting the usercontrol. The user control has events just like a ASP.NET Webform has.
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.usercontrol?view=netframework-4.7.2
The ASP.NET usercontrol has an ascx.cs like an ASP.NET Webform has an aspx.cs.
https://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-user-control-in-Asp-Net/
Saturday, December 22, 2018 9:58 PM -
User1351072825 posted
That seems perfect, but just to be perfectly clear, it can substitute or act as a View, within my multiview?
Saturday, December 22, 2018 10:24 PM -
User1120430333 posted
That seems perfect, but just to be perfectly clear, it can substitute or act as a View, within my multiview?
I don't see why you can't have a usercontrol within a view of a Multiview. On the other hand, you don't need a view in order to have a usercontrol on a page.
Saturday, December 22, 2018 11:06 PM -
User1351072825 posted
It's essentially a matter of keeping all data on the page, without having to keep re-running the queries each time the user wants to view the next or previous profile.
So it loads data for all four profiles at once, and just leaves visible what the user chooses to see at the time being.
Saturday, December 22, 2018 11:09 PM -
User1120430333 posted
It's essentially a matter of keeping all data on the page, without having to keep re-running the queries each time the user wants to view the next or previous profile.
So it loads data for all four profiles at once, and just leaves visible what the user chooses to see at the time being.
The only way you can do that is use a session object to hold the data on the initial read and keep loading data from the session object, if in fact, the data is static after the initial read of the data.
Sunday, December 23, 2018 2:28 AM -
User1351072825 posted
I'm also using the MultiView within an update panel.
The main objective is remove the necessity for a database query every time the user switches between profiles.
Calling the database takes too long. If it's all being stored in a ViewState variable, like a DataTable, then it will just only need to read that table between different profiles.
As long as the database isn't being again called after the page loads, or until the user hits "save", that's a viable solution;
but not the main problem I was stuck on. That's just the reason behind my design choice.
Sunday, December 23, 2018 3:36 AM -
User1120430333 posted
Viewstate is slow and involves dragging the data over the wire between the client browser and the Web server application on the round trip.
A datatable is slow too.
http://lauteikkehn.blogspot.com/2012/03/datatable-vs-list.html
https://dzone.com/articles/reasons-move-datatables
https://www.codingblocks.net/programming/boxing-and-unboxing-7-deadly-sins/
The optimal approach would be to use a collection, a List<T> using the DTO pattern and hold the collection of DTO(s) in session, which a List<T> is bindable to a control.
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
Sunday, December 23, 2018 7:43 AM -
User1351072825 posted
Thank you for the resources. I think i have a firm understanding on the basics but there's a lot I need to learn to improve my skills.
Would you mind writing out a loose example of what you're describing?Sunday, December 23, 2018 9:55 PM -
User1120430333 posted
SQL datareader using a List<T> and a custom object.. The custom object would be your DTO. And you would return the List<T> of DTO(s), and the reader is using column name from the database table
the example of using a datareader is using ordinal position of the column on the database table, if you can us the above column name.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader
At it's simplistic form of putting list inti session and getting the list out session.
https://www.youtube.com/watch?v=LW-1MsoG1C8
1)create a new List<DTO within a method function
2) create t-sql to pull data.and execute it.
3) in reader loop, make a new DTO populate the DTO's public properties from the reader table columns and add the DTO to the list
4) return the list from the function.
5) put the list into session
5) if the list in session is not a null value, the get the list out of session and bind it to the control
6 if list in session is a null value, then get the list from the database by executing the function to return the list, bind the list to the control and put the list into session
That's it in its most simplistic form.
Capiche?
Monday, December 24, 2018 1:09 AM -
User-893317190 posted
Hi Amish-SpaceProgram,
It seems you want to improve the performance of retrieving data from database.
You could use .net framework's build-in Cache object, which could add cache into your project.
The link below provides how to use the cache.
https://www.c-sharpcorner.com/blogs/caching-in-asp-net
You could also try to use OutputCache , it could cache your aspx page.
https://asp.net-tutorials.com/caching/output-cache/
Best regards,
Ackerly Xu
Friday, January 4, 2019 7:45 AM