At current, I have a set of dynamically generated image buttons, which are created with this (excert) of code:
This method works absolutely fine on the initial page load:
However, if I actually click one of the buttons, the click method:
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,
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(); } }
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(); }
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,
Comment