Home 
 
 
 
 
 Blog 
 
 
 
Sytling Image

Posts Tagged ‘asp.net’

Stacking PostBack Events

February 11th, 2011 - Be the First Comment!

The other day at work I was working through a problem with a coworker. The short of it is that he had something going on that I didn’t even know could happen. I made a small project to experiment with this and it attached for download if you want to try it out. It is in ASP.NET and C#. He was stacking postbacks (accidently, causing strange behavior). He had a checkbox with an OnCheckChanged event specified, but it also had AutoPostBack=”false” on it.

Check the checkbox first, then click the Submit button.

    /* Front End Code
     * One check box has a normal postback the other creates
     * a stacked PostBack.
     */
    <div>
        <asp:CheckBox ID="cb1" Text="CheckBox 1 (AutoPostBack=false)" OnCheckedChanged="cb1_CheckChanged" AutoPostBack="false" runat="server" />&nbsp;
        <asp:CheckBox ID="cb2" Text="CheckBox 2" OnCheckedChanged="cb2_CheckChanged" AutoPostBack="true" runat="server" />&nbsp;
        <br />
        <br />
        <asp:Label ID="lblTarget" runat="server" />
        <br />
        <br />
        <asp:Button ID="Submit" Text="Submit" runat="server" OnClick="Submit_Click" />&nbsp;
        <asp:Button ID="Reset" Text="Reset" OnClick="Reset_Click" runat="server" />
    </div>

    /* Back End Code
     * The back end code just adds the name of the ID of the button lblTarget
     */
        protected void cb1_CheckChanged(object sender, EventArgs e)
        {
            if (lblTarget.Text.Equals(lblTargetText))
            {
                lblTarget.Text = " cb1 ";
            }
            else
            {
                lblTarget.Text += " cb1 ";
            }
        }

Effectively he was doing something that requires a PostBack then saying not to PostBack. This leads to a stacking effect (which was unknown to us until recently). On the next PostBack  the method for the OnCheckedChanged event was fired then the actual item that caused the PostBack’s method is executed. I never realized you could queue up PostBack events. Hopefully this will help someone else out.

PostBack Event Experiments

ASP.NET Masterpage, JavaScript, and ResolveUrl

October 6th, 2010 - Be the First Comment!

Today was an interesting day for many reasons. The one I’m going to share with you has to do with javascript and the ResolveUrl method in ASP.NET Masterpage. I recently added jQuery to our web project at work. We have a strange setup and using /Script/jQuery.js in the script tags src attribute won’t work. For that reason I decided to use ResolveUrl() which gets the URL of a file you pass it (i.e. ResolveUrl(“~/Script/jQuery.js”) ). This worked great until I was going through the application working on something else and randomly received this error message:

System.Web.HttpException:The Controls collection cannot be modifiedbecause the control contains code blocks (i.e. <% … %>).

It only happened on a couple of pages and the others would load fine which added to the confusion. Honestly, this error made little to no sense when I first saw it, so I did what any resourceful individual would. I googled it. Luckily the first result was the answer to my issue! The fix is to use data binding (<%#)  instead of writing it out (<%=). It is strange, but it resolves the problem. This is the wonderful explanation and solution: http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/.

Returning to work.

August 24th, 2009 - 1 Comment

First day of class and back to work. Oh the excitement! Really, not sarcastically. I had a few things to fix / rework on the MSU Google Map I made before the summer started so I popped open Visual Studio and began editing. Unfortunately every time I tried to type if to create some logic to test for a null image, visual studio crashed. After four attempts of doing the same thing I decided it was time to bust out dreamweaver. It might have been the large amounts of javascript involved and tele-sense killing it, but it was rather annoying. Good thing dreamweaver doesn’t have that problem. After that I was checking out a problem involving an issue that wouldn’t allow IE8 users (with compatibility mode off) to click in the map to access information about a building. After looking around to see what might be causing it I couldn’t find anything in the code. It was also odd because it worked fine in compatibility mode and in firefox. I searched in the google group for the issue and low and behold its IE8. IE8 incorrectly calculates the x y click offset. How wonderful. The best part is Microsoft has known about the issue since IE8 RC1 and it still hasn’t been fixed! Their solution? Use a meta tag to force IE7 interpretation in IE8. Wow. Two losses for MS today.

Styling Image