Strange problem with buttons

Commodus2

Newcomer
Joined
Mar 5, 2003
Messages
15
Hi,

I have this problem when an event is triggered... better say.. not triggered.

I have a WebControl.Table. filled with rows with buttons in them, created dynamicaly. all is fine.
filled in page_load. If i press a button, a CommandEvent is triggered so that in this way i can check which button is pressed. it doesn't trigger it the first time.

I press the button. page reloads(postback) because the table is filled in page_load, the data is shown, but the event is not triggered. If i then press the button again, it is triggered.
page reloads and the story goes on. I have to press the button twice before the event is triggered.

I placed reponse.writes in the funtion to see which functions are called. guess what... the commandevent function is not called the first time. Looks like it is cut off the first time. I am confused why it calls it the second time...

When i trace the code. it just stops when it filled the table(in a postback) showing the data in the table on the screen.

when i put an if around the filling code saying that it only fills the table when page is not poastback, it just not filles it... and the event is not triggered and i pushed the button.

I hope someone can help me. It is very difficult to explain a problem here from scratch.

Greetz Commodus2
 
Ok.. it is a whole bunch a code... beware :)

the most important functions are Page_Load, ShowHolliday(i know missspelled) and requestManMutation.
C#:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HollidayReg
{

	/// <summary>
	/// Summary description for WebForm2.
	/// </summary>
	
	
	public class main_man : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputButton btnRequestHolliday;
		protected System.Web.UI.WebControls.Table tblHollidayView;
		protected System.Web.UI.WebControls.DropDownList ddlEmployee;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Button btnLogout;
		protected System.Web.UI.HtmlControls.HtmlInputButton btnPersonalOverview;
		// ------------------------------------------------------------------------------------------------------------------
		 
		
		private void Page_Load(object sender, System.EventArgs e)
		{
			Response.Write("Page_Load()<br>");
			if (!ValidateLogin()) 
			{
				Server.Transfer("login.aspx");
			} 
			else 
			{
				Response.Write(sender.GetType().ToString()+"<BR>"+e+"<BR>");
				// filling droplists with data from database
				if (!fillDdlEmployee()) 
				{
					throw new Exception("Error fetching employees from database");
				}
				if (Session["user"] != null)
				{
					int id = ((CUser)Session["user"]).Employeeid;
					//if (!Page.IsPostBack){
						if (ddlEmployee.SelectedItem != null && id != Convert.ToInt32(ddlEmployee.SelectedItem.Value))
							ShowHolliday(Convert.ToInt32(ddlEmployee.SelectedItem.Value));
						else
							ShowHolliday(id);
							
					//}
				}
				
			}
 
		}

		// ------------------------------------------------------------------------------------------------------------------
		private void ShowHolliday(int id)
		{
		Response.Write("ShowHoliday()<br>");
			try{
				CDataRet dataRet = new CDataRet();
				ArrayList al = new ArrayList();
				al = dataRet.getHollidays(id);
				
				tblHollidayView.Rows.Clear();

				TableRow trHeader = new TableRow();
				TableCell tdHeader = new TableCell();
				// table header
				tdHeader.Text="<b>Actions</b>";
				trHeader.Cells.Add(tdHeader);
				tdHeader = new TableCell();
				tdHeader.Text="<b>Type</b>";
				trHeader.Cells.Add(tdHeader);
				tdHeader = new TableCell();
				tdHeader.Text="<b>Owner</b>";
				trHeader.Cells.Add(tdHeader);
				tdHeader = new TableCell();
				tdHeader.Text="<b>Starttime</b>";
				trHeader.Cells.Add(tdHeader);
				tdHeader = new TableCell();
				tdHeader.Text="<b>Endtime</b>";
				trHeader.Cells.Add(tdHeader);
				tdHeader = new TableCell();
				tdHeader.Text="<b>Approved</b>";
				trHeader.Cells.Add(tdHeader);
				tdHeader = new TableCell();
				tdHeader.Text="<b>Deleted</b>";
				trHeader.Cells.Add(tdHeader);

				tdHeader.Dispose();
				tblHollidayView.Rows.Add(trHeader);
				trHeader.Dispose();
				IEnumerator e = al.GetEnumerator();
				while (e.MoveNext())
				{
					
					TableRow tr = new TableRow();
					TableCell td = new TableCell();

					Button btnAlter = new Button();
					
					btnAlter.CssClass = "buttonAction";
					btnAlter.CommandName = "alter";
					btnAlter.CommandArgument = Convert.ToString(((CHolliday)e.Current).Id);
					btnAlter.Command+=new CommandEventHandler(requestManMutation);
					btnAlter.Attributes.Add("onClick","openRequestWindow('"+Convert.ToString(((CHolliday)e.Current).Id)+"')");
					btnAlter.Text="Alter";
					td.Controls.Add(btnAlter);
					
					
					// --------------------------------------------------------------------------
					// ---------------------- Buttons created here ------------------------------
					// --------------------------------------------------------------------------
					if (((CUser)Session["user"]).Employeeid != id || ((CUser)Session["user"]).Managerid == id)
					{
						Button btnPurge = new Button();
						btnPurge.CssClass = "buttonAction";
						btnPurge.CommandName = "purge";
						btnPurge.CommandArgument = Convert.ToString(((CHolliday)e.Current).Id);
						btnPurge.Command+=new CommandEventHandler(requestManMutation);
						btnPurge.Text = "Delete";
						td.Controls.Add(btnPurge);

						Button btnApprove = new Button();
						btnApprove.CssClass = "buttonAction";
						btnApprove.CommandName = "approve";
						btnApprove.CommandArgument = Convert.ToString(((CHolliday)e.Current).Id);
						btnApprove.Command+=new CommandEventHandler(requestManMutation);
						btnApprove.Text = "Approve";
						td.Controls.Add(btnApprove);
					} else {
						Button btnDelete = new Button();
						btnDelete.CssClass = "buttonAction";
						btnDelete.CommandName = "delete";
						btnDelete.CommandArgument = Convert.ToString(((CHolliday)e.Current).Id);
						btnDelete.Command+=new CommandEventHandler(requestManMutation);
						btnDelete.Text = "Delete";
						td.Controls.Add(btnDelete);
					}
					// --------------------------------------------------------------------------
					// --------------------------------------------------------------------------
					// --------------------------------------------------------------------------
					
					tr.Cells.Add(td);
						
					td = new TableCell();
					td.Text = ((CHolliday)e.Current).TypeName;
					tr.Cells.Add(td);
					
					td = new TableCell();
					td.Text = ((CHolliday)e.Current).OwnerName;
					tr.Cells.Add(td);
					
					td = new TableCell();
					td.Text = ((DateTime)((CHolliday)e.Current).StartDate).ToString();
					tr.Cells.Add(td);
					
					td = new TableCell();
					td.Text = ((DateTime)((CHolliday)e.Current).EndDate).ToString();
					tr.Cells.Add(td);

					td = new TableCell();
					bool approved = ((CHolliday)e.Current).Approved;
					if (approved) 
					{
						td.Text = "True";
					} 
					else 
					{
						td.Text = "False";
					}
					tr.Cells.Add(td);
					td = new TableCell();

					bool deleted = ((CHolliday)e.Current).Deleted;
					if (deleted) 
					{
						td.Text = "True";
					} 
					else 
					{
						td.Text = "False";
					}
					tr.Cells.Add(td);

					td.Dispose();
					
					tblHollidayView.Rows.Add(tr);
					tr.Dispose();
				}
				tblHollidayView.DataBind();
				Session.Add("tblHollidayView",tblHollidayView);
			}
			catch(Exception ae)
			{
				Response.Write(ae.StackTrace.ToString()+" "+ae.Message.ToString());
			}
		}

[edit]please use
C#:
 tags [/cs ] [/edit][/COLOR]
 
Last edited by a moderator:
C#:
private bool ValidateLogin()
		{
			Response.Write("ValidateLogin()<br>");
			bool returnValue = false;
			
			if (((CUser)Session["user"]) != null)
			{
				returnValue = true;
			}
			return returnValue;
		}

		// ------------------------------------------------------------------------------------------------------------------

		// ------------------------------------------------------------------------------------------------------------------
		private bool fillDdlEmployee()
		{
			Response.Write("fillDdlEmployee()<br>");
			DataTable dt;
			//DataTable
			CDataRet dataRet = new CDataRet();

			if (Session["user"] != null && ((CUser)Session["user"]).Employeeid > -1){
				dt = dataRet.getData((CUser)Session["user"],"employees");
				if (dt != null) 
				{
					ddlEmployee.DataSource = dt;
					ddlEmployee.DataValueField = "id";
					ddlEmployee.DataTextField = "Name";
					if (!Page.IsPostBack){
						ddlEmployee.DataBind();
					}
					return true;
				} 
				else 
				{
					return false;
				}
			} else {
				return false;
			}
		}


		// ------------------------------------------------------------------------------------------------------------------
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			Response.Write("OnInit()");
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		//this.ddlEmployee.SelectedIndexChanged += new System.EventHandler(this.ddlEmployee_SelectedIndexChanged);
		//this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
		//this.Load += new System.EventHandler(this.Page_Load);
		private void InitializeComponent()
		{    
			this.ddlEmployee.SelectedIndexChanged += new System.EventHandler(this.ddlEmployee_SelectedIndexChanged);
			this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
			this.btnPersonalOverview.ServerClick += new System.EventHandler(this.btnPersonalOverview_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		// ------------------------------------------------------------------------------------------------------------------
		private void btnRequestHolliday_ServerClick(object sender, System.EventArgs e)
		{
		}

		// ------------------------------------------------------------------------------------------------------------------
		private DropDownList cloneDropDownList(DropDownList ddl)
		{
			Response.Write("cloneDropDownList()<br>");
			DropDownList retDdl = new DropDownList();
			
			IEnumerator i = ddl.Items.GetEnumerator();
			while (i.MoveNext())
			{
				if (i.Current != null)
				{
					retDdl.Items.Add((ListItem)i.Current);
				}
			}
			return retDdl;
		}

		// ------------------------------------------------------------------------------------------------------------------
		private void requestManMutation(Object sender, CommandEventArgs e)
		{
			// --------------------------------------------------------------------------
			// ----------------- Function for commandevent handling ---------------------
			// --------------------------------------------------------------------------

			CDataRet dataRet = new CDataRet();
			Response.Write("requestManMutation()<br>");
			if ((e.CommandName).Equals("delete"))
			{
				dataRet.deleteRow(Convert.ToInt32(e.CommandArgument));
			}
			if ((e.CommandName).Equals("purge"))
			{
				dataRet.purgeRow(Convert.ToInt32(e.CommandArgument));
			}
			if ((e.CommandName).Equals("approve"))
			{
				dataRet.approveRow(Convert.ToInt32(e.CommandArgument));
			}
			Response.Write("<br><br><br>___________________"+e.CommandName + "_" + e.CommandArgument +"<BR>");
			
			// --------------------------------------------------------------------------
			// --- If i enable this, Event is not responding to buttons -----------------
			// --------------------------------------------------------------------------
			
			/*if (Session["user"] != null){
				if (ddlEmployee.SelectedItem != null && ((CUser)Session["user"]).Employeeid != Convert.ToInt32(ddlEmployee.SelectedItem.Value))
					ShowHolliday(Convert.ToInt32(ddlEmployee.SelectedItem.Value));
				else
					ShowHolliday(((CUser)Session["user"]).Employeeid);
			}*/
			
			
		}


		// ------------------------------------------------------------------------------------------------------------------
		private void btnPersonalOverview_ServerClick(object sender, System.EventArgs e)
		{
			Response.Write("btnPersonalOverview_ServerClick()<br>");
			if (Session["user"] != null)
			{
				if (ddlEmployee.Items.FindByValue(Convert.ToString(((CUser)Session["user"]).Employeeid)) != null)
				{
					ddlEmployee.SelectedIndex = ddlEmployee.Items.IndexOf(ddlEmployee.Items.FindByValue(Convert.ToString(((CUser)Session["user"]).Employeeid)));
					ShowHolliday(((CUser)Session["user"]).Employeeid);				
				}
			}
		}

		private void ddlEmployee_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			/*
			if (Session["user"] != null){
				if (ddlEmployee.SelectedItem != null && ((CUser)Session["user"]).Employeeid != Convert.ToInt32(ddlEmployee.SelectedItem.Value))
					ShowHolliday(Convert.ToInt32(ddlEmployee.SelectedItem.Value));
				else
					ShowHolliday(((CUser)Session["user"]).Employeeid);
			}
			*/
		}

		private void btnLogout_Click(object sender, System.EventArgs e)
		{
			Response.Write("btnLogout_Click()<br>");
			Session.Remove("user");
			Page.Server.Transfer("login.aspx");
		}


		// ------------------------------------------------------------------------------------------------------------------

	}
}
[edit]please use
C#:
 tags [/cs ] [/edit][/COLOR]
 
Last edited by a moderator:
:eek: :eek:

Okay .. I see where the error is with the comments you added.. *takes a look*

*raises hand* I have questions.. :)

Have you tried running this in debug mode and stepped through to see what is exactly happening? You can add break points, etc and step through each line of code as it executes to see where exactly the problem may be occuring...

So just about all buttons in your code call this event? Is it just specific buttons that won't call the event, or all of them?

If you uncomment those lines of code the entire event does not fire? Is this linked with the other exact lines commented out in the other events?
 
First of all... thanks for the effort you are taking to look into my code :)

I have debugged the code.
It goes like this:

It runs through Page_Load. all goed well. ShowHolliday. buttons are displayed.
I press the 'approve' button(btnApprove in my code). It runs through the Page_Load again(obvious). ShowHolliday again. Buttons are displayed. And that's it! code stops.

I press the approve button again. Page_Load, ShowHolliday, and yes requestManMutaion is called! mutation on the data is performed. The screen always runs behind on the mutation, this is because it is already printed when i mutate the data. When i call the ShowHolliday after the mutation it should work, is it not that the requestManMutatyion is not called the first time, os screen is empty and that is where my problem lies... no matter what i do, it always goes wrong. screen not up to date or code not called.

greets Commodus
 
:(

To be honest I can't see any direct problem.. of course I've only skimmed it a few times to try and follow the code along. The error just seems a little odd.

When I have some more free time I'll try and take a closer look... hopefully someone who's more knowledgable then I am in ASP.NET (which should be just about anyone, heh) will find the time to take a look and find the problem.
 
There is no direct problem i guess...
the only thing is that I cannot figure out why this event is not always triggered. Is it overwritten? shut down before it is supposed to fire? simply deleted from the to-be-executed queue?

i don't know... and it frustrates me....
well... thanks for your help so far and I hope you can help me in the future. My .net knowledge isn't that strong also unfortunately

thanks
 
Mayby i'm looking all wrong here.
Is there mayby a better way to do this?
All i want is a collection of buttons on a row
Corresponding with a datarecord. Say I have 10 records all must have 'alter' and 'delete' buttons before them. If I click one of them it must do the action of the record(call the object that can perfor the operation) which it's assigned to by means of an id
 
Any Luck?

You probably don't remember much of this year old query regarding buttons not signaling appropriate handlers. I am having this exact same problem now and wonder if you found any solution to it? I was able to find out that it is caused by removing rows from the table as you do in your code. For some weird reason this confuses microsoft. I think it's a legitimate bug. -v
 
Back
Top