dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Click Event for ImageButtons created in code and persisted from Session.

  1. #1
    Join Date
    Dec 2008
    Posts
    11

    Click Event for ImageButtons created in code and persisted from Session.

    I am building a web site for a friend who is a graphic designer.

    He wants to display one image at a time on his home page and have this image selected by clicking on one of a related group of thumbnails.

    I have an <asp:Panel id="ThumbnailList"... > hard coded in the page markup and I'm creating ImageButton instances within my C# code behind from the rows in a DataTable.

    Getting the group of ImageButton's to appear on the page is working, but when I click on them I can't get any life out of my ThumbnailClick(object sender, ImageClickEventArgs e) event handler.


    The code goes a little something like this...



    private const string THUMBNAIL_KEYS = "THUMBNAIL_KEYS";
    private List<string> sessionThumbKeys;

    PopulateThumbnailList()
    {
    var queryInfoArgs = "...";
    DataTable dt = FetchDataTable(queryInfoArgs)

    foreach(DataRow r in dt.Rows)
    {
    string fileID = r["fileID"];
    string imageName = r["name"];
    string imageURL = r["url"];

    ImageButton imageButton = CreateNewThumbnail(fileID,imageName,imageURL);

    this.ThumbnailList.Controls.Add(imageButton);

    string sessionKey = "ImageButton_" + fileID;

    Session.Add(sessionKey, imageButton);

    sessionThumbKeys.Add(sessionKey);
    }
    Session.Add(THUMBNAIL_KEYS, sessionThumbKeys);
    }


    protected ImageButton CreateNewThumbnail(string fileID, string name, string uRL)
    {
    ImageButton imageButton = new ImageButton();


    imageButton.ID = "ImageButton_" + fileID;
    imageButton.ToolTip = name;
    imageButton.ImageUrl = uRL;

    // Information about the file selected to be sent back when clicked
    imageButton.Attributes.Add("file_id",fileID);

    // Events
    imageButton.Click += new ImageClickEventHandler(ThumbnailClick);


    // Click wasn't working so I added the following
    // Borrowing the javascript doPostBack function used by HiddenFields
    imageButton.OnClientClick = String.Format("javascript:__doPostBack('ct100$ContentPlaceHolderPageBody${0}','{1}')",imageButton.ID ,fileID);

    }

    protected void ThumbnailClick(object sender, ImageClickEventArgs e)
    {
    .... retrieve fileID, query dataBase for url and set up the full version of the image on the page
    }

    Because the ImageButtons are not in the markup and don't get added when the Page constructor runs I have to bring them back out of session. Since it's all a bit new to me, I'm not 100% sure I'm doing this at the right point in the page lifecycle.



    // OnPreLoad
    protected override void OnPreLoad(EventArgs e)
    {
    if (Session[THUMBNAIL_KEYS] != null)
    {
    foreach (string s in (List<string>)Session[THUMBNAIL_KEYS])
    {
    if (Session[s] != null)
    {
    ImageButton i = (ImageButton)Session[s];
    ThumbnailList.Controls.Add(i);
    }
    }
    }
    base.OnInit(e);
    }

    This loads them back into the page nicely but the event doesn't fire.

    I initially only set the Click property on the ImageButtons, then I thought about the fact that the ImageButtons get placed into session before they are rendered, so when pulling them out of Session again it's as if they had never been clicked.

    So I then added the OnClientClick string, I can get the Request.Form["__EVENTTARGET"] and Request.Form["__EVENTARGUMENT"] information back nicely at page load and display it on a couple of labels.

    I've started to look at Page methods such as RaisePostBackEvent() and RegisterRequiresRaiseEvent()

    e.g. during the OnPreLoad foreach...


    foreach (string s in Session[THUMBNAIL_KEYS...

    ImageButton i = Session[s] as ImageButton;

    if(i.UniqueID == Request.Form["__EVENTTARGET"])
    {
    this.RegisterRequiresRaiseEvent(i);
    }

    but no joy so far.

    I'd really appreciate some pointers!

    Can anyone help? If you can I'll run straight out on Tuesday morning and buy you a cut price Easter Egg!

    Thanks

  2. #2
    Join Date
    Dec 2007
    Location
    Dayton, OH
    Posts
    390
    This is a .NET question. Please ask there. You can do this with ease if you use JavaScript though. Good luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles