Dynamically created html controls - Detecting click event? ASP.NET C# - c#

I'm work on a project in Asp.net (C#), I've the following code (C#) that generate dynamically html buttons (for each item in my list) at run time:
In my .aspx file:
<div id="myDiv" runat="server" ...></div>
In my .aspx.cs file:
List<Item> list;
private void Display()
{
foreach (Item item in list)
AddButton(item);
}
private void AddButton(Item item)
{
HtmlButton button = new HtmlButton();
button.InnerText = item.Content;
button.ID = item.name;
button.ServerClick += new EventHandler(Item_Click);
myDiv.Controls.Add(button);
}
private void Item_Click(Object sender, EventArgs e)
{
// HERE I WANT TO DO SOMETHING ON THE SPECIFICALLY
// CLICKED BUTTON,
// THE PROBLEM IS THAT I CAN'T KNOW WHICH BUTTON OF
// THE DYNAMICALLY ADDED BUTTONS (BY THE AddButton()
// FUNCTION) IS CLICKED, NOTE: I HAVE MORE THAN 10 ITEMS IN
// list. I MEAN I CAN'T PASS THE CLICKED BUTTON AS A
// PARAMETER TO THIS Item_Click FUNCTION, IN GENERAL, WHAT I
// WANT IS SOMETHING LIKE:
// Item_Click(Object sender, EventArgs e, HtmlButton clickedButton)
// WITH SUCH DECLARATION OF Item_Click FUNCTION I CAN DO WHAT
// I WANT ON THE CLICKED BUTTON, BUT IT DOESN'T WORK THAT
// WAY, BECAUSE EventHandler's PARAMETERS ARE: Object,
// EventArgs, AND IT WOULD NOT HELP ME TO CREATE MY CUSTOM
// EVENTHANDLER BECAUSE HtmlButton.ServerClikc IS OF TYPE
// EventHandler.
}
Any suggestions?
Thanks for Help!

You must fill list in page_load without if(!IsPostBack)
void Page_Load(object sender, EventArgs e )
{
// don`t use if(!IsPostBack) because every postback your contols leave and you must register your controls.
list=new List<Item>();
list.Add(...);
list.Add(...);
list.Add(...);
}

Related

Fire Menu Strip Item Click Event Dynamically in C# Windows Form

I have already added a menu strip controller and added click events for those menu items.
Ex:
private void mnuaddTestingBuyersForFigures_Click(object sender, EventArgs e)
{
frmAddTestingBuyersForFigure obj = new frmAddTestingBuyersForFigure();
objUserManagerBll.SetAccess(obj, User.UserID);
IsAlreadyLoded(obj);
}
private void mnuOperatorIncentive_Click(object sender, EventArgs e)
{
frmOperatorIncentive obj = new frmOperatorIncentive();
objUserManagerBll.SetAccess(obj, User.UserID);
IsAlreadyLoded(obj);
}
private void mnuSetUpIncentiveMonthProcess_Click(object sender, EventArgs e)
{
frmSetUpWeeksForIncentiveMonth obj = new frmSetUpWeeksForIncentiveMonth();
objUserManagerBll.SetAccess(obj, User.UserID);
IsAlreadyLoded(obj);
}
Here I want fire above click event from another event. I just want to pass the menu strip name as parameter to the method and fire corresponding event of it.
Ex :
ShowMe("mnuSetUpIncentiveMonthProcess");
//Out put open the frmSetUpWeeksForIncentiveMonth form
ShowMe("mnuOperatorIncentive");
//Out put open the frmOperatorIncentive form
Without using conditional statements
You can find the control by its name and then call its PerformClick() method:
private void ShowMe(string name)
{
var item = (MenuStripItem)this.Controls[name];
item.PerformClick();
}

linkbuttons behaviour postbacks versus not postback

Can you help me understand why, when the page is first loaded by the example below, the buttons don't work as intended eg Button 2 doesn't call GetItems(int.Parse("2"), 3); but rather calls GetItems(int.Parse("4"), 3); however after the first postback all the buttons work correctly e.g. Buttonx calls GetItems(int.Parse("x"), 3);
Thanks
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetItems(1, 2); //default values (first time the page is loaded)
}
GenerateButtons(5);
}
private void GenerateButtons(int c)
{
LinkButton[] x = new LinkButton[c];
for(int i=0; i<c;i++)
{
x[i] = new LinkButton();
x[i].Text = (i+1).ToString();
Panel1.Controls.Add(x[i]);
x[i].OnClick += new EventHandler(Button_Click);
}
}
protected void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender; // Which button was clicked;
GetItems(int.Parse(button.Text), 3); //3 is a constant; first argument is index of button extracted from its caption
}
PS. when I refer to button 1 I have button[0] in mind. button2=button[1] and so on. after postback button1 is correctly attached to the event to trigger GetItems(1,3). Before the postback button1 causes GetItems(3,3) to run. Not as intended
Use the Page_Init so it will work on the first load
protected void Page_Init(object sender, EventArgs e)
{
GenerateButtons(5);
}
Source: https://msdn.microsoft.com/en-us/library/ms178472.aspx
Init Raised after all controls have been initialized and any skin
settings have been applied. The Init event of individual controls
occurs before the Init event of the page. Use this event to read or
initialize control properties
.
When you dynamically create controls you do so in Page_PreInit not Page_Load
protected void Page_PreInit(object sender, EventArgs e)
{
GenerateButtons(5);
}
This article explains and will help you
http://www.robertsindall.co.uk/blog/dynamically-adding-web-controls/

Intercept button click on page load

I need to write a code that can intercept a click of some button (asp button) than execute some code, and if a method return true, call the original click.
So the points are:
1- I don´t know how to save the original click.
2- Identify the button that was clicked.
Ex:
protected void Page_Load(object sender, EventArgs e)
{
var b = getButtonThatWasClicked();
var originalClick = b.Click;
if(callSomeMethod(b))
originalClick(null,null);
}
EDIT:
Ok managed to get the button who made the click doing this...Now i need to prevent the original Click to get called. The method bellow didn't worked. Even overriding the original click to a new Handler the old Handler got called and executed. I thing ASP.NET read it and make something like a call stack of events to get called.Even if the handler change the old still in the stack.
public void ButtonsTestMethod()
{
var listOfButtons = listaDeBotes.Where(b => b.CommandName != "");
foreach (var button in listOfButtons)
{
if (Request.Form[button.UniqueID] != null)
{
var buttonFromRequest = Request.Form[button.UniqueID];
if (buttonFromRequest == null)
continue;
if (button.CommandName != "xxx")
{
//validate things
//if(TemPermissao(Tela,GetAcaoDoBotao(botao.CommandName)))
//if(!canexecuteSomething())
button.Click += new EventHandler(defaultClick);
}
}
}
}
void defaultClick(object sender, EventArgs e)
{
//show error
}
protected void Page_Load(object sender, EventArgs e)
{
//other code
ButtonsTestMethod();
}
I don´t know if its possible but would appreciate some help =/
Thanks.
To get the control name, you can try the following in the page load:
protected void Page_Load(object sender, EventArgs e)
{
if( IsPostBack )
{
string senderControl = Request.Params["__EVENTTARGET"].ToString();
//senderControl will contain the name of the button/control responsible for PostBack
}
}
The first argument in the button click event handler is the sender. You can cast that sender as a Button object and then you should be able to identify which button that was based on that object. That way, you can eliminate having that function to figure out which is clicked.
void GreetingBtn_Click(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
if(clickedButton.Text == "bla")
{
//Do stuff
}
}

Can't call Click Event on dynamic button

I want to create a method in code behind that creates a button and places it in a PlaceHolder. I want this button to have a Click event.
After calling the "test" method button is placed correctly but the click event is not called.
private void test()
{
Button linkBtn1 = new Button();
linkBtn1.Text = "linkBtn1";
linkBtn1.OnClientClick = "return false;";
linkBtn1.Click += new EventHandler(linkBtn1_Click);
PagesPlaceHolder.Controls.Add(linkBtn1);
}
void linkBtn1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Removing the OnClientClick = "return false;" is necessary but not sufficient to get this to work. If you want the event handler on the dynamically added button to be triggered, you'll need to add this button every time the page loads.
One simple way would be to save the fact that the button has been added in ViewState, and then check that on PageLoad, and re-add the button if needed.
Here's a sample that works for me (and throws the exception when clicked)
protected void Page_Load(object sender, EventArgs e)
{
//if the button was added previously, add it again
if (ViewState["Added"] != null && (bool)ViewState["Added"])
addButton();
}
//this is the method that adds the button
protected void add_Click(object sender, EventArgs e) {
ViewState["Added"] = true;
addButton();
}
private void addButton() {
Button linkBtn1 = new Button();
linkBtn1.Text = "linkBtn1";
linkBtn1.Click += new EventHandler(linkBtn1_Click);
placeholder1.Controls.Add(linkBtn1);
}
void linkBtn1_Click(object sender, EventArgs e) {
throw new Exception("Button Click Event Triggered. Hello yellow screen!!!");
}
As #oleksii points out, have the clientside code is returning false so the form never gets submitted. You need to comment this out, then your event handler should fire.

How to add EventHandle _NOT_ at Page_Load

I'm making dynamic table at Page_LoadComplete, I cant do in at Page_Load becouse data can be changed during events process, so at Page_LoadComplete i make some buttons and want add them EventHandler:
protected void Page_LoadComplete(object sender, EventArgs e) {
btn.Click += new EventHandler(b_Click);
}
But it doesnt work, how to add events to button not at Page_Load?
simple, even not dynamic code:
void b_Click(object sender, EventArgs e) {
Label1.Text = "!!!";
}
protected void Page_Load(object sender, EventArgs e) {
Button1.Click += new EventHandler(b_Click);
}
on Page_Load works fine.
protected void Page_LoadComplete(object sender, EventArgs e) {
Button1.Click += new EventHandler(b_Click);
}
on Page_LoadComplete do nothing.
Ok here's the example:
Given this html snippet:
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" Text="Click Me" OnClick="go_" />
</div>
</form>
</body>
public partial class WebForm1 : System.Web.UI.Page
{
public int ControlCount
{
get { return ViewState["Controls"] == null ? 0 : (int)ViewState["Controls"]; }
set { ViewState.Add("Controls", value); }
}
protected void Page_Load(object sender, EventArgs e)
{
for(int i = 0; i < ControlCount; i++)
{
Button b = new Button();
b.Click += btn_Click;
b.Text = "Hi";
form1.Controls.Add(b);
}
}
void btn_Click(object sender, EventArgs e)
{
((Button)sender).Text = "bye";
}
protected void go_(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Hi";
form1.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
ControlCount++;
}
}
Every time you click the first button a new button will be added to the page with the text "Hi", and every time you click a "Hi" button the text for THAT button only will change to "Bye"
This works because I add the controls twice. Once in the event handler of the main button, where I determine that I need a new control, and then AGAIN in OnLoad where I have a new, empty page again, but now I know (via ControlCount) how many controls I added.
The problem is the control doesn't exist on postback (since you created it at run time). Creating the control in Init should solve this, and will allow the event to fire. You can change all of the properties in LoadComplete if you like, but the button needs to exist at that point.
What you're trying will never work because controls that are added dynamically must be REadded every time the page reloads.
Since your control population is based on the data being used in the form, you should put all your control creation logic in a single function, and in your event handlers, maniuplate the data then call your control creation logic function, while in Page Load you would JUST call the control creation function (presuming that you've already persisted the data between calls) so that all the controls that were created the first go around get recreated. Then, once they have been recreated, they can respond to the events that they fired.

Categories