C# ASP.net ImageButton click event problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dperren
    New Member
    • Mar 2007
    • 10

    C# ASP.net ImageButton click event problems

    At current, I have a set of dynamically generated image buttons, which are created with this (excert) of code:

    Code:
    TableCell tasks = new TableCell();
    
    while (reader.Read())
                    {
                        ImageButton button = new ImageButton();
    		    button.Click += new System.Web.UI.ImageClickEventHandler(taskButton_Click);
                        button.ImageUrl = "Images/task.gif";
                        button.BorderWidth = 2;
    
                        //Assign the id to the button so we can access it in the onClick method.
                        //Best way?
                        int id = reader.GetInt32(taskID);
                        button.CommandName = id.ToString();
    
    					bool cancelled = reader.GetBoolean(taskCancelled);
    					bool complete = reader.GetBoolean(taskComplete);
    					string due = reader.GetString(dueTime);
    
    					string assignee = "";
    					if(reader.IsDBNull(taskAssignee))
    					{
    						assignee = null;
    					}
    					else
    					{
    						assignee = reader.GetString(taskAssignee);
    					}
    
    					//Get CSS Class
    					button.CssClass = this.getTaskCSS(cancelled, complete, assignee, due);
    
    					tasks.Controls.Add(button);
                    }

    This method works absolutely fine on the initial page load:
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
    		string[] userSplit = HttpContext.Current.User.Identity.Name.Split(new Char[] { '\\' });
    		this.user = userSplit[1];
    
    		if (!Page.IsPostBack)
    		{
    			//Build list of tasks per half hour, and list of user-assigned tasks.
    			this.buildTaskList();
    			this.buildUserTaskList();
    			this.assignDefaults();
    		}
        }
    However, if I actually click one of the buttons, the click method:
    Code:
    protected void taskButton_Click(object sender, ImageClickEventArgs e)
    	{
    		ImageButton clicked = (ImageButton)sender;
    		string taskID = clicked.CommandName;
    
    		int id = Int32.Parse(taskID);
    		this.currentTask = id;
    
    		//taskList.Rows.Clear();
    		//userTaskTable.Rows.Clear();
    
    		this.buildTask(id);
    		this.buildTaskList();
    		this.buildUserTaskList();
    	}
    Is never accessed: if I step through the code, it hits the IsPostBack switch, skips the contents (as expected), then finishes without ever touching the click method.

    However, if I comment out the IsPostBack switch, then the buttons function as expected, accessing the click method fine. I can't for the life of me see what's wrong here - can anyone shed any light?

    Thanks,
  • dperren
    New Member
    • Mar 2007
    • 10

    #2
    Please excuse the double post, but I created a very simplified version of the application which has exactly the same effect:

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    		if (!Page.IsPostBack)
    		{
    			drawImages();
    		}
        }
    
    	protected void drawImages()
    	{
    		int i = 0;
    
    		TableRow row = new TableRow();
    		TableCell cell = new TableCell();
    
    		while (i < 5)
    		{
    			ImageButton imbutton = new ImageButton();
    		    imbutton.Click += new ImageClickEventHandler(googlePictureClick_Click);
                imbutton.ImageUrl = "http://content.answers.com/main/content/wp/en/thumb/6/69/48px-Google_Earth.png";
                imbutton.BorderWidth = 2;
    
    			cell.Controls.Add(imbutton);
    			i++;
    		}
    
    		row.Cells.Add(cell);
    		Table1.Rows.Add(row);
    	}
    
    	protected void googlePictureClick_Click(object sender, ImageClickEventArgs e)
    	{
    		drawImages();
    	}
    }
    Code:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    		<asp:Table ID="Table1" runat="server">
    		</asp:Table>
    		&nbsp;</div>
        </form>
    </body>
    </html>
    It's most probably something really stupid, I'm only on my third day of this...

    Thanks again,

    Comment

    • dperren
      New Member
      • Mar 2007
      • 10

      #3
      Managed to sort it - the IsPostBack indeed shouldn't have been there; instead I cleared the tables of their rows and updated them with the new ones.

      Comment

      Working...