-5

我从数据库中选择数据并通过 asp:Repeater 绑定数据。我在 ItemTemplate 中创建复选框并从数据库接收数据。当我“选中”任何行上的复选框时,我想更新数据库中的数据。

protected void myRepeater_ItemDataBound(对象发送者,RepeaterItemEventArgs e){

        //*** Level ID ***//
        Label lblLevelID = (Label)(e.Item.FindControl("lblLevelID"));
        if ((e.Item.FindControl("lblLevelID") != null))
        {
            lblLevelID.Text = DataBinder.Eval(e.Item.DataItem, "LevelID").ToString();
            strLevelID = lblLevelID.Text;
            //MyArr[Convert.ToInt16(In), 0] = strLevelID;
        }

...

<asp:Repeater id="myRepeater" runat="server" OnItemDataBound="myRepeater_ItemDataBound">
    <HeaderTemplate>
        <table border="1" style="font-size:12px;" width="800">
    </HeaderTemplate>
    <ItemTemplate>  
        <tr>
            <td align="left" width="25"><asp:Label id="lblLevelID" runat="server"></asp:Label>&nbsp;</td>
            <td align="left" width="350"><asp:Label id="lblLevelDesc" runat="server"></asp:Label>&nbsp;</td>
            <td align="left" width="25"><asp:Label id="lblChoiceID" runat="server"></asp:Label>&nbsp;</td>
            <td align="left" width="350"><asp:Label id="lblChoiceDesc" runat="server"></asp:Label>&nbsp;</td>
            <td align="center" width="50"><%--<input type="checkbox" id="chkSelected" OnCheckedChanged="chkSelected_OnCheckedChanged"  AutoPostBack="True" />--%>
            <asp:CheckBox ID="chkSelected" runat="server" />
            <asp:Label ID="lblTest" runat="server"></asp:Label>
            </td>
        </tr>           
    </ItemTemplate>
    </asp:Repeater>
4

3 回答 3

2
foreach (RepeaterItem rpItem in myRepeater.Items)
{
  CheckBox chkbx = rpItem.FindControl("chkSelected") as CheckBox;
  if (chkbx.Checked)
  {
    //your code here
  }
}
于 2012-10-10T10:09:15.010 回答
0

使用 jQuery

<asp:CheckBox ID="chkSelected" runat="server" CssClass="sb" />

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript" language="javascript"></script>

<script type="text/javascript">

$(document).ready(function() {

    if($(".cb").is(":checked"))
    {
        alert("Well done!");
    }

});

</script>

如果要注册更改事件:

<script type="text/javascript">

$(document).ready(function() {

    $(".cb").live("change", function(){

        if($(this).is(":checked"))
        {
            alert("I am checked!");
        }
        else
        {
            alert("I am unchecked!");
        }

    });

});
</script>

希望能帮助到你

于 2012-10-10T10:56:36.530 回答
0

现在我使用这段代码

 protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    { 

        //*** Level ID ***//
        Label lblLevelID = (Label)(e.Item.FindControl("lblLevelID"));
        if ((e.Item.FindControl("lblLevelID") != null))
        {
            lblLevelID.Text = DataBinder.Eval(e.Item.DataItem, "LevelID").ToString();
            strLevelID = lblLevelID.Text;
            //MyArr[Convert.ToInt16(In), 0] = strLevelID;
        }
        //*** Selected ***//
        CheckBox chkSelected = (CheckBox)(e.Item.FindControl("chkSelected"));
        if ((e.Item.FindControl("chkSelected") != null))
        {
            if (DataBinder.Eval(e.Item.DataItem, "Selected").ToString() == "True")
            {
                chkSelected.Checked = true;
                if (chkSelected.Checked)
                {
                    UpdateData0();
                }
            }
            else
            {
                chkSelected.Checked = false;
                if (chkSelected.Checked)
                {
                    UpdateData1();
                }
            }

        }

它显示来自数据库的数据,但当我在复选框上“选中”时没有更新。

于 2012-10-11T02:49:14.667 回答