473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Event click doesn't fire with dynamically buttons created

I'm creating dynamically buttons with some click event but when I click it this doesn't work


the page_init is before my Page_Load
Expand|Select|Wrap|Line Numbers
  1. public bool AssignClicked
  2.         {
  3.             get
  4.             {
  5.                 return Convert.ToBoolean(ViewState["AssignClicked"]);
  6.             }
  7.             set
  8.             {
  9.                 ViewState["AssignClicked"] = value;
  10.             }
  11.         }
  12.  
  13.  
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Init(object sender, EventArgs e)
  2.         {
  3.             if (AssignClicked)
  4.             {
  5.                 ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
  6.  
  7.                 Button Btn_clic = (Button)sender;
  8.                 var name = Btn_clic.Text;
  9.  
  10.                 List.ListUsers listArea = new List.ListUsers();
  11.                 List<Data.Area> Area = listArea.AreaList();
  12.  
  13.                 List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();
  14.  
  15.                 List<Button> Botones = new List<Button>();
  16.  
  17.                 var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
  18.  
  19.                 foreach (var team in TeamFCH)
  20.                 {
  21.                     Button DButton = new Button();
  22.                     DButton.CommandName = "Btn" + Convert.ToString(team);
  23.                     DButton.ID = "Btn_" + Convert.ToString(team);
  24.                     DButton.Text = team;
  25.                     DButton.CommandArgument = name;
  26.  
  27.                     DButton.Click += new EventHandler(DynamoButton_Click);
  28.  
  29.                     Botones.Add(DButton);
  30.  
  31.                     GoodPanel.Controls.Add(DButton);
  32.                     DButton.CssClass = "btn-primary outline separate";
  33.                 }
  34.             }
  35.         }
  36.  
Expand|Select|Wrap|Line Numbers
  1. these is the button that create them
  2.  
  3.         protected void DButton(object sender, EventArgs e)
  4.         {
  5.             AssignClicked = true;
  6.             Page_Init(sender, e);
  7.         }
This is the event that I want to get fired

Expand|Select|Wrap|Line Numbers
  1. protected void DynamoButton_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             Button Btnclick = (Button)sender;
  5.             var team = Btnclick.Text;
  6.             string name = Btnclick.CommandArgument;
  7.  
  8.             List.ListUsers listArea = new List.ListUsers();
  9.             List<Data.Area> Area = listArea.AreaList();
  10.  
  11.             List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == "ENG" && x.TEAM == team && x.STANDBY == 0).ToList();
  12.  
  13.             var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
  14.             Grv_Eng.DataSource = ListOfToolsOk;
  15.             Grv_Eng.DataBind();
  16.             ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
  17.         }
Jul 4 '17 #1
1 2637
Frinavale
9,735 Expert Mod 8TB
The ViewState isn't loaded during the Initialization life cycle stage. The ViewState isn't loaded until the Load stage which occurs after the Initialization stage.

I recommend saving the AssignClicked into a hidden field (or a cookie if you prefer) that you can retrieve from the request object to determine what controls need to be dynamically created during the Initialization event... so that the view state (and other information) about the controls can be loaded during the Load stage.

This is a link to the MSDN documentation for the ASP.NET Page Life Cycle Overview

-Frinny
Jul 10 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jim Mitchell | last post by:
I have the code snippet below. I fill a table of imagebuttons and would like to know which one was clicked to trigger the post back. Unfortunately, the Command event does not fire unless I load...
2
by: Sam Miller | last post by:
Hi, I have a button event that won't fire. I left it on Friday and it worked fine. I came back in on Monday and it won't fire. I tried putting another button and just putting a...
2
by: hn | last post by:
Hi, I have linkbuttons created dynamically and they display fine on the web page. However, when I click on the those link buttons, the event doesn't fire. Please tell me what's wrong with the...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
2
by: Tim_Mac | last post by:
hi, i have an aspx page which dynamically loads a user control and adds it to a placeholder. the control is recreated and added to the placeholder for postbacks as well. the user control...
2
by: kohoutec | last post by:
Hi Im very much a beginner so apologies if my terminology is wrong. I have a combo box with some items already loaded, the users can also enter a new item. If they enter a new item and then...
2
by: Ryan Liu | last post by:
Hi, I need the MouseDown event be trigged everytime when you press mouse in a datagrid (System.Windows.Forms). But seems mouse event will only fire once, and it seems changed to edit mode...
1
by: SAL | last post by:
Hello, I created a datagrid where I set the Events to there associated functions (i.e. Grid1_UpdateCommand, etc.). All these Events I have established work as they are suppose to except for my...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
4
by: gamesforums | last post by:
Hi! I have some code within the Application_Start event in Global.asax that runs a sqlscript against my sql server to check the databas version. My problem is that sometimes the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.