Sunday, November 30, 2008
Free refactor in Visual Studio
Also, DevExpress provided a free Refactor! for VS 2005. I found it originally from Free refactoring tools for ASP.NET code in Visual Studio 2005. Not sure if it works fine in VS 2008.
Thursday, November 27, 2008
Output GridView to Excel
To output GridView or DataGrid to Excel file, one can use:
- Use oledb classes to create and write data into Excel file
- If datasource is DataTable or DataSet, use writeXml method
- Use RenderControl method of Control class
Taking the latest way as an example
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
htmlWrite.WriteLine("<table cellspacing=\"0\", rules=\"all\", border=\"1\">");
gvResult.RenderControl(htmlWrite);
htmlWrite.WriteLine("</table>");
System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
String fileName = Guid.NewGuid().ToString() + ".xls";
String path = @"c:\temp\";
File.WriteAllText(path + fileName, stringWrite.ToString(), encoding);
Data Binding Expression
One can use objects as datasouce and bind to it by using GridView control.
<asp:Label ID="lbNumber" runat="server" Text='<%#Eval("Number") %>'></asp:Label>
Here "Number" is a property of the datasouce object.
If the field of datasouce is a strong typed object itself, we can also use like this:
<asp:Label ID="lbProjectType" runat="server" Text='<%#Eval("ProjectType.Type") %>'></asp:Label>
"ProjectType" is a property of datasouce object and is an object which has a member called "Type".
If we want to do some complex work during binding, we can also do it at code behind. One approach is writing code in RowDataBinding or ItemDataBinding event handler, The other way is define a method and call it declaratively. Below is an example for the later:
<asp:Label ID="lbScheduledDate" runat="server" Text='<%# DisplayDate(Eval("ScheduledFrom"), Eval("ScheduledTo")) %>'></asp:Label>
Thursday, November 6, 2008
IIS can not access by anonymous account
After a couple of hours searching on internet. I find the cause. That's because my IUSR_computername doesn't has the right of "log on locally". So I go to:
controlpanel -> Admin Tool -> Local Security Settings -> Local Policies -> User Rights Assignment and find that Guest group doesn't have the permission of "Log on locally". Since IUSER_computername is memeber of Guest group. It doesn't have it as well.
I removed it from Guest group and add it to User group to make this account can be log on locally.
Auth Diagnostics 1.0 is a good tool to find IIS authentication issues. It can be downloaded here
Also, there is a good guideline here to help troubleshooting such issue.
Sunday, November 2, 2008
Create Template Dynamically - Part 2
Another article, Using Templated Controls Programmatically shows how to use CompiledTemplateBuilder to do the something. Actually, CompiledTemplateBuilder class is an implement of ITemplate, in it's constructor, a method need to be passed in. And later on, it's initiateIn method will call the method to finish render task.