I'm trying to call a event with a JavaScript.
But when I click the button that fire the event on the script drop me this error:
JavaScript runtime error: 'newButton_Click' is undefined
this is my script
<script>
$(".btn-primary .outline .separate").click(function (e) {
newButton_Click($(this), e)
})
</script>
and this is the event that I want to fire:
protected void newButton_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
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();
}
to fire the event I'm assigning an OnClientClick
:
newButton.OnClientClick = "newButton_Click";
I'm using the class to find the button because the id is dynamically and I want that many dynamically buttons make the same event:
newButton.CssClass = "btn-primary outline separate";
[EDITED]
here is where I create the buttons:
protected void Page_Init(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();
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 += (se, ev) =>
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
Button Btnclick = (Button)se;
var teams = Btnclick.Text;
List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == name && x.TEAM == teams && x.STANDBY == 0).ToList();
var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
Grv_Eng.DataSource = ListOfToolsOk;
Grv_Eng.DataBind();
};
pan1.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
}
this is the event that fire the Page_Init
protected void DButton(object sender, EventArgs e)
{
AssignClicked = true;
Page_Init(sender, e);
}
The values to AssignClicked are those:
public bool AssignClicked
{
get
{
return Convert.ToBoolean(ViewState["AssignClicked"]);
}
set
{
ViewState["AssignClicked"] = value;
}
}