Search code examples
javascriptasp.netrepeater

Confirm Delete pop up with Record Count


General Info:

Aspx page holds an Ascx User control. Inside the User control, the Repeater is contained inside a View, contained inside a Multiview. Asp.Net 2.0 framework / C#

Details:

I have a repeater (inside an ascx user control) that shows records, and the first column is a checkbox. If checked, that row will be deleted. OUtside the repeater, I have a button that will deleted all rows that are checked.

Everything works fine, but have been asked to add a pop up "confirm delete" message that includes the number of records that will be deleted if the user clicks "Ok" on the pop up.

Something like: "You are about to delete 8 records".

Currently my button looks like this:

    <asp:Button ID="btnDeleteAllRecords" runat="server" Text="Delete all Checked Records" Onclick="btnDeleteAllRecords_Click" OnClientClick="javascript:GetCbCount();" />

I have this javascript code block:

<script type="text/javascript">
function GetCbCount()   
{
var cb = document.getElementById("rptrVoicemail").getElementsByTageName("input");
  var cbCount;   
for(i = 0;  i < cb.lenght; i++)   
{   
    if(cb[i].type == "checkbox")   
    {   
        if(cb[i].checked)   
        {   
            cbCount =  cbCount + 1;   
        }               
    }       
}
return confirm('You are about to delete' + cbCount + 'records.');

} 
</script>

When I click my button I'm getting:

Error: 'document.getElementById(...)' is null or not an object on this line:

var cb = document.getElementById("rptrVoicemail").getElementsByTageName("input");

Why is the JS not seeing my repeater? Is it because it's buried inside a MultiView? How can the JS be corrected so that the pop up will show the record count in the message?

UPDATE: I changed the script to:

function GetCbCount(){ 
   var inpt = document.getElementById("vmDiv"); 
   var checkboxes = inpt.getElementsByTagName("input"); 
   var cbCount; 
   for(i = 0; i<checkboxes.lenght;i++){ 
      if (checkboxes[i].type == "checkbox" && checkboxes[i].checked){ 
         cbCount = cbCount + 1;
      } 
   } 
   return confirm('You are about to delete ' + cbCount + ' Voicemails.'); 
} 

Solution

  • This should work:

    document.getElementById('<%= rptrVoicemail.ClientID %>').getElementsByTageName("input");
    

    Another approach is this little script that returns the ClientID. You could add it even to an included JS-file.

    function GetClientId(strid)
    {
         var count=document.forms[ 0 ].length ;
         var i = 0 ;
         var eleName;
         for (i = 0 ;  i < count ;  i++ )
         {
           eleName = document.forms [ 0 ].elements[ i ].id;
           pos=eleName.indexOf( strid ) ;
           if(pos >= 0)  break;          
         }
        return eleName;
     }
    

    Found here.