Articles → .NET → Add Event Handler On Button At Run Time In ASP.NET

Add Event Handler On Button At Run Time In ASP.NET









protected void btnSave_Click(object sender, EventArgs e) {
	if (Request.QueryString.Keys.Count > 0) {
		//  Write a logic for update
	}
	else {
		//  Write a logic for add
	}
}









Implementation




<body>
	<form id="form1" runat="server">
		<div>
			<asp:Button ID="btnSave" runat="server" Text="Save" />
		</div>
	</form>
</body>




public EventHandler addButtonClickEventHandler;
public EventHandler updateButtonClickEventHandler;






addButtonClickEventHandler = new EventHandler(addEvent);
updateButtonClickEventHandler = new EventHandler(updateEvent);

if (Request.QueryString.Keys.Count > 0) {
	btnSave.Click -= addButtonClickEventHandler;
	btnSave.Click += updateButtonClickEventHandler;
}
else {
	btnSave.Click -= updateButtonClickEventHandler;
	btnSave.Click += addButtonClickEventHandler;
}






void addEvent(object sender, EventArgs e) {
	//  Write an add logic
}
void updateEvent(object sender, EventArgs e) {
	//  Write an update logic
}







Posted By  -  Karan Gupta
 
Posted On  -  Monday, December 26, 2011

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250