October 2005 - Posts - Raj Kaimal

October 2005 - Posts

BoundField DataFormatString attribute not being applied.

Version: ASP.net 2.0 RTM

I wasted a few minutes figuring out this one.

You have a BoundField object in a GridView bound to a field of type DateTime with a DataFormatString attribute but the format string is not being applied.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" />

Instead, the field appears to be formatted using its ToString() method like so:
Output: 10/31/2005 7:00:54 PM
 

Cause

To prevent cross site scripting attacks, the field value is HtmlEncoded. The HtmlEncoding occurs before applying the DataFormatString making the format string have no effect.

 

Resolution

In this case (ie. when using a field of type DateTime), set HtmlEncode to false.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>

The Output renders as: 10/31/2005

This setting also applies to the HeaderText.
The following:
HeaderText="Employee<BR/>Name"
will not render a line break unless you set HtmlEncode to false.


I am not sure why the ASP.net team decided against HtmlEncoding the string after applying the DataFormatString.

Update for 3.5 SP1, 3.0 SP1, 2.0 SP1
SP1 introduces a new property called HtmlEncodeFormatString which allows you to specify whether the formatted text should be HTML encoded when it is displayed.

Posted by rajbk | 103 comment(s)
Filed under:

The problem with Biometric fingerprint readers

Biometric fingerprint readers allow you to logon to your computer in the active directory and to your favorite websites with the touch of your finger. Unfortunately, most users rely so much on fingerprint readers that they forget their domain password. This becomes frustrating when a user tries to access resources in the AD from a machine that does not have a fingerprint reader or is not in the AD.

 

Users start by entering the wrong passwords and eventually get locked out of their account thanks to Group Policy settings. You end up with your CIO attending a conference in Vegas asking you to reset his password and giving it over the phone. You start to wonder if the person calling is really the person they claim they are or some hacker trying social engineering techniques and start to think about how you could verify their identity, if the phone line is secure, whether you will loose your job if you don’t give him the temp password etc. It goes downhill from there…

 

Maybe fingerprint readers of the future will allow you to remotely/securely send credentials from any machine. Maybe they already exist.

Posted by rajbk | with no comments
Filed under:

Refactor Rename incredibly slow on medium to large projects

Please vote on this if you think it is important to you.

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=f17e1c19-54be-4bc6-bc79-1cefdd324c80

Hopefully the team working on this will make it a priority if they see a lot of votes. Thanks.

I am not expecting MS to fix this in the current release but I by giving feedback, I hope that they will make it a priority for the next release.

Posted by rajbk | 2 comment(s)
Filed under:

Changing values via Ajax/Atlas and maintaining state

The advantage of Ajax, Atlas or CallBacks is that we can refresh regions in a page without having to reload the entire page.

One of the problems I have run into is preserving the state of the refreshed region when my page is semi-ajax (ie. it may use either ajax or the browser’s submit mechanism to postback data).

Consider this simple example:
You change the value of a Label control like so:
document.getElementById("Label1").innerHTML = "Hello World!";

When you postback the page, your application has no way of knowing the change that occurred. This causes the Label control to be rendered the way it was before you made the change using ajax.

To work around this, I use a hidden input tag that “shadows” the value(s) being populated. The hidden input tag’s value is changed through script or code behind if the control it is shadowing changes value.

This way, when a postback occurs, the application is aware of the change by reading the hidden input field.

I am sure there is a better way to do this.

Posted by rajbk | 2 comment(s)
Filed under:

Enumerating XmlDataSources as Tabular/Hierarchical data

Information based on VS.NET 2005 RC1

You have an XmlDataSource on your page and you wish to enumerate the items as tabular data

 

private void EnumerateDataSource() {

    IDataSource ds = XmlDataSource1 as IDataSource;

    if (ds != null) {

        XmlDataSourceView xmlDsView = ds.GetView(String.Empty) as XmlDataSourceView;

        if (xmlDsView != null) {

            IEnumerator enumerator = xmlDsView.Select(DataSourceSelectArguments.Empty).GetEnumerator();

            while (enumerator.MoveNext()) {

                //Retrieve "Name" attribute from node

                //<Employee ID="4" Name="John" />

                string employeeName = Convert.ToString(DataBinder.Eval(enumerator.Current, "Name"));

                TextArea1.Value += employeeName + "\r\n"; //Append to a TextArea control

            }

        }

    }

}

 

You have an XmlDataSource on your page and you wish to enumerate the items as hierarchical data

 

private void EnumerateHierarchicalDataSource() {

    IHierarchicalDataSource hs = XmlDataSource1 as IHierarchicalDataSource;

    if (hs != null) {

        HierarchicalDataSourceView hsDataSourceView = hs.GetHierarchicalView(String.Empty);

        if (hsDataSourceView != null) {

            IHierarchicalEnumerable iHierarchEnumerable = hsDataSourceView.Select();

            ReadRecursive(iHierarchEnumerable, 0);

        }

    }

}

 

//Recursively read the hierarchical data node

//Quits recursion on current node if HasChildren is not true

private void ReadRecursive(IHierarchicalEnumerable enumerable, int depth) {

    IEnumerator enumerator = enumerable.GetEnumerator();

    while (enumerator.MoveNext()) {

        IHierarchyData hierarchyData = enumerable.GetHierarchyData(enumerator.Current);

        XmlElement element = hierarchyData.Item as XmlElement;

        if (element != null) {

            Textarea2.Value += String.Format("{0} : {1}{2}",

                depth, element.Attributes["Name"].Value,

                Environment.NewLine); //Append to a TextArea control

          

            if (hierarchyData.HasChildren) {

                IHierarchicalEnumerable iHierarchEnumerable = hierarchyData.GetChildren();

                if (iHierarchEnumerable != null) {

                    ReadRecursive(iHierarchEnumerable, depth + 1);

                }

            }

        }

    }

}

Posted by rajbk | with no comments
Filed under:

XmlDataSources, DataItem and the sealed XmlDataSourceNodeDescriptor

One way of getting to the DataItem in ItemDataBound when binding to an XmlDataSource is by using the DataBinder.Eval method like so:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) {

    Trace.Write(e.Item.DataItem.GetType().ToString());

    string employeeName = DataBinder.Eval(e.Item.DataItem, "Name") as string; //Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Name"))

 

}

e.Item.DataItem is of Type System.Web.UI.WebControls.XmlDataSourceNodeDescriptor which is sealed :-(

Comments from Matt Hawley

Actually, XmlDataSourceNodeDescriptor implements IXPathNavigable, which allows you to create an XPathNavigator. See the following...

Label lbl = (Label) e.Item.FindControl("lbl");
XPathNavigator nav = ((IXPathNavigable) e.Item.DataItem).CreateNavigator();

XPathNodeIterator it = nav.Select("./name");
it.MoveNext();
lbl.Text = it.Current.Value;


Comments from Oleg

IMHO, The right way to get DataItem value is to use the XPathBinder class. The Matt's sample will be looks like:

Label lbl = (Label) e.Item.FindControl("lbl");

IEnumerator er = XPathBinder.Select(e.Item.DataItem, "./name").GetEnumerator();

er.MoveNext();
lbl.Text = ((XmlElement)er.Current).Value;

or it can be simple bound with Eval method:
lbl.Text = XPathBinder.Eval(e.Item.DataItem, "./name", "");

 

Posted by rajbk | 1 comment(s)
Filed under:

Gates, Glazer shake hands

What are they really thinking?
http://news.com.com/2300-1014_3-5893371-1.html
Posted by rajbk | with no comments
Filed under:

30 seconds of music preview...

When purchasing music online, thirty seconds of preview from the start of the song is not enough, especially if you have never heard the artist's work before.

Songs generally change pace after the beginning.

Maybe they should provide a thirty second sample from the start and another thirty seconds from the middle.  They could also have the whole song available for preview at a much lower sampling rate (something like 12Khz/16bit).

Posted by rajbk | with no comments
Filed under:

ObjectDataSource and Method parameter change

VS.NET 2005 RC1

If you have defined a couple of ObjectDataSources that point to a DataObjectMethod and at a later stage decide to change a parameter in the DataObjectMethod, you page will compile without any problems but you will see errors at runtime.

Clicking on the DataObjectMethod and selecting "find all references" does not return anything either.

I guess the solution for now is when you change a DataObjectMethod, search your solution for any occurrences of that method and fix accordingly.

If anyone has a better idea, please let me know.

Setting parameters for DataSourceControls from code-beside (ASP.NET 2.0)

Posted by rajbk | with no comments
Filed under:
More Posts