开发者

ASP.NET C# Error: Object Event is undefined

开发者 https://www.devze.com 2022-12-13 12:17 出处:网络
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 us

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
            imgB开发者_StackOverflowtn.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);
        }


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消