I am creating buttons in a table programmatically and the click event has stopped working. I must have done something earlier in the page, as it used to work. Any suggestions as to where I might look would be much appreciated.

The buttons are built into a table row, one row for each record in the database:

Button btnModify = new Button();
btnModify.ID = "btnModify" + rowCnt.ToString();
btnModify.Text = "Modify";
btnModify.Click += new System.EventHandler(btn_OnClick);
tCell.Controls.Add(btnModify);
                            
Button btnDelete = new Button();
btnDelete.ID = "btnDelete" + rowCnt.ToString();
btnDelete.Text = "Delete";
btnDelete.Click += new System.EventHandler(btn_OnClick);
tCell.Controls.Add(btnDelete);

The only code I have in my page load is

if(!IsPostBack)

during which I authenticate the user in active directory get the data related to their session. When the user clicks the button it never reaches btn_OnClick.

Thanks in advance for any help!

I am not sure but i guess the reason is that the name of both the button event is same btn_Onclick...this may be the reason...

try to change the name of event and then run..

btnModify.Click += new System.EventHandler(btnEdit_OnClick);
btnDelete.Click += new System.EventHandler(btnDelete_OnClick);

I am creating buttons in a table programmatically and the click event has stopped working. I must have done something earlier in the page, as it used to work. Any suggestions as to where I might look would be much appreciated.

The buttons are built into a table row, one row for each record in the database:

Button btnModify = new Button();
btnModify.ID = "btnModify" + rowCnt.ToString();
btnModify.Text = "Modify";
btnModify.Click += new System.EventHandler(btn_OnClick);
tCell.Controls.Add(btnModify);
                            
Button btnDelete = new Button();
btnDelete.ID = "btnDelete" + rowCnt.ToString();
btnDelete.Text = "Delete";
btnDelete.Click += new System.EventHandler(btn_OnClick);
tCell.Controls.Add(btnDelete);

The only code I have in my page load is

if(!IsPostBack)

during which I authenticate the user in active directory get the data related to their session. When the user clicks the button it never reaches btn_OnClick.

Thanks in advance for any help!

Member Avatar for siju kuriakose

Hi,
Pls put your code in Page Load event without check the condition if(!IsPostback).
Hope your reply ...

Please post the code inside entire page load event.Will help to analyze better

I've pulled the code out of the !IsPostBack test, and it works perfectly. I'm puzzled as to why the click event would cease firing if this code in in !IsPostBack.

Following, as a test, this I tried creating a session variable which I set once the user is authenticated. Putting the code inside a test for that session variable also causes it to fail.

There are 2 things that goes on inside !IsPostBack:
1. Active Directory authentication - I pull the Contect.User.Identity.Name to get the user's logon name, go to AD to get extensionAttribute1 and their given name.
2. I use the value of extensionAttribute1 to run a database query and return the data the user will want to edit.

All of this was working fine up until I needed to make one change: initially I had been retrieving the users given name and surname (sn) from AD; however we discovered inconsistencies between AD and our active data database hich caused issues using this method. So I had changed it to using extensionAttribute1 instead.

The database query is handled by a SqlDataSource object, which runs a stored procedure. It is set to cache the results. The results set populates a dropdown list. Once the dropdownlist is populated a function is called which build a table on the page, using data related to the first item in the dropdown. This table has row data received from a stored procedure (again using a SqlDataSource object), and the two buttons to modify or delete the row programtically built in. If the user changes the item in the dropdown selection, the page automaticallly rebuilds with the new data.

So - to keep it working right now I have the code outside of any check - which means it hits AD each time the user performs some sort of action. Something I would prefer to not have happen. Any thoughts would be greatly appreciated!

Thx.

Sorry - one mistake in my last post - caching is off - not on.

The problem is solved. Thanks for the feedback. I've broken the pageLoad into two bits: !IsPostBack, handling the Active Directory goo, and a test to see if the dropdown list is populated - if not it runs the StoredProc and populates it, otherwise it just skips over that bit so I don't get double entries in the dropdown. I have no idea at this time why it worked ok for several months before making these changes ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.