C# .NET - getting confirm message even if checkbox is not selcted in repeater?

Asked By deepi singh on 10-Nov-11 05:24 AM

pls check my below code iam getting confirm messge even if the checkbox not selected?can u pls help me out..whts wrong with my code

function ConfirmDelete()

{

alert("Delete");

var btnDelete = document.getElementById("ctl00_PlaceHolderMain_btnDelete");

if (btnDelete.disabled == true)

{


return false;

}

else

{

alert("Enter");

var blnChecked = false;


for(i=0;i<document.all.length;i++)

{

elm = document.all[i];


if(elm.type=='checkbox')

{

if(elm.checked) ----------------------even checkbox not selected it is entering in this condition and rasing confirm message?

blnChecked =

true;


}

}


if(blnChecked)

{



var sWarning = "Are you sure you want to delete the footnote(s)?";

return confirm(sWarning);

}


else

{

return true;

}

}

Kirtan Patel replied to deepi singh on 10-Nov-11 05:30 AM
<script type="text/javascript">
  function ConfirmDelete()
  {
    var x = document.getElementById('<%= CheckBox1.ClientID %>');
    if (x.checked == true)
    {
      return confirm("Are you sure you want to delete?");
    }
    else
    {
      return false;
    }
  }
    
</script>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
  <asp:CheckBox ID="CheckBox1" Text="CheckBox" runat="server" />
  <asp:LinkButton ID="LinkButton1" OnClientClick="ConfirmDelete()" runat="server">LinkButton</asp:LinkButton>
</asp:PlaceHolder>
deepi singh replied to Kirtan Patel on 10-Nov-11 05:35 AM
i want in loop...iam having so may checkboxes in repeater...
dipa ahuja replied to deepi singh on 10-Nov-11 05:40 AM
Try using RepeaterItemDataBound

<script type="text/javascript">
    function check(chk) {
      if (!chk.checked) {
        alert('Please Check the Checkbox');
        return false;
      }
    }
  
</script>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Show" CommandName="show" /><br />

 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
    if (e.Item.ItemType == ListItemType.Item)
    {
      CheckBox chk = (CheckBox)e.Item.FindControl("CheckBox1");
      Button bt1 = (Button)e.Item.FindControl("Button1");
 
      bt1.Attributes.Add("onclick""javascript:return check(" + chk.ClientID + ")");
    }
  }