Monday, March 12, 2012

Delete Contents of IsolatedStorageFile

0 comments
The easiest way to delete contents of an IsolatedStorageFile is to open it in the Truncate mode:

IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForAssembly();

IsolatedStorageFileStream isoFile = new IsolatedStorageFileStream(myFileName, FileMode.Truncate, isoStore);

According to documentation, if a file is opened in this mode its size should be 0 bytes.

Friday, December 2, 2011

Delete ScriptManager History

0 comments
Well, I have not found a way to clear ScriptManager history proper, but the workaround  is to use the AddHistoryPoint method and set the value of your property to "0":

ScriptManager1.AddHistoryPoint("myKey", "0");


Tuesday, October 25, 2011

Check if GridView Column Exists

1 comments
If you have a DataRowView you can use the following extension method to check if a GridView column exists:

public static bool ColumnExists( this  DataRowView rowData,  string  fldToCheck)
{

return rowData.Row.Table.Columns.Contains(fldToCheck);
}

Normally, you would use it on RowDataBound event, e.g:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

DataRowView rowData = e.Row.DataItem as DataRowView;
string  fldToCheck = "MyFieldName";
if (e.Row.RowType == DataControlRowType.DataRow)
{
      if (rowData.ColumnExists(fldToCheck) ...

Monday, October 17, 2011

Modify Query String

0 comments

If you build a hyperlink on a web page dynamically, sometimes you may need to append or modify an existing query string.
To modify a url with an existing query string, first grab the AbsoluteUrl property:

string path = Request.Url.AbsolutePath ;

This will give you a url wihout the query string portion.

If you need to reuse an existing portion of the query string, you have to get it yourself, e.g:

string key = Request.QueryString["key"];
string path = Request.Url.AbsolutePath + "?key="+ key;


Now, you can build your url:

string url  = path+ "&key2="+ myValue;




Thursday, October 13, 2011

Find GridView Column Index

0 comments
If you create GridView columns dynamically using the AutogenerateColumns = true feature, sometimes you need to find a column index from the column name.

I created an extension method to find a column index:

public static int GetIndex(this DataRowView tableData, string fieldName)
        {
            DataColumn dc = tableData.DataView.Table.Columns[fieldName];
            
            if (dc != null)
            {
                return dc.Ordinal;
            }
            return -1;
        }

This is how you call from the RowDataBound event handler:

DataRowView tableData = e.Row.DataItem as DataRowView;
int pos = tableData.GetIndex("MyFieldName");                
if (pos != -1){
   //do your thing
}

Wednesday, July 27, 2011

WPF GridView Cell Borders

0 comments
If you populate a GridView programmatically, and you want to set its style, it's not obvious.

The best approach I found is to set its style in XAML first, and then assign it in code. For example, this is how you would put borders under each row:

<ListView.Resources>
<Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#BABABE" />
</Style>
</ListView.Resources>

Then, after you set the DataContext property in code, use:

myListView.ItemContainerStyle = (Style)myListView.Resources["itemstyle"];

Thursday, March 17, 2011

Check QueryString for Null

0 comments
If you need to determine whether the Request.QueryString is null and take some action depending on the result, it is not enough to check for null. QueryString is an HttpValueCollection type, so even if the Request.Url does not have a query string appended to it, the value of the collection is not going to be null. The proper way to check if the query string contains anything is to check for null and the Count property as well:

if(Request.QueryString!= null && Request.QueryString.Count>0){
     //some piece of code.
}