1

我有一个中继器,里面有一个复选框。现在我想根据列值(0/1)检查它。我已经通过转发器的 itemDataBound 事件进行了尝试。如果行的值为 1,那么它会做什么,然后它选中所有复选框,如果第一个复选框未选中,那么它的所有复选框都未选中。我的代码是:-`

                    <td align="center">
                        <asp:CheckBox ID="chk" runat="server" />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>`

ItemDataBound 事件代码是:-

 protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataTable dt = new DataTable();
    dt = obj.abc(id);
    if (dt.Rows.Count > 0)
    {
        CheckBox chk = (CheckBox)(e.Item.FindControl("chk"));
        if (chk != null)
        {

            if (Convert.ToInt32(dt.Rows[0]["xyz"]) == Convert.ToInt32("0"))
            {
                chk.Checked = false;
            }
            else
            {
                chk.Checked = true;

            }
        }

    }

}
4

1 回答 1

3

有很多方法可以做到这一点。

  1. 您可以编写内联 ASP.NET:

    <asp:CheckBox id='isMarried' runat='server' 
    Checked='<%# Convert.ToBool(Eval("IsMarried")) ? true : false %>' />
    
  2. 正如您所提到的,您可以使用repeater_ItemDataBound找到每一行的复选框,并相应地设置其值:

    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Person person = (Person)e.Item; // e.Item is the datasoruce of the row
        if (person.IsMarried) 
        {
            CheckBox isMarried = (CheckBox)(e.Item.FindControl("isMarried"));
            isMarried.Checked = true;
        }
    }
    
  3. Another way could be to inject the Boolean state (here, the marriage state) in a hidden field and send it to the client, then on client-side, using jQuery (or any other JavaScript framework), update check-boxes' checked state according to the value of those hidden fields:

于 2011-12-09T06:54:48.147 回答