如何提醒用户是否在转发器内选中了复选框 [英] How to alert user whether check box is checked or not inside repeater

查看:71
本文介绍了如何提醒用户是否在转发器内选中了复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在选中复选框时提醒用户。

我在转发器内使用了复选框。





我试过这样的..



I need alert the user when the check box is checked or not.
I have used check box inside the repeater.


I have tried like this..

 <script type="text/javascript">
      function validate() {
          var count = 0;
          var repeater = document.getElementById("check");
          var checkBoxes = repeater.getElementsByTagName("input");
          for (var i = 0; i <checkBoxes.length; i++)
           {
              if (checkBoxes[i].type == "checkbox" && checkBoxes[i].checked) 
              {
                  count++;
              }
            
             
          }
          if (count == 0) {
              alert("Kindly select the feedback to delete");
              return false;
          }
          else {
              confirm("Are you sure you want to delete?");
          }
      }
</script

>







>



<asp:Repeater ID="feedback" runat="server"   onitemcommand="RepterDetails_ItemCommand">
<ItemTemplate>
<table style="border-top:1px dotted #3b5998; width:650px" id="check">
<tr><td>

<asp:CheckBox ID="chk" runat="server" EnableViewState="true" />
</td>
</tr>
</table>


</ItemTemplate>
<FooterTemplate>
<tr>
<td>
    <table width="650px" style="background-color:White;height:10px">

    <td align="center">
    <asp:Button ID="lnkDelSelected" ForeColor="white" runat="server" Text="Delete Selected" onclick="LinkButton1_Click" OnClientClick="return (validate());"   CssClass="item_button"></asp:Button>
    </td></tr>
    </Table>
    </td>
    </tr>
    </FooterTemplate>
</asp:Repeater>









但是它无法正常工作。



如果在转发器中选中了第一个复选框,则显示正确的警告消息。

如果在转发器中选中第一个和任何其他复选框,则显示正确的警告消息





问题是,



如果我选择剩余的复选框,而不选择第一个复选框,



不接受那个条件复选框。检查






请建议我这个条件。





提前感谢





but its not working correctly.

if the first check box is checked in repeater, its showing correct alert message.
if the first and any other checkbox is checked in repeater, its showing correct alert message


the problem is,

if i have select remaining checkboxes, without selecting first check box,

its not taking that condition checkbox.checked



kindly suggest me the condition.


thanks in advance

推荐答案





使用以下链接



http://aspsnippets.com/Articles/Validate-Repeater-with-CheckBox-at-least-one-checked -using-JavaScript-in-ASPNet.aspx [ ^ ]

http://bytes.com/topic/asp-net/answers/861378-checking-if-atleast- one-checkbox-has-been-checked-using-javascript [ ^ ]


它怎么可能有效?您甚至没有尝试处理更改复选框的选中状态的事件。您可以处理此元素的事件 onclick ,并在事件处理程序中检查状态。请参阅:

http://www.w3schools.com/jsref/event_onclick.asp [ ^ ],

http://www.w3schools.com/jsref/prop_checkbox_checked.asp [ ^ ]。



< dd> -SA
How could it possibly work? You are not even trying to handle the event of changing the checked state of the check box. You can handle the event onclick of this element, and, in the event handler, checkup the state. Please see:
http://www.w3schools.com/jsref/event_onclick.asp[^],
http://www.w3schools.com/jsref/prop_checkbox_checked.asp[^].

—SA


请尝试以下解决方案,让我知道它是否有效:

我不明白为什么你的代码不起作用。这似乎是正确的。



在这里我做了一个解决方案,它的工作完美:



带页脚按钮的转发器:

Please try following solution and let me know if it works :
I don't understand why your code does not work. It seems correct.
:
Here I have made one solution and it's working perfect :

Repeater with footer button :
<asp:Repeater ID="rptUsers" runat="server">
               <HeaderTemplate>
                   <table id="check" border="1">
                       <tr>
                           <th>Select</th>
                           <th>Name
                           </th>
                           <th>Country
                           </th>
                       </tr>
               </HeaderTemplate>
               <ItemTemplate>
                   <tr>
                       <td>
                           <asp:CheckBox ID="chkbox" Text="" runat="server" />
                       </td>
                       <td>
                           <%#Eval("Name") %>
                       </td>
                       <td>
                           <%#Eval("Country") %>
                       </td>
                   </tr>
               </ItemTemplate>
               <FooterTemplate>
                   <tr>
                       <td>
                           <table style="background-color: yellow; height: 10px">
                               <tr>
                                   <td>
                                       <asp:Button ID="btn" ForeColor="Green" runat="server" OnClick="btn_Click" Text="Delete Selected" OnClientClick="return (Validate());"></asp:Button>
                                   </td>
                               </tr>
                           </table>
                       </td>
                   </tr>
                   </table>
               </FooterTemplate>
           </asp:Repeater>





绑定转发器和按钮点击后面的代码:



Code behind to bind repeater and button click :

protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             //bind repeater with dummy data
             rptUsers.DataSource = BindGrid();
             rptUsers.DataBind();
         }
     }

     //btn click example
     protected void btn_Click(object sender, EventArgs e)
     {
         Response.Write("Post back done");
     }


     //Dummy data to bind repeater
     private DataTable BindGrid()
     {
         DataTable dt = new DataTable();
         dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                     new DataColumn("Name", typeof(string)),
                     new DataColumn("Country",typeof(string)) });
         dt.Rows.Add(1, "Rajesh Biswas", "India");
         dt.Rows.Add(2, "Johny Depp", "US");
         dt.Rows.Add(3, "Mad Hatter", "US");
         dt.Rows.Add(4, "Willy Bonka", "US");
         return dt;
     }





Javascript代码:





Javascript code :

<script type="text/javascript">
    function Validate() {
        var count = 0;
        var repeater = document.getElementById("check");
        var checkBoxes = repeater.getElementsByTagName("input");
        for (var i = 0; i < checkBoxes.length; i++) {
            if (checkBoxes[i].type == "checkbox" && checkBoxes[i].checked) {
                count++;
            }
        }
        if (count == 0) {
            alert("Kindly select the feedback to delete");
            return false;
        }
        else {
            return confirm("Are you sure you want to delete?");
        }

        //Using Jquery
        //var chkboxrowcount =


这篇关于如何提醒用户是否在转发器内选中了复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆