October 2006 - Posts
I've been writing some custom controls and I have several controls in a composite controls that have links. Take this custom CompositeControl, for example, where I define a calendar:
private Calendar _calendarControl =null;
// properties to expose calendar properties
protected override void CreateChildControls()
{
this.Controls.Clear();
_calendarControl = new Calendar();
//assign default properties
}
protected override void Render(HtmlTextWriter writer)
{
//render table HTML
_calendarControl.Render(writer)
//other controls rendered, as well as table
}
The problem was none of the links worked! They rendered as static text. This happened with linkbuttons as well. I had never seen that before; changing to defining a Table server control in the CreateChildControls class, and adding the controls there, worked well to correct the problem.
If you need to perform statements at the database level for a SQL Server 2005 Express database, what do you do? With an attached database, you can reference it by name, but Express databases aren't necessarily attached, so in a detached state, how do you handle this? The full path to the file is acceptable to reference it. Consider the following command:
backup log [C:\DOCUMENTS AND SETTINGS\BRIAN\MY DOCUMENTS\VISUAL STUDIO 2005\PROJECTS\RELIGION.NET\RELIGION\RELIGION.MDF] with truncate_only;
Make sure that you enclose the name with [] for it to work. This will reference the detached database successfully.
http://www.codeplex.com is a great community site with many developers contributing various types of awesome applications. That is where I stored my Reminder.NET project, which was discussed on the Extreme Programming blog.
One of them that I downloaded and tried is the Lightbox.NET utility, which wraps the existing javascript file project (called Lightbox) into a .NET class. This is a great feature because it uses javascript to open up modal forms, which you can see the picture in full-screen in a modal form, then press x or click the close link to close it.
The utility will install javascript and other needed files in a lightboxNet folder in the web project, which is created by the component itself upon running the project you reference it in.
I've been working with the combobox, and it appears that the FlatStyle property has some bugs in how it renders the combobox. When setting the property to FlatStyle.Flat, it doesn't render correctly when using anchoring to resize the item; instead, you can see two items, one with the original painting of the flat combobox, then the resized combobox under windows style. When you click on the drop down, this fixes the problem, until it is resized again.
Using the popup setting works better, but it seems to have some bugs as well.
.NET 2.0 has a WebBrowser control for windows development. Using this control, you can request and view HTML content parsed as you would see it in IE or Firefox. It's really simple to use; pass a Uri to the Navigate method, and it navigates to both http sites, as well as local HTML content (I passed in a c:\...\htmlfile.html path and it worked). When working with Uri, the constructor of uri takes an absolute and relative url in it, so this is really straightforward.
I deployed changes to a site, and it turns out I had an error in the configuration file. I made a change, deployed the new file, and still had an error. I tried to figure out what it was; the error messaged showed the new configuration file, but still had the same previous error. In deploying all my ASP.NET pages, it reloaded the compiled files for the pages, and used the new configuration, correcting the problem. I had remembered some about this from reading Dino Esposito's book; because the site wasn't recompiled, it was still using my previous configuration file. Still seems strange though.
If you need to encrypt or decrypt the connection string, I found a great resource that you can access. It is available on: http://aspnet.4guysfromrolla.com/articles/021506-1.aspx. It shows you how to do it programmatically, and through the aspnet_regiis utility.
One point to note; I used the RSA provider, and I got an error when deploying to a remote host. I don't know if the encryption needs to be established on the remote server, or if it was that the provider was not installed on the host machine. I can't think it would be the latter, so ensure that it works before you deploy.
I've been currently managing a couple of web sites for different organizations, which I volunteer to do web work for. It can be hard to manage multiple sites for the same organization, especially with the redundancy you see. I've been looking at ways of reducing it, and here are a couple of things that I found.
- If you can, use a VirtualPathProvider; you can have multiple sites all in the same provider, which use the same bin, App_Code, and other folders. However, it's not that straight-forward to setup, and the lack of separation of certain folders may deter it. See more at: http://msdn2.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx
- Name the files the same that you would use; for instance, name the master page the same (I use site.master) so that if you can copy/paste pages over to the new site (copying whole functionality), you have less to change in the future.
- I've been duplicating tables/views/stored procedures to isolate changes; however, it is useful to create a custom provider to handle the functionality, if it is widely used. For a working example, check out the example at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnetprovmod_prt9.asp. It is some work, but if you reuse that functionality a lot, it really pays off.
- Reuse in custom controls works well; however, it can be time consuming.
- Only create custom features when you definitely have duplication; "i may in the future" is probably not worth doing until it actually occurs. There was a statistic out that users actually use 10% of the system 90% of the time, so some code isn't worth writing. Though, if you are doing it for learning, it doesn't hurt.
These were the ones that I found. I will add as I remember or find new ideas. Any comments on ideas are welcome.
When using generics, and you get an error "Given Expression is Never of <X> Type", it is usually how it is assigned. For example, I was trying to do this:
public IInterface<T> CreateProvider<T>()
{
if (typeof(T) is ObjectA)
return new ObjectAProvider() as IInterface<T>;
.
.
.
else
throw new NotImplementedException();
}
This was a problem; I got the above error. When digging for an answer, I found this as the solution: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=111314&SiteID=1 Type has an IsAssignableFrom method, which I was able to do successfully:
if (typeof(T).IsAssignableFrom(typeof(ObjectA)))
return new ObjectAProvider() as IInterface<T>;
.
.
.
else
throw new NotImplementedException();