Archive for May, 2012

Putting a line break in an resx resource file

I had a Label control that I wanted to populate from a resource file, like so:

 <asp:Label ID="lblNote" runat="server" Text="<%$ Resources:AppInfo, Note%>" />

I needed to put a line break in the middle of the text.  To do that, I wanted to put a “<br>” in the text, but since the resource file complained when I tried to put greater than and less than symbols, I had to be sly about it. So in the AppInfo.resx file, I had this entry to add a line break:

   <data name="Note" xml:space="preserve">
    <value>Note: I want this to be the first line and &lt;br&gt;this to be the second.</value>
  </data>

Leave a comment

SingleOrDefault Sequence contains more than one matching element

I was receiving an exception “SingleOrDefault Sequence contains more than one matching element” when trying to return a single object from a List. Here is a little demonstration of what was happening. First, my very useful, real-life class:

    public class Testing
    {
        public string Name{ get; set; }
        public int ID { get; set; }

        public Testing(string name, int id)
        {
            Name = name;
            ID = id;
        }
    }

So in the Page_Load event, I populate my useful, real-life class with some very important data. This is very secure data, so don’t share it with anyone:

        protected void Page_Load(object sender, EventArgs e)
        {
            List<Testing> testing = new List<Testing>(){};
            testing.Add(new Testing("san francisco",1));
            testing.Add(new Testing("toronto",2));
            testing.Add(new Testing("chicago", 3));

            var item = testing.SingleOrDefault(i => i.ID == 1);
        }

If all my IDs are unique, no problem, this will run successfully. If, however, I have duplicate IDs, then problem will arise. So if I change my code to that below, adding a duplicate ID:

        protected void Page_Load(object sender, EventArgs e)
        {
            List<Testing> testing = new List<Testing>(){};
            testing.Add(new Testing("san francisco",1));
            testing.Add(new Testing("toronto",2));
            testing.Add(new Testing("chicago", 3));
            testing.Add(new Testing("new york", 1));

            var item = testing.SingleOrDefault (i => i.ID == 1);
        }

Bong, I get the error “SingleOrDefault Sequence contains more than one matching element”.
The way I got around this was to replace SingleOrDefault with FirstOrDefault, and my code ran correctly:

        protected void Page_Load(object sender, EventArgs e)
        {
            List<Testing> testing = new List<Testing>(){};
            testing.Add(new Testing("san francisco",1));
            testing.Add(new Testing("toronto",2));
            testing.Add(new Testing("chicago", 3));
            testing.Add(new Testing("new york", 1));

            var item = testing.FirstOrDefault (i => i.ID == 1);    
        }

Leave a comment

Ain’t too proud to hack – why won’t my Enter key click my button?

That’s right. With apologies to the “The Temptations”, I ain’t too proud to hack.

I was working on a script to submit a form by programmatically clicking a button using JavaScript when the user pressed the Enter key in a text box. Simple. Apparently, not simple enough for me. Here is the code I had:

<script type=”text/javascript”>
function test() {
if (typeof e == ‘undefined’ && window.event) { e = window.event; }
if (e.keyCode == 13) {
document.getElementById(‘btnTest’).click();
}
}
</script>

<asp:TextBox ID=”txtTest” onkeypress=”test(event)” runat=”server” />
<asp:Button ID=”btnTest” Text=”Click” runat=”server” onclick=”btnTest_Click” />

So whenever a key was pressed while the focus was in the txtTest control, a PostBack event would occur. The problem was that whenever I pressed the Enter key, a PostBack would fire (i.e. the Page_Load event would be triggered), but the btnTest_Click event wasn’t firing. The odd part (from my perspective) was that if I disabled the “e.keycode == 13” line and entered a letter key (‘a’, ‘b’, etc…. Wait, there is no “etc” key), the btnTest_Click event would fire.

So, as is my way, I cheated. I made one little change, which was changing the keycode from the enter key to an empty string, and that caused click event to fire properly:

function test() {
if (typeof e == ‘undefined’ && window.event) { e = window.event; }
if (e.keyCode == 13) {
e.keyCode = “”;
document.getElementById(‘btnTest’).click();
}
}

Leave a comment