I'm trying to get fire a click event but wen I click over the dynamically button created this doesn't fire the event, I make some break points and this don't show an error or something else, I see the console of IE and doesn't show an error.
this is my code in page_load
:
protected void Page_Load(object sender, EventArgs e)
{
if (AssignClicked)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
Button Btn_clic = (Button)sender;
var name = Btn_clic.Text;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();
List<Button> Botones = new List<Button>();
var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
foreach (var team in TeamFCH)
{
Button newButton = new Button();
newButton.CommandName = "Btn" + Convert.ToString(team);
newButton.ID = "Btn_" + Convert.ToString(team);
newButton.Text = team;
newButton.CommandArgument = name;
newButton.Click += new System.EventHandler(newButton_Click);
newButton.OnClientClick = "return ModalGood();";
Botones.Add(newButton);
GoodPanel.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
}
before the Page_load
I make this:
public bool AssignClicked
{
get
{
return Convert.ToBoolean(ViewState["AssignClicked"]);
}
set
{
ViewState["AssignClicked"] = value;
}
}
Here is where I make the click that change the AssignClicked
value to true:
protected void Assign_Click(object sender, EventArgs e)
{
AssignClicked = true;
Page_Load(sender, e);
}
And this is the Event of the dynamicalliy button created:
protected void newButton_Click(object sender, EventArgs e)
{
Button Btnclick = (Button)sender;
var team = Btnclick.Text;
string name = Btnclick.CommandArgument;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == name && x.TEAM == team && x.STANDBY == 0).ToList();
var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
Grv_Eng.DataSource = ListOfToolsOk;
Grv_Eng.DataBind();
}
I was guided by this example LinkButton onclick event is not triggering for dynamically created buttons c#
The button that fire the event Assign_Click
:
<asp:Button ID="Btn_mch" class="btn btn-primary area" Text="MCH" runat="server" Width="110px" OnClick="Assign_Click"></asp:Button>
Here is where the dynamically buttons get insert:
<asp:UpdatePanel ID="UpdatePanel3" runat="server" ControlID="Btn_eng">
<ContentTemplate>
<div id="tools_info" role="dialog">
<div class="modal-dialog modal-sm" style="width: 450px">
<div class="modal-content">
<div class="modal-header">
<h3 id="title-info"><span class="glyphicon glyphicon-list-alt"></span>  Tools information</h3>
</div>
<div>
<%--<asp:Button ID="Btn_good" Text="Good condition" CssClass="btn-link" runat="server" />--%>
<button id="Btn_good" class="btn-link" onclick='TableGood();'" type="button">Good Condition</button>
<asp:Button ID="Btn_bad" Text="Bad condition" CssClass="btn-link" runat="server" OnClientClick="TableBad();" />
</div>
<br />
<div class="container">
<div id="tgood">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="GoodPanel" runat="server">**HERE IS WHERE BUTTONS GET ADD**</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btn_eng" EventName="Click"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
</div>
<div id="tbad">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="BadPanel"></asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btn_bad" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</div>
<br />
</div>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btn_eng" EventName="Click"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>