0

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;
            }
        }
  • Include your ASP markup, Classes are not the way to target ASP elements. You'd use `ClientIDMode="static"` or `<%=MyButton.ClientId%>` – Tyler Roper Jul 05 '17 at 19:06
  • Possible duplicate of [Calling an ASP.NET EventHandler from JavaScript](https://stackoverflow.com/questions/9067320/calling-an-asp-net-eventhandler-from-javascript) – Wiktor Zychla Jul 05 '17 at 19:08
  • but I can't use the id, because the is assigned dynamically to many different buttons, and I want that many buttons make the same event – Cesar Gutierrez Davalos Jul 05 '17 at 19:12
  • We need to see your ASP. We can't just "guess" how your buttons are set up. – Tyler Roper Jul 05 '17 at 19:13
  • A general answer involving `ClientScript.GetPostbackEventReference` is provided under the question I point yours is a duplicate of. – Wiktor Zychla Jul 05 '17 at 19:16
  • The click event should not be client-side. Right now you're using the server-side, to bind a click event on the client-side, that triggers an event on the server-side. Check this out: https://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button – Tyler Roper Jul 05 '17 at 19:19
  • hmm I insist, I can't use the id, there are many buttons with different id that use the same event, in the supposed duplicate this use an unique id, so I think this is a different problem... just saying – Cesar Gutierrez Davalos Jul 05 '17 at 19:20
  • Possible duplicate of [How can i create dynamic button click event on dynamic button?](https://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button) – Tyler Roper Jul 05 '17 at 19:21
  • if I do like the solution that shows in there, drop me this error `A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something ` – Cesar Gutierrez Davalos Jul 05 '17 at 19:24
  • Then change `e` to something else... `newButton.Click += (se, ev) => button_Click(se, ev);` – Tyler Roper Jul 05 '17 at 19:25
  • well, the event click doesn't fire – Cesar Gutierrez Davalos Jul 05 '17 at 19:29
  • Where is the `foreach` located, and why do you insist on drip-feeding us the information? – Tyler Roper Jul 05 '17 at 19:30
  • I just edit it @Santi – Cesar Gutierrez Davalos Jul 05 '17 at 19:36
  • @Cesar Gutierrez Davalos: are `protected void newButton_Click` and `newButton.Click += (se, ev) =>` used for the same thing? In this case you shouldn't use Javascript at all. You've already attached the same server-side event for all buttons and when any button is clicked, then postback is performed and server-side event will be called. – Evgeny Gorb Jul 05 '17 at 19:58
  • when I move the code of `newButton_Click` I comment that part in my code. however I know I need to use JavaScript but how can I make it? the way that I use doesn't work – Cesar Gutierrez Davalos Jul 05 '17 at 20:03
  • @Cesar Gutierrez Davalos: Please correct me if I wrong. This is how I understood yor logic: You are getting a list of teams and creating a button dynamically for each team. Then, when any button is clicked, then a grid is filled with data based on team related to the button clicked. Right? – Evgeny Gorb Jul 05 '17 at 20:15
  • @EvgenyGorb yes, you are totally right – Cesar Gutierrez Davalos Jul 05 '17 at 20:19
  • @Cesar Gutierrez Davalos: How do you define and set value to `AssignClicked`? – Evgeny Gorb Jul 05 '17 at 20:36
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148449/discussion-between-evgeny-gorb-and-cesar-gutierrez-davalos). – Evgeny Gorb Jul 05 '17 at 20:38

0 Answers0