2016-08-04 61 views
0

我想打開按鈕點擊中繼器上的彈出窗口根據綁定到當前行的id.I寫這個javascript函數,我想根據行ID調用這是唯一的。然後這個java腳本函數從中繼器的ItemDataBound事件中調用。如何在Repeater事件的按鈕點擊調用javascript函數Repeater1_ItemDataBound

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

在頁面上:

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

在後面的代碼中使用此

<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。

+0

感謝您的回答,這有助於解決我的問題。 – Monika

相關問題