我从数据库中选择数据并通过asp:Repeater绑定数据。 我在ItemTemplate中创建了复选框并从数据库接收数据。 当我“检查”任何行上的复选框时,我想更新数据库中的数据。
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;
}
...
<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> </td>
<td align="left" width="350"><asp:Label id="lblLevelDesc" runat="server"></asp:Label> </td>
<td align="left" width="25"><asp:Label id="lblChoiceID" runat="server"></asp:Label> </td>
<td align="left" width="350"><asp:Label id="lblChoiceDesc" runat="server"></asp:Label> </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>
答案 0 :(得分:2)
foreach (RepeaterItem rpItem in myRepeater.Items)
{
CheckBox chkbx = rpItem.FindControl("chkSelected") as CheckBox;
if (chkbx.Checked)
{
//your code here
}
}
答案 1 :(得分: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>
希望有所帮助
答案 2 :(得分: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();
}
}
}
它是来自数据库的显示数据,但是当我在Checkbox上“选中”时没有更新。