ViewData and TempData are both used to pass data from Controller into View in
ASP.NET MVC model. This article is trying to point out their's difference.
ViewData is only available in current request but TempData, which stored in user
session, whose life will be around current and the right next requests. In the
other word, if you are trying to share data between two continued request or
during a redirection then TempData will be your choice.
BTW, to ensure you get
the data by either ViewData or TempData you can use the code
<%=ViewData["name"]
?? TempData["name"]%>
Sunday, 31 January 2010
Tuesday, 26 January 2010
List<T> working with DataGridView
As you all know you can bind a List<T> into a DataGridView in .net 3.5. The DataGridView even automatically allows you to edit the bound List<T> data. But there are still some tips when working with DataGridView. This article is a quick guide to let you know how to make this kind of generic binding working properly even when you programmatically changing the source data.
Case 1: when you edited the data in a bound List<T>, simply call DataGridView.Refresh() to allow you changes be shown. Or you can set the DataSource of the DataGridView to null and reset the DataSource to the List<T>.
Case 2: when you added or removed records from the List<T> then you cannot refresh the DataGridView to see your changes, in this case you have to re-bind the DataSource (by re-bind I mean set the DataSource to null and then set it back to the List<T> otherwise the re-bind will not happen since the DataGridView will think the datasource didn't change)
Case 3: When you directly bind a List<T> to a DataGridView you will find it doesn't allow you adding or deleting record from the UI even you set the AllowUserToAddRows and AllowUserToDeleteRows. To allow you do these things in DataGridView UI we are going to use another Type BindingSource which implemented related interface to allow DataGridView add/edit/delete data from its data source. The sample code:
List<T> data = new List<T>();
BindingSource binding = new BindingSource();
inding.DataSource = data;
DataGridView.DataSource = binding;
Go back to Case 1 and 2, when you are using BindingSource instead of directly use List<T> now you have another way to refresh your DataGridView, BindingSource.ResetBindings(bool) which forces the DataGridView to reread all the items from the List<T> and refresh their displayed values.
Case 1: when you edited the data in a bound List<T>, simply call DataGridView.Refresh() to allow you changes be shown. Or you can set the DataSource of the DataGridView to null and reset the DataSource to the List<T>.
Case 2: when you added or removed records from the List<T> then you cannot refresh the DataGridView to see your changes, in this case you have to re-bind the DataSource (by re-bind I mean set the DataSource to null and then set it back to the List<T> otherwise the re-bind will not happen since the DataGridView will think the datasource didn't change)
Case 3: When you directly bind a List<T> to a DataGridView you will find it doesn't allow you adding or deleting record from the UI even you set the AllowUserToAddRows and AllowUserToDeleteRows. To allow you do these things in DataGridView UI we are going to use another Type BindingSource which implemented related interface to allow DataGridView add/edit/delete data from its data source. The sample code:
List<T> data = new List<T>();
BindingSource binding = new BindingSource();
inding.DataSource = data;
DataGridView.DataSource = binding;
Go back to Case 1 and 2, when you are using BindingSource instead of directly use List<T> now you have another way to refresh your DataGridView, BindingSource.ResetBindings(bool) which forces the DataGridView to reread all the items from the List<T> and refresh their displayed values.
Wednesday, 20 January 2010
How to add a Form into Tab control
Sometimes for reusing your old code you may want to add a form instead of a user control into a tab. To do so you need modify some properties of your form otherwise your form may not be showing properly.
The properties are:
FormBorderStyle = None; //remove the form border and title area, make it like a flat piece
TopLevel = false; //TopLevel control cannot be embeded
WindowState = Normal; //If you are going to set the Dock value, WindowState cannot be Maximized
The last thing is do not forget call form.Show(), otherwise the form is not going to render itself.
The properties are:
FormBorderStyle = None; //remove the form border and title area, make it like a flat piece
TopLevel = false; //TopLevel control cannot be embeded
WindowState = Normal; //If you are going to set the Dock value, WindowState cannot be Maximized
The last thing is do not forget call form.Show(), otherwise the form is not going to render itself.
Tuesday, 12 January 2010
How to set K2 login model for Joomla 1.5
If you are not a patient man just like me then this article gonna help you.
Step1, download K2 full package and install it
Step2, enable K2 login model
Step3, assign existed user to K2 user group
Step4, to let the user publish new items, you have to change the related K2 user group to allow item editing, add items, editing level (All or Own), and publish items
You can ignore all the other settings and log in as the user, and then you can see a link Add New Item below your login user name now.
In case you want to know more about implement K2: A quick step-by-step guide on installing and setting up K2.
Step1, download K2 full package and install it
Step2, enable K2 login model
Step3, assign existed user to K2 user group
Step4, to let the user publish new items, you have to change the related K2 user group to allow item editing, add items, editing level (All or Own), and publish items
You can ignore all the other settings and log in as the user, and then you can see a link Add New Item below your login user name now.
In case you want to know more about implement K2: A quick step-by-step guide on installing and setting up K2.
Subscribe to:
Comments (Atom)