ASP.NET onClientClick返回false无效

时间:2019-04-28 00:12:52

标签: javascript asp.net autopostback

我要在单击按钮时隐藏div,并在不同的按钮单击中显示它们。可悲的是,在图像按钮单击上始终有一个回发。是什么引起了这个问题?

这是我的后端代码

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i <= 10; i++)
    {
        HtmlGenericControl divTest = new HtmlGenericControl("div");
        divTest.Attributes.Add("class", "divClass");
        divTest.Attributes.Add("ID", "myDIV");
        divTest.InnerText = "Div" + i;
        form1.Controls.Add(divTest);

        ImageButton collapseButton = new ImageButton();
        collapseButton.ImageUrl = "~/images/minus.png";
        collapseButton.Attributes.Add("OnClientClick", "myHideFunction(); return false;");
        collapseButton.Height = 20;
        collapseButton.Width = 20;
        divTest.Controls.Add(collapseButton);

        ImageButton expandButton = new ImageButton();
        expandButton.ImageUrl = "~/images/plus.png";
        collapseButton.Attributes.Add("OnClientClick", "myShowFunction(); return false;");
        expandButton.Height = 20;
        expandButton.Width = 20;
        form1.Controls.Add(expandButton);
    }
}

这是我的前端代码

CSS

<style>
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
</style>

JavaScript

<script>

function myShowFunction() {
    var x = document.getElementById("myDIV");
    x.style.display = "block";
}

function myHideFunction() {
    var x = document.getElementById("myDIV");
    x.style.display = "none";
}
</script>

1 个答案:

答案 0 :(得分:1)

在客户端上添加按钮:

public class BackgroundElement : MonoBehaviour{

[SerializeField]
private float **speed;**

public void Move()
{

    transform.Translate(Vector2.left * speed * Time.smoothDeltaTime);
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
[SerializeField]
private BackgroundElement[] backgroundElements;

void Start()
{

}

void Update()
{
    if (true)
    {
        foreach (BackgroundElement element in backgroundElements)
        {
            element.Move();
        }
    }
}
}

您需要从这样的方法中返回false:

collapseButton.OnClientClick = "return myHideFunction();");