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;
            }
        }
19
  • Include your ASP markup, Classes are not the way to target ASP elements. You'd use ClientIDMode="static" or <%=MyButton.ClientId%> Commented Jul 5, 2017 at 19:06
  • Possible duplicate of Calling an ASP.NET EventHandler from JavaScript Commented Jul 5, 2017 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 Commented Jul 5, 2017 at 19:12
  • We need to see your ASP. We can't just "guess" how your buttons are set up. Commented Jul 5, 2017 at 19:13
  • 1
    Let us continue this discussion in chat. Commented Jul 5, 2017 at 20:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.