简体   繁体   English

数据列表中的复选框列表

[英]Checkbox list inside datalist

I have a checkbox list inside a datalist: 我在数据列表中有一个复选框列表:

     <asp:DataList ID="dtlstfilter" runat="server">
      <ItemTemplate>
      <div style="display: none;" id='<%#changes(Eval("FilterCode")) %>' class="p7ABcontent">
       <p>
        <asp:CheckBoxList AutoPostBack="true" Font-Size="12px" ID="chklist" runat="server" ></asp:CheckBoxList>
        </p>
        </div>
       </ItemTemplate>
       </asp:DataList>

And I loaded two list items in this to say 'yes' and 'no'. 我在其中加载了两个列表项,分别说“是”和“否”。 How can i get the event in the selected checkbox? 如何在选中的复选框中获取活动?

You need to bind SelectedIndexChanged event and pass the arguments to get the current rowNumber or anything else you need using custom attributes (user defined attributes). 您需要绑定SelectedIndexChanged事件并传递参数以获取当前rowNumber或使用自定义属性(用户定义的属性)所需的其他任何内容。

In html 在html中

<asp:DataList ID="dtlstfilter" runat="server"  >
    <ItemTemplate>
      <div style="display: none;" id='<%#changes(Eval("FilterCode")) %>' class="p7ABcontent">
         <p>
             <asp:CheckBoxList AutoPostBack="true" Font-Size="12px" ID="chklist" runat="server"  onselectedindexchanged="chklist_SelectedIndexChanged"   CommandName="myCommand" CommandArguments="1" DataListRowNumber="1" ></asp:CheckBoxList>
        </p>
     </div>
    </ItemTemplate>
</asp:DataList>

In Code behind 在后面的代码中

protected void chklist_SelectedIndexChanged(object sender, EventArgs e)
{
    CheckBoxList chklst = (CheckBoxList)sender;
    string commandName = chklst.Attributes["CommandName"].ToString();
    string commandArguments = chklst.Attributes["commandArguments"].ToString();
    string dataListRowNumber = chklst.Attributes["DataListRowNumber"].ToString();
}

Another way, just to think about. 换一种方式,只需要考虑一下。 This solution minimizes the roundtrips between client and server. 此解决方案最大程度地减少了客户端和服务器之间的往返时间。

Use a button's click event. 使用按钮的单击事件。 You can iterate through the DataListItems and use the FindControl method to find the CheckBoxList. 您可以遍历DataListItems并使用FindControl方法找到CheckBoxList。 Now you can determine which elements are checked: 现在,您可以确定要检查的元素:

foreach (DataListItem item in dtlstfilter.Items)
{
  if (item.ItemType == ListItemType.Item)
  {
    CheckBoxList checkBox = item.FindControl("chklist") as CheckBoxList;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM