пятница, 30 апреля 2010 г.

How to: Disable ASP.NET Themes at Page Level




I've created a theme Css for my Asp.net 2.0 web application. The web.config Pages Theme key is set to the theme name. Everything seams to work fine except for any page that I want to disable the master theme and apply another stylesheet.css.

According to the documentation I've read. I should be able to disable the theme by setting the "enabletheming" attribute on the aspx Page to false. This doesn't seam to work. When I look at the source of the generated page I can see the link to the new stylesheet that I want but just prior to the body tag I also see a reference to asp.net theme stylesheet that I'm tring to disable.

Is this a bug? I've tried everything I know of to disable the theme at the page level.




The solution is to set Theme="" on the page:

<%@ Page Language="C#" EnableTheming="false" Theme="" %>

How to add a vertical or horizontal scrollbar to a ASP .NET Panel control?



Definition and Usage

The ScrollBars property is used to set or return the position and visibility of scroll bars in the Panel control


Syntax


Some Content

AttributeDescription
valueSpecifies the which scrollbars are shown or not shown.
Possible values:
  • None - Default. No scroll bars are shown
  • Horizontal - Only a horizontal scroll bar is shown
  • Vertical - Only a vertical scroll bar is shown
  • Both - Horizontal and vertical scroll bars are shown
  • Auto - Horizontal, vertical, or both scroll bars are shown if needed


Example

The following example sets the ScrollBars property to "Auto" in a Panel control:

<form runat="server">
<asp:Panel id="pan1" runat="server"
Height="100px" ScrollBars="Auto">

Some content

</asp:Panel>
</form>

вторник, 6 апреля 2010 г.

How to use a Save File Dialog in WPF?

The SaveFileDialog class defined in Microsoft.Win32 namespace represents Windows Save File Dialog control.


The following code snippet creates a SaveFileDialog, sets its default extension and fiter properties and calls ShowDialog method that displays SaveFileDialog control.



Microsoft.Win32.
SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = 
".txt";
dlg.Filter = 
"Text documents (.txt)|*.txt"if (dlg.ShowDialog() == true )
   string filename = dlg.FileName;
}

How to serialize an object to XML by using Visual C#

Serialzation


Now here's the cool part. This is how serialization works:

ShoppingList myList = new ShoppingList();
myList.AddItem( new Item( "eggs",1.49 ) );
myList.AddItem( new Item( "ground beef",3.69 ) );
myList.AddItem( new Item( "bread",0.89 ) );


// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xml" );
s.Serialize( w, myList );
w.Close();

// Deserialization
ShoppingList newList;
TextReader r = new StreamReader( "list.xml" );
newList = (ShoppingList)s.Deserialize( r );
r.Close();

The first chunk of code is simply creating an instance of the ShoppingList class and populating it. After that, we have the serialization part. Here is where the object gets converted into XML. As you can see, all it requires is the use of the XmlSerializer class which is set to serialize anything of type ShoppingList (look at the constructor). The serializer does its work when the Serialize method is called and will output XML to any stream. In this case, we have it output to a file.
Next there is the deserialization part. Here we use the same serializer (since it's set to the right type) and we read in an XML file and the Deserialize method will create the appropriate ShoppingList class object. This code sample shows serialization from a file, but you could just as easily do it from an http stream.

четверг, 1 апреля 2010 г.

ASP.NET Web sites That Have EDMX files do not compile and deploy in partial trust



After finishing one of the websites that were developed using Entity framework , I published  it to godaddy hosting. When i tried to open the newly published site, i got the following error:


Parser Error Message: Type 'System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider' cannot be instantiated under a partially trusted security policy (AllowPartiallyTrustedCallersAttribute is not present on the target assembly).
After some investigation , I found that the mentioned error was caused by the following build Provider section that were placed in web.config by VS 2008:
<buildproviders>
<add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"></add></buildproviders>
The “EntityDesignerBuildProvider” class is contained in  System.Data.Entity.Design.dll which does not have the AllowPartiallyTrustedCallersAttribute and so any call to it in the partial trust will fail.
when i removed the mentioned “buildProviders” section and refreshed the site, i got the following error:
The specified default EntityContainer name 'entity model name here' could not be found in the mapping and metadata information.
Parameter name: defaultContainerName.
After that i started to think that removing the mentioned section prevented the complier from compiling the entity model.
I think the problem is that since i didn’t precompiled the website and just copied it to the hosting server(using “copy website” option), the runtime will try to compile it dynamically (including the entity model “edmx" file) hence the first  mentioned error will occur.
many solutions comes to my head, like pre-compiling the website to avoid dynamic compilations on the hosting server, or just moving the entity model from App_code to a separate class library project which allow me to only build the class library project and publish the dll to the website Bin directory.
precompiling the website wasn’t an option for me because i know that i still need to apply partial updates to it and so i can’t re-upload the whole stuff every time i change something in the website.
So i selected the second solution.I goes a head and moved the entity model file “edmx” from the website App_Code  to a separate class library project.then compiled entity model project and added it’s resulted dll to the website Bin directory.
Problem solved :)
Hope that help you !