I was a bit surprised that getting Paging & sorting to work with ASP.NET’s GridView is not automatic.. but this tutorial is perfect, I just copied the code and it worked first time.
Great!
http://ryanolshan.com/technology/gridview-without-datasourcecontrol-datasource/
Found this helpful article I thought I’d post..
http://webpaws.com/blog/2008/08/19/postback-in-aspnet-passing-variables-on-postback/
Basically I had a class variable in an ASP.NET and found it was getting lost on page post-back.. well all you have to do is put that object in the Viewstate object already created for you.
eg:
public partial class MyPage : System.Web.UI.Page
{
protected int MyInt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyInt = 12345;
ViewState["MyKey"] = MyInt;
}
}
/*
This is called on a page post-back...
*/
protected void Event(object sender, EventArgs e)
{
//Get factor list from viewstate
MyInt = (int)ViewState["MyKey"];
//Go nuts
}
}
As soon as I started using Web Services I came against this problem, and loads of other people have asked it before but I didn’t really find a satisfactory answer. The easiest method is to create your own custom class (or a struct, as I did) and use that:
The struct
public struct KeyValueStruct
{
private int _key;
private string _val;
public int Key
{
get { return _key; }
set { _key = value; }
}
public string Value
{
get { return _val; }
set { _val = value; }
}
public KeyValueStruct(int key, string value)
{
_key = key;
_val = value;
}
}
The web method
[WebMethod]
[XmlInclude(typeof(KeyValueStruct))]
public List<KeyValueStruct> GetEnergySources()
{
List<KeyValueStruct> sources = new List<KeyValueStruct>();
sources.Add(new KeyValueStruct(1, "Solar"));
sources.Add(new KeyValueStruct(2, "Wind"));
return sources;
}
Simple!