Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, May 13, 2009

How to Close Web Browsers

btn.Attributes.Add("OnClick", "self.close()");
will do the trick.
Another alternative is
Response.Write (" < script > self.close() ; < /script >
");

Monday, March 16, 2009

How to Sort or handle Paging in GridView Manually without databinding

The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
The GridView 'GridViewID' fired event Sorting which wasn't handled.

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewSortExpression = e.SortExpression;
int pageIndex = this.GridView1.PageIndex;
this.GridView1.DataSource = SortDataTable(GridView1.DataSource as DataTable, false);
this.GridView1.DataBind();
this.GridView1.PageIndex = pageIndex;

}

private string GridViewSortDirection
{
get
{
return ViewState["SortDirection"] as string ?? "ASC";
}

set
{
ViewState["SortDirection"] = value;
}
}



private string GridViewSortExpression
{
get
{
return ViewState["SortExpression"] as string ?? string.Empty;
}
set
{
ViewState["SortExpression"] = value;
}
}



private string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;
case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}



protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.DataSource = SortDataTable(this.GridView1.DataSource as DataTable, true);
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}

protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
}
}
return dataView;
}

else
{
return new DataView();
}
}

How to have Display Number in GridView

< asp:TemplateField >
< ItemTemplate >
< %# Container.DataItemIndex + 1 % >
< /ItemTemplate >
< /asp:TemplateField >

Tuesday, March 10, 2009

Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started..

The error that you are getting is because your StateServer is not enabled. You have to go to AdministrativeTools > Computer Management > Services and Applications >Services and start the ASP.NET State Service

Wednesday, November 26, 2008

The type or namespace name 'Script' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

To resolve this problem, please ensure that you have the system.web.extension dll added as a reference in your project.

Alternatively, copying the system.web.extension.dll to the bin directory for a deployment project should resolve the problem.

Wednesday, November 05, 2008

How to Show File Dialog

Here is some code to allow users to select Folder.

public class FolderBrowser : FolderNameEditor
{
private FolderNameEditor.FolderBrowser m_obBrowser = null;
private string m_strDescription;
public FolderBrowser()
{
m_strDescription = "Select folder";
m_obBrowser = new FolderNameEditor.FolderBrowser();
}

public string DirectoryPath
{
get{return this.m_obBrowser.DirectoryPath;}
}

public DialogResult ShowDialog()
{
m_obBrowser.Description = m_strDescription;
return m_obBrowser.ShowDialog();
}
}

Monday, June 30, 2008

How to sort and allow paging manually using Gridview

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}