I just came across a post on the asp.net forums about wanting to control how view state is saved with a page. As we all know by default view state is rendered as a hidden element on the page, but that can easily be changed and you can set your pages up so that viewstate is saved to the cache, session, or the database.
The first step I would suggest is to create a base page for your aspx pages and override two methods, LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium. As you can tell by their names, these are the two methods responsible for determining how view state is actually handled in the page.
The following is what I put together for the SavePageStateToPersistenceMedium method. This method will take the viewstate object that is passed in and run it through a special formatter to serialize the object. According to MSDN, the LosFormatter object is designed for highly compact ASCII format serialization and serializes the view state for a WebForm page. Once the object has been serialized, I then save the viewstate to a session variable. I used a session variable for demonstration purposes only, but once the object has been serialized, it can be saved to session, cache, or a database. Here is the code for the SavePageStateToPersistenceMedium method:
protected override void SavePageStateToPersistenceMedium(object viewState)
{
// Saftey check to ensure we have a valid object
if( viewState==null) return;
// Instantiate the formatter used by the framework to serialize viewstate
LosFormatter formatter = new LosFormatter();
// We need a stream writer to write the viewstate text to
StringWriter writer = new StringWriter();
try
{
// Serialize our view state to the string writer
formatter.Serialize( writer, viewState );
// Save the view state to session state
Session["ViewState"] = writer.ToString();
}
catch
{
throw new HttpException("Invalid view state");
}
finally
{
// Clean up
writer.Close();
}
}
The next step is to override the LoadPageStateFromPersistenceMedium and load the viewstate data from the same location it was saved to in the SavePageStateToPersistenceMedium method. This is where we take the data out of our session object and then deserialie the data. Here is the code for the LoadPageStateFromPersistenceMedium method:
protected override object LoadPageStateFromPersistenceMedium()
{
object viewStateObj = null;
// Instantiate the formatter used by the framework to serialize viewstate
LosFormatter formatter = new LosFormatter();
// Grab our viewstate from session
object sessionViewState = Session["ViewState"];
try
{
// Deserialize the object
viewStateObj = formatter.Deserialize(sessionViewState.ToString());
}
catch
{
throw new HttpException("Invalid view state");
}
return viewStateObj;
}
Once you implement this code, you will notice that a hidden viewstate field is still dumped out into your page, but its empty.
I haven't seen many people blog about this, but can they co-exist? Has anyone done it and if so, what kind of problems have you run into?
While listening to Fritz Onion today on his first web cast, he told us about a URL that just made my day:
http://pluralsight.com/wiki/default.aspx/Fritz.AspNetWithoutWebProjects
This is an article that I wish I found about 8 months ago! It goes into details on how to setup asp.net appliations without using a web project from visual studio. I converted one of my projects and it worked like a champ! Debugging and all!
I am going to keep the settings this way for a while to see if I find any “Gotcha's“ and I will report back if I do. In the mean time, if you have been using this method for a while and know of any gotcha's, please let me know.
Check out this blog entry if you are interested in providing feedback on how the new asp.net directory names should be setup. I know I have seen quite a bit of discussion on these names and how they should be configured through a config file, well now is your chance to provide more feedback to the product team....
After reading Scotts post on CSS book recommendations, I want to try to get similiar feedback on SQL Server books. This is one area that I would really like to learn more about. I know enough about SQL server to get me around as I have been working with SQL Server for about 6 years now, but never really got into how to setup and properly configure SQL Server as I have always had the help of a DBA. Now, I have some side projects going on, where I really need to understand SQL Server better. I want to learn more about indexing, performace, security and just basic better server management.
So what book do you recommend and why?
I finally got my CD's from MSDN with the new Visual Studio 2005 beta's, so I installed the beta today on a virtual PC image and ran into some problems once I got everything installed.
Here are links to the two error messages I am getting.
The first error message is when I start Visual Studio
http://dotnetjunkies.com/WebLog/images/dotnetjunkies_com/whoiskb/1574/o_VSBetaError1.jpg
This second error message I get when I go to try to create any application
http://dotnetjunkies.com/WebLog/images/dotnetjunkies_com/whoiskb/1574/o_VSBetaError2.jpg
Does anyone have any work arounds for these problems?
I just came across this review and the screen shots that they shown I have never seen before, so I thought I would share this with everyone:
http://www.extremetech.com/slideshow/0,2394,l=&s=25501&a=126556,00.asp
I think the new alt-tab looks really cool.....
http://www.extremetech.com/slideshow_viewer/0,2393,l=&s=25501&a=126556&po=6,00.asp
My company decided to try a little “experiment” for our latest project. We have a pretty large .Net project going on so they decided to stick all the programmers (11 of us) into a big conference room, so that we can work more effeciently. Before being moved into this room, we would be all over the place trying to track people down to talk about different issues, and now we are all in the same room. Plus, we are able to completely focus on our goals at hand, and are not bothered by other projects as well. All in all, it has worked out pretty well for us. There are times where it can get tough when there are multiple conversations going on and you don't want to participate, but we also utilize a small conference room next door to try to keep distractions down.
One thing that everybody has done though is invested in some good headphones so that you can tune stuff out. Lately I have started listening to some streaming radio from the Internet and was curious if anyone knows of some good places to find a good variety of stations. I have a few stations picked out from my WMP and MSN Entertainment, but would like to broaden my play list.
Any recommendations?
I just saw Dino's post on his wish list for ASP.Net tracing and saw he wanted the tracing window in a popup window. This is something we wanted for our web apps as well, so here is how I tackled the problem. I know its not perfect and the output is still put out to the main page, but the reason why I had to come up with this scheme was because in our web apps we are using absolute positioning. When we did this our content was placed over the top of the trace div.
Here is the code.....
============== Parent ASPX Page =================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<TITLE>Parent Page</TITLE>
<SCRIPT>
function SetupTraceWindow()
{
var div = GetTraceDiv();
if(div==null) return;
win = window.open("Trace.htm","Trace");
if(win==null) windown.status="The trace window was blocked. Please make sure popup blockers are off.";
}
// Called by the child window to get the html of the trace output and also hides the trace output on the parent page.
function GetTraceHTML()
{
var div = GetTraceDiv();
if(div==null) return "";
var html = div.outerHTML;
div.style.display="none";
return html;
}
// Returns the div that is created by the ASP.Net trace function
function GetTraceDiv()
{
var div = document.getElementById('__asptrace');
return div;
}
</SCRIPT>
</HEAD>
<BODY ms_positioning="GridLayout" onload="SetupTraceWindow();">
<FORM id="Form1" method="post" runat="server">
This is the parent page.
</FORM>
</BODY>
</HTML>
======================= Trace.htm =======================
<!DOCTYPE html public "-//w3c//dtd html 4.0 transitional//en">
<HTML>
<HEAD>
<TITLE>ASP.Net Trace Output Popup</TITLE>
<SCRIPT language="javascript">
function SetupTrace()
{
var html = opener.GetTraceHTML();
document.writeln(html);
opener.focus();
}
</SCRIPT>
</HEAD>
<BODY onload="SetupTrace()">
<DIV id="myTrace"></DIV>
</BODY>
</HTML>
We had discussion today at work about how we should use regions, and it was funny to see everyone had a different opinion on the matter. Here is what I usually do:
Class - Wrapped around the class
Private Members
Constructors
Properties
Public Methods
Private Methods
Base OverRides
Interface Definitions
What do you do?
I got an interesting error today, but first some background. I am creating a server control that inherits from DropDownList and internally I also add an image server control next to it. I add this image dynamically by trying to add it to the controls collection of the dropdownlist. When I try to do this I get an error saying that you cannot add controls to the dropdownlist controls collection.
When I look at the dropdownlist in reflector I noticed that the CreateControlsCollection has been overridden and an emptycontrolcollection is created. What this emptycontrolcollection does is enforce the rule that you cannot add controls to the control collection.
Now, I was able to override the CreateControlsCollection for my control, but I am curious as to why they did this. Does anyone know why? Its strange because I did this same functionality with a textbox and had no problems.
This weekend I worked on implementing sort logic in the datagrid. I wanted to use the built in sorting that the datagrid provided and was a little dissapointed. I will post the code below but I was suprised by two things:
- No indication of which direction the sort should be
- No easy way to get to the column header the sort came from
Not that these two were show stoppers or anything, but I guess I was just expecting those two things to be there.
But anway, I have posted the code below that I came up with to implement bi-directional sorting in my datagrid. This code will perform the sort on my dataview plus attempt to add an image to the column header. If anyone has some feedback on an easier way to do this in case I missed something, please share.
Here is the code behind code, and these are just the routines that are involved in my sort logic:
private void grdContacts_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
String sort = e.SortExpression;
String vsort = GetSortViewState();
// If the current sort expression is the same as the one in view state that means they are resorting
// the same column and we need to sort in the opposite direction. Once we add the DESC flag to the sort, if
// the user clicks on the same column again, it won't won't match and the sort expression will be reset to
// the same column but with ASC order.
if(sort==vsort)
{
SetColumnSortIcon(sort,false);
sort = String.Concat(sort," Desc");
}
else
{
SetColumnSortIcon(sort,true);
}
// Save off our sort so that on a post back we can determine what direction the sort should be if they click the same column
SetSortViewState(sort);
BindPageData(sort);
}
private void SetColumnSortIcon(String sortExpression, bool ascending)
{
String imgAsc = String.Concat(" ","<img border=0 src=",Request.ApplicationPath,"/images/sortasc.gif",">");
String imgDesc = String.Concat(" ","<img border=0 src=",Request.ApplicationPath,"/images/sortdesc.gif",">");
// Loop through all of the columns in the datagrid and compare the sort expressions to find a column match.
// Once the column match has been found, lets add our image HTML to the header to show which column is being sorted on.
foreach(DataGridColumn col in grdContacts.Columns)
{
if(col.SortExpression==sortExpression)
{
String text = col.HeaderText;
// The is an indicator to know if we have already added an image tag. If it exists in the header that means
// we already have an icon in the header and the current column was previously sorted and the direction of the sort
// has changed. This could cause a problem if we use the text in our header to force a space
int pos = col.HeaderText.IndexOf(" ");
if(pos>-1) text = col.HeaderText.Substring(0,pos);
if(ascending)
col.HeaderText = String.Concat(text,imgAsc);
else
col.HeaderText = String.Concat(text,imgDesc);
break;
}
}
}
#endregion
#region Private methods
private String GetSortViewState()
{
object obj = ViewState["sortexpression"];
return (obj==null) ? String.Empty : (string)obj;
}
private void SetSortViewState(String sort)
{
ViewState.Add("sortexpression",sort);
}
private void ClearSortViewState()
{
ViewState.Remove("sortexpression");
}
#endregion
This just came across one of the MSDN blogs I watch and thought I would share just in case anyone missed it.
http://blogs.msdn.com/csharpfaq
The C# team, along with members of the C# Community, are answering Frequently Asked Questions (FAQs) via a web log (blog) set up at http://blogs.msdn.com/csharpfaq. Entries from that site will be displayed on MSDN through this page with only a short delay due to caching.
I have finally decided to get commited to finish up my MCSD and also get certified for the MCSD for Microsoft .NET.
The first exam on my list is the 70-229 Sql Server exam. I am looking at mainly using books as my guide to study for this exam and was curious as to what thoughts people have to the books that are out there and how they related to the exam.
Any tips/input would be extremely appreciated.....I heard this test is one of the tougher ones....
I just got done reading Peter's post about a limitation he ran into about sharing session variables across web applications, and thought I would blog about an approach we recently took with one of our web apps to get around this problem.
We got our solution from this MSDN article, which shows you how to separate out your web applications into smaller web applications. The first step is to create an IIS application and this is the root of your web application. Under this root, you create your smaller modules as virtual directories, but don't configure them to have their own web application. (now refer to the article to see how to make this friendly to VS.Net) Now, because the root is setup as a web application, this is the guy that manages your session state for all of the modules below it, and our session state can no be shared across all of the different modules.
Now I understand that this won't be for everyone, but in our case it has worked out great. We are currently building a pretty large web system that consists of various modules, and one requirement we would like to satisfy is to allow us to deploy each module separately but also be able to share session state across all of the modules. We have a main IIS root application and each module is setup as a virtual directory under it. The root, and each of the modules each has its own ASP.NET web project and can be compiled separately. This would allow us to only update specific modules when needed. So now when we make changes to just module A, we can build the web app for module A, plus all of its supporting assemblies. By having all of the modules under the root, all of the modules can now also share session state.
Hope this makes sense, but if it doesn't the MSDN article will really clear things up since they provide good instructions on how to get everything configured.
UPDATE: Fixed the MSDN link
I just got done reading through this MSDN article about IDE enhancements that are going to be released with whidbey. While all of these enhancements are nice, what are some tools that you would like to see added to the IDE to increase our productivity?
Note: looks like I had a problem with the date/time stamp that was put on my previous post about this.....
We are currently taking our current flagship product which is an illustration system for life insurance companies and doing a complete redesign/rewrite for .Net. In our original system we used Active Reports for the reporting tool and have been fairly happy with it.
Moving forward though we have found that Active Reports.Net actually runs slower than its non .Net version. Our reporting was always the slowest part of our system and we were hoping to figure out ways to speed up our reports, so that news was not promising.
I was curious as to see what others are doing in regards to reporting for their .Net applications. Enterprise solutions such as the new SQL Server reporting tool is not an option as the license and framework must support royalty free distribution to desktops. The output must support PDF, and having an RTF option would be nice.
I just saw Williams post about a bug in Visual Studio and thought I would do a follow up to the article I posted last week about discovering the same bug.
We have an agreement with some local MS consultants who were able to try to get some more information for us. It was recommended to us to follow the Team Development with Visual Studio.net and VSS white paper, as that paper gives us guidelines to avoid these dependency problems. The white paper suggests using project references as much as possible, implement a daily build processes that the assemblies are outputed to, and share that location so developers could pull the assemblies down, or require developers to build the assemblies locally on their machines. These steps are required as it is advised not to check the assemblies into VSS. They also suggestion to break your solutions up to logical groups so that you can create establish your project references. This becomes more and more difficult as you have a project that has 50 projects in it, and this is where we are. Right now the project we are working will end up having quite a number of projects, and we are doing our best to set them up to use project references but there are times where we are going to have to use file references and that is where the common output directory would be very helpful.
We also came up with a different solution that is working out well for us, that I don't believe is mentioned in the whitepaper, and that is to take advantage of the post build event in each project. In projects that we know are going to be referenced by another solution through a file reference, we setup a post build event to do an xcopy of the new dll to this common output directory. We use the following command:
XCOPY "$(TargetPath)" "\CommonAssemblies\" /R/Y
Where the macro $(TargetPath) has the fully qualified path to the dll that you just built. At this point, we do check in the assemblies from our CommonAssemblies folder.
One final thought, while we classify this as a bug, apparently MS does not and they do not have plans to fix this. The reason we were given was because there are workaround for the problem, and the issue results from not implementing VSS best practices.
Not really sure which one is worse..... First some background, and I will try to keep it simple....
I have two projects in a solution, A and B where A references B as a project reference in the same solution. Now, we also have the output for the projects pointing to the same assemblies folder(Server Assemblies below) because there is a third project C that references A&B as a file reference.
In case it helps, here is the folder structure for these projects:
Client
Assemblies
Project C
Server
Assemblies
Two Projects (solution is here)
ProjectA
ProjectB
So now the knowledge base article that shed some light on our problem: http://support.microsoft.com/default.aspx?scid=kb;en-us;313512
Title: BUG: "Could Not Copy Temporary Files to the Output Directory" Error Message When You Build a Solution That Contains Multiple Projects
Cause: This problem may occur when one of the assemblies that you compile is larger than 64 kilobytes (KB) and the following conditions is true: Your solution contains projects that are compiled to the same output folder.
Now, our problem is because we have the output directory of projects A&B pointing to the Server/Assemblies folder and our thought there was it would make our file references from ProjectC easier. We read a VSS article that mentioned not to check in the debug/release folders, but we see a need to check in the assemblies at some point so that projects that do file references can work when you “pull latest”. We want to pull the latest on Server/Assemblies, Client(recursively) and have things run for the projects in the client folder.
What I was wondering though is this, how would others structure their projects and how does it impact your VSS structure and build/development processes?
Thanks
While recently developing a web application, I came across an interesting problem that I haven't seen before. Part of our application uses session cookies to keep track of data. One cookie is actually set by ASP.net forms authentication, and we set a few others of our own. Initially we had no problems with using these variables......
Then entered the fraameset.....
We recently had a user that was entering our site through a fraameset. They had one of the fraames pointing to our site, and they said the site was unusable. After setting up a quick test and tried to log into our site (which sets the authentication cookie), I kept getting redirected back to the login page which kind of hinted that the cookies were not being kept around. I then found a knowledge base article, and found out that this was a design feature with Internet Explorer, http://support.microsoft.com/default.aspx?scid=kb;EN-US;323752
The problem arises when you have a fraame set in which the fraame points to a different website that uses a different top level domain. In my test I had the fraameset hosted site.com, and one of the fraames pointed to mysite.com.
I took the easy solution that the KB article mentions and added the HTTP header to IIS and the problem was fixed.
BTW, I did not spell fraameset or fraame wrong. The .text engine would not let me post with those words in the message.