如何在Repeater事件Repeater1_ItemDataBound的按钮点击时调用javascript函数

本文关键字:按钮 调用 函数 javascript ItemDataBound Repeater 事件 Repeater1 | 更新日期: 2023-09-27 18:01:01

我想根据绑定到当前行的id打开中继器按钮点击的弹出窗口。我编写了这个javascript函数,我想根据唯一的行id调用它。然后这个java脚本函数从中继器的ItemDataBound事件中调用。

   This is java script code :
    <script type="text/javascript">
        function test(lblEduCatId) {
            window.onload = function() {
                var modal = document.getElementById('myModal');
                var btn = document.getElementById("<%=btnAccess.ClientID %>").value;
                var span = document.getElementsByClassName("close")[0];
                btn.onclick = function() {
                    modal.style.display = "block";
                }
                span.onclick = function() {
                    modal.style.display = "none";
                }
                window.onclick = function(event) {
                    if (event.target == modal) {
                        modal.style.display = "none";
                    }
                }
            }
        }

    </script>

.cs code
 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string lblEduCatId = DataBinder.Eval(e.Item.DataItem, "EduCatId").ToString();
            string lblIsFree = DataBinder.Eval(e.Item.DataItem, "IsFree").ToString();
            string lblAccess = DataBinder.Eval(e.Item.DataItem, "Access").ToString();

            Button access = (Button)e.Item.FindControl("btnAccess");
            access.OnClientClick = "return test(lblEduCatId);";
            if (Session["UserName"] != null)
            {
                if (lblAccess.ToString() == "True")
                {
                    access.Text = "Access";
                    access.CommandName = "Access";
                }
                else
                {
                    access.Text = "Subscribe";
                    access.CommandName = "Subscribe";
                }
            }
            else
            {
                if (lblIsFree.ToString() == "True")
                {
                    access.Text = "Access";
                    access.CommandName = "Access";
                }
                else
                {
                    access.Text = "Subscribe";
                    access.CommandName = "Subscribe";
                }
            }
        }

    }

如何在Repeater事件Repeater1_ItemDataBound的按钮点击时调用javascript函数

在后面的代码中使用这个:

Button access = (Button)e.Item.FindControl("btnAccess");
access.OnClientClick = "test(" + lblEduCatId + "); return false;";

在页面上:

<script type="text/javascript">
function test(lblEduCatId) {
    //open the modal with the correct lblEduCatId received from the repeater
}
</script>

您当前的代码将不起作用,因为在Repeater1_ItemDataBound中,您没有将变量lblEduCatId添加到OnClientClick,只是将"lblEduCatId"添加为纯文本字符串,并且没有引号。在javascript中使用的第二个

var btn = document.getElementById("<%=btnAccess.ClientID %>").value;

这也不起作用,因为btnAccess在该上下文中不存在。在中继器中,每个按钮都有自己的客户端ID。