Search This Blog

Friday, October 31, 2008

Put your DataTable value to ViewState such that you can rearrange the ViewState

ViewState["userDataSource"] = dtTemp;


public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}


private void SortGridView(string sortExpression, string direction)
{
DataTable dt = ((DataTable)ViewState["userDataSource"]);
DataView dv = new DataView(dt);
dv.Sort = sortExpression + " " + direction;

this.gvUser.DataSource = dv;
gvUser.DataBind();
}

protected void gvUser_Sorting(object sender, GridViewSortEventArgs e)
{

string sortExpression = e.SortExpression;
GridView dd = ((GridView)(object)sender);

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, "DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, "ASC");
}

}

Pagination in DataList

Generally DataList Control does not support pagination by its default properties..But if you need it you have to do it by mannual coding.

Add
PagedDataSource pds = new PagedDataSource();
at the top of your Page_Load(object sender, EventArgs e) event

Now Add these methods in your code behind page


public void BindDatalist() //Get Data and Set Page Size ,Page Index
{

//dlstFeatureAlbum.DataSource = FeatureAlbum.GetAlbums();
// dlstFeatureAlbum.DataBind();

DataTable dt = new DataTable();

dt = FeatureAlbum.GetAlbums();
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 24;//Convert.ToInt16(ddlPageSize.SelectedValue);
if (dt.Rows.Count <= 24)
{
divPageIndex.Visible = false;
}
if (dt.Rows.Count == 0)
{
lblEmptyList.Visible = true;
}
pds.CurrentPageIndex = CurrentPage;
lnkbtnNext.Enabled = !pds.IsLastPage;
lnkbtnPrevious.Enabled = !pds.IsFirstPage;

dlstFeatureAlbum.DataSource = pds;
dlstFeatureAlbum.DataBind();

doPaging();
}


//Code for Pagination

public int CurrentPage //this method is for storing the current page index
//to view state
{

get
{
if (this.ViewState["CurrentPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
}

set
{
this.ViewState["CurrentPage"] = value;
}

}

private void doPaging() //Calculating total results and divide them into pages
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}

dlPaging.DataSource = dt;
dlPaging.DataBind();
}

protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindDatalist();
}
}

protected void lnkbtnPrevious_Click(object sender, EventArgs e) //navigation
{
CurrentPage -= 1;
BindDatalist();
}

protected void lnkbtnNext_Click(object sender, EventArgs e) //navigation
{
CurrentPage += 1;
BindDatalist();
}

protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
}
}

protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentPage = 0;
BindDatalist();
}

//End

Write XML from code behind

public void CreateXMLFile(string FileName)
{
DataTable dt = new DataTable();
dt = FeatureAlbum.GetAlbums();
string path;
path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + FileName + ".xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.IO.FileInfo fileName = new System.IO.FileInfo(path);
try
{
if (!fileName.Exists)
{
StreamWriter sw = new StreamWriter(path, true);
sw.WriteLine("");
sw.WriteLine("
");
sw.Flush();
sw.Close();
}
doc.Load(path);
XmlElement newElem;
XmlElement newChElem;
XmlElement newSuperChElem;
XmlNode XNode;

for (int i = 0; i < dt.Rows.Count; i++)
{
newElem = doc.CreateElement("image");
doc.DocumentElement.AppendChild(newElem);
// newElem.SetAttribute("","");

newChElem = doc.CreateElement("filename");
newChElem.InnerText = dt.Rows[i]["Name"].ToString();
newElem.AppendChild(newChElem);

newChElem = doc.CreateElement("caption");
newChElem.InnerText = dt.Rows[i]["Description"].ToString();
newElem.AppendChild(newChElem);
}
//newChElem = doc.CreateElement("Time");
//newChElem.InnerText = "Time";
//newElem.AppendChild(newChElem);

XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.ASCII);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
}
catch (Exception ex)
{
//Your Catch block here...
}
}