views:

94

answers:

1

I am getting an EVENT undefined rror in the ASP.NET C# code below. I am dynamically creating image buttons and am explicitly assigning them to a CLICK event. After the user clicks on a thumbnail, the user is directed to a page with a blown up image of that thumbnail. When the user presses the back button to go to the original page this is where things get interesting. If the user attempts to click on a thumbnail again, the browser fails to reconigze the image button click event with an error of "EVENT is undefined. This error is random, it always occur after a minimum of one "postback", but the sequence of when it occurs is not consistent. Interesting enough, this is only in IE. Google Chrome and Firefox work fine and recognize the EVENT. I've played around the the ViewState property, but that hasn't worked. Any ideas?

protected void ImageButton_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton ib = (ImageButton)sender;
            Server.Transfer(@"FullImage.aspx?file=" +
                            HttpUtility.UrlEncode("~/Gallery/Pictures/RegSize/pic" +
                                                  ib.CommandArgument + ".jpg"));
        }

    protected void Page_Load(object sender, EventArgs e)
    {

        //Array containing file locations of thumbnail pictures

        string[] files = null;

        files = Directory.GetFiles(Server.MapPath("~/Gallery/Pictures/RegSize"), "*.jpg");



        for (int i = 0; i < files.Length; i++)
        {
            System.Web.UI.WebControls.Image imgWeb = new System.Web.UI.WebControls.Image();

            //Create bitmap to retrieve Image's size information
            Bitmap bmp = new Bitmap(Server.MapPath("~/Gallery/Pictures/RegSize/Pic"
                        + i.ToString() + ".jpg"));

            //Create dynamic ImageButton to hold the Image
            System.Web.UI.WebControls.ImageButton imgBtn =
                new System.Web.UI.WebControls.ImageButton();
            imgBtn.Click += new ImageClickEventHandler(ImageButton_Click);
            imgBtn.Attributes.Add("OnClick", "ImageButton_Click");
            //imgBtn.EnableViewState = false;
            imgBtn.ImageUrl = "~/Gallery/Pictures/RegSize/pic" + i.ToString() + ".jpg";
            imgBtn.CommandArgument = i.ToString();
            //Set Imagebutton Width/Height according to the Bitmaps Width/Height
            imgBtn.Width = Resize(bmp.Size, 200, 200, "WIDTH");
            imgBtn.Height = Resize(bmp.Size, 200, 200, "HEIGHT");

            imgBtn.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
            imgBtn.Style.Add(HtmlTextWriterStyle.Margin, "5px");

            //Dispose Bitmap, no need for it
            bmp.Dispose();

            //imgBtn.ImageUrl = @"thumbnail.aspx?file=\gallery\pictures\regsize\pic" + i.ToString() + ".jpg";
            //imgBtn.PostBackUrl = @"FullImage.aspx?file=" + HttpUtility.UrlEncode("~/Gallery/Pictures/RegSize/pic" + i.ToString() + ".jpg");

            divDisplay.Controls.Add(imgBtn);
        }
+2  A: 

There could be several things going on here, but the first one I see is that you shouldn't create new controls and add them to your page in Page_Load(), since by that time in a postback, ViewState will have already been used to re-establish the control's state.

Instead, you should do that work in the Page_Init() event handler.

In case that doesn't fix it: do you have output caching or client-side caching configured for this page? When you do a back in the browser, is the page re-requested from the server? Have you watched the HTTP conversation with Fiddler?

Also, a minor point: it's better to use "using" for your Bitmap than to explicitly called Dispose().

RickNZ
I've never used or heard of fiddler, so I downloaded and checked my cached settings. Thank You, its a great tool! Anyways, The web form was being privately cached, so I added the following code into my Page_Load() event;Response.Cache.SetCacheability(HttpCacheability.NoCache);The problem is fixed, but why would caching the web form cause a "Event is unidentified". I believe my pictures and css files are still being cached. I say this because in fiddler under the "cache" column for web sessions the field is blank for pictures and css files, as opposed to "no-cache" value.
contactmatt
It sounds like it's related to state management. I suspect that the issue may be related to ViewState and control initialization happening before the control exists. If the event setup happens during Page_Init, your control isn't there, and the event handler isn't assigned, which produces the event undefined error. Re: Fiddler; yes, it's a great tool (the author posts once in a while here on SO). You can use "raw" mode to see the exact HTTP headers.
RickNZ