我想根据绑定到当前行的id打开转发器按钮上的弹出窗口。我根据唯一的行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";
}
}
}
}
答案 0 :(得分:0)
在后面的代码中使用它:
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中,只需要&lt; lblEduCatId&#39;作为纯文本字符串,没有引号。 在您使用的JavaScript中第二个
var btn = document.getElementById("<%=btnAccess.ClientID %>").value;
这也不起作用,因为btnAccess在该上下文中不存在。在转发器中,每个按钮都有自己的clientID。