[英]How to Call javascript function on button click of Repeater event Repeater1_ItemDataBound
我想根据绑定到当前行的ID打开重复器按钮时打开弹出窗口。我编写此JavaScript函数,要根据唯一的行ID调用此Javascript函数。然后从重复器的ItemDataBound事件调用此Java脚本函数。
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";
}
}
}
}
在后面的代码中使用它:
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在该上下文中不存在。 在转发器中,每个按钮都有其自己的clientID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.