asp.net user control - upload image control - asp.net

i want to create a user control that will display an image (the location of the image is store in my db) the control will have the ability to change the image or delete it.
i want to build another control that will hold some of that control.
what are the properties that i need to set or get from the first control to the second and then to the page that will hold them?

well, it seems that we are talking about inheritance,
in my first thinking to solve this quickly you have to create a class (maybe on app_code dir) that inherits from usercontrol. Here you gonna implements tha base methods that will be used for each derived classes.
when you have this "Base class" implemented your control will not inherits fom the default usercontrol, but will from your base class.
by this way you can create a kind of base class that provides adicional properies or methods for your purposes (thats a kind of extension if you dont understand much of oop)

Related

Webparts asp.net

What are the disadvantages to using webparts in asp.net? Are they losing their popularity?
I was planning on creating a user defined Dashboard and was thinking of using Webparts, is this the way to go or is there another way of doing customisable dashboards these days? I would prefer not to have to use a 3rd party product.
To me, webparts are more of a sharepoint thing these days.
Other than that, with some design you can get all you need with user controls and some classes of your own to get common functionality and layout sorted out.
Say you have a base class called DashboardWidget that inherits from WebControl here you could define a overridable method
ProcessDataSource -> executes a query to a database or service and formats the results for presentation
And in the prerender method (or something similar along the asp.net lifecycle) you call ProcessDataSource (you could use the DataSource property most controls already have).
Then you inherit from DashboardWidget to make your controls and in the render method you override ProcessDataSource to get the info the way you need and the Render method to setup what to display.
Ideally you should define your own data source class to have a single place to define where to connect, how, credentials, etc..
As for the layout, newer versions of asp.net include several layout option you may use (and there is always pure html if you want)
I'm shooting from the hip a bit here, but that's a way to do it without involving webparts, that to me, are not that popular outside of sharepoint.
Hope anything of this helps

asp.net webforms - composite controls interacting with page controls

I am writing a composite control in c# asp.net as an experiment and would like to have all the instances of my composite control interact with one external control. Is this possible?
Things I have tried:
option 1: check from within the control whether an external control exists on the page, and add it if it doesn't
option 2: have the target control's id passed to the composite control at design time and then use this.Page.FindControl()
Obviously it was wishful thinking that it would be that simple :)
If I try do this from within the CreateChildControls Method, this.Page.FindControl(target control) always returns null. If I try to add the control to the page from within this method, it throws an exception:
"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."
is there another method / event where I can achieve this?
Why don't you expose a public property on your Composite Control of what output from them, then when rendering the Panel's contents, recurse through the page, find all instances of the composite control, grab the text, and add it to the panel?
You can create multiple instances on the same Web form by implementing the INamingContainer Interface. This basically helps prevent id clashes in the same namespace.
If you want to access another control set a property on it to expose the data you want made public.
Build Composite Controls

How can I find and interact with a user control on a page from a separate user control?

I have an aspx page that has two different user controls. I want to find user control A and be able to set properties, etc., from user control B.
I was thinking I could do something like this:
Dim CMFilters As Control = Me.Parent.FindControl("CMFilters")
...but that doesnt work to be able to set properties and call methods. I somehow need to get the user control and and declare it as that user control type.
You should not make control A dependent of control B.
Instead, read and write the properties of both controls from the page that contains the controls.
So expose all properties you want to set in both controls A and B as public properties (read/write or read-only) and connect them e.g. in the Page_Load event of your page.
There's an article here explaining how to do it.
I would like to note that this is not considered a good design. This is the type of thing that's referred to as a "code smell". As a general rule of thumb, objects should be designed so that they are unaware of other objects, and can function independently of other objects.
A better approach would be to simply let the objects do what they do independently, and let the page class handle the interactions, since each is a child element of the page.
This design is listed as a code smell here:
http://www.codinghorror.com/blog/2006/05/code-smells.html
Inappropriate Intimacy
Watch out for classes that spend too much time together, or classes
that interface in inappropriate ways. Classes should know as little as
possible about each other.

Base class for UserControls that automatically provides an update panel?

Let's say I am building a bunch of UserControls in an ASP.Net Web application, and that all those user controls inherit from a custom base class that I have created, which in turn inherits from System.Web.UI.UserControl.
Now let's say that I (via my custom base class) want the contents of each user control to be automatically wrapped in an UpdatePanel control.
How would I do this? I suspect it involves overriding the CreateControlCollection and/or CreateChildControls methods in my custom base class, but I am not sure how.
Any ideas?
The easiest thing would be to just inherit your base control from UpdatePanel. Then every child control you add to it is automatically inside of one, and you don't need to worry about which container they are being placed in.
If for whatever reason this is not possible, you could override any number of methods (OnLoad, OnPreRender, CreateChildControls, etc.) to add some logic where you move any child controls that were added to your base control's control collection, to an UpdatePanel control that your control contains.

Using a Base Controller for obtaining Common ViewData

I am working on an ASP.NET MVC application that contains a header and menu on each page. The menu and header are dynamic. In other words, the menu items and header information are determined at runtime.
My initial thought is to build a base Controller from which all other controllers derive. In the base controller, I will obtain the menu and header data and insert the required information into the ViewData. Finally, I will use a ViewUserControl to display the header and menu through a master page template.
So, I'm trying to determine the best practice for building such functionality. Also, if this is the recommended approach, which method should I override (I'm guessing Execute) when obtaining the data for insertion into the ViewData.
I'm sure this is a common scenario, so any advice/best-practices would be appreciated! Thanks in advance!
EDIT:
I did find the following resources after posting this (of course), but any additional anecdotes would be awesome!
http://www.singingeels.com/Blogs/Nullable/2008/08/14/How_to_Handle_Side_Content_in_ASPNET_MVC.aspx
How do you use usercontrols in asp.net mvc that display an "island" of data?
Depends on where your information is coming from. We have standard view data that we use to generate some of the information we have on screen that we create in just this fashion. It works well and is easily maintained. We override the View method to implement strongly typed view names and use this information to retrieve some of the data that the master page requires as well.
You could write a helper extension to render the header/menu
That way you could have it show in different places in the view should you need to, but only one place for maintenance.
public static HtmlString MainMenu(this HtmlHelper helper)
Use a base controller class to implement generell filter methods. The controller class implements some filter interfaces IActionFilter, IAuthorizationFilter, IExceptionFilter and IResultFilter which are usefull to implement some common behavior for all controllers.
If the menu data is the same on all pages but different for each unique user.
Generate the menudata in an OnAuthorization or Initialize method of your controller base class. First will be called on authorization. Initialize will be called before every action method. You have access to ViewData Context. Generate the menudata there.
Put the view content for menu and header into the master page and access generated ViewData there.
I tackled a similar design challenge a couple months ago - implementing a breadcrumb feature that changes as user navigates from page to page.
I overrided the OnActionExecuting method to gather the breadcrumbs and store them in ViewData (I use the name of the action as the breadCrumb of the view). Then I updated the Master page to include a user control that takes the ViewData and renders the breadcrumbs.
One thing to be aware is that if you were using the default ASP.NET MVC error handling attribute [HandleError] and your error page is using the same Master page that attempts to read the ViewData, you will soon find out that you can't access ViewData from your error page and it will raise an exception. Depending on whether you need the ViewData for failure scenarios, the viable solution is to use a separate Master page or do this: How do I pass ViewData to a HandleError View?
I'll answer your question with another question. Will the base controller have to determine what type it really is in order to generate the proper menu data? If so, then you're defeating the purpose of polymorphism and the code to generate the data should go in each controller, perhaps in OnActionExecuting if the menu is the same for all actions. Pushing it back down into a parent class seems likely to end up with some switch statement in the parent class doing what each derived controller really ought to take care of.

Resources