我正在尝试在选中Checkbox时运行一个函数(通过AutoPostBack),由于某种原因,它不起作用,并且OnChecked函数不会发生。
这是我的代码:
<table class="table table-hover task-table">
<asp:DataList ID="ListOfTasks" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal" ClientIDMode="Static">
<ItemTemplate>
<tr>
<td style="width: 20px;">
<asp:Label runat="server" Visible="false" ID="TaskIdVal" Text='<%# Eval("TaskId") %>'></asp:Label>
<asp:CheckBox ID="DoneCb" runat="server" AutoPostBack="true" OnInit="OnChecked" ClientIDMode="Static"/>
</td>
<td><%# Eval("Name") %></td>
<td><%# Eval("UserId").ToString() != Session["userid"].ToString() ? GlobalFunctions.ParagraphEvalHtml("<i class='glyphicon glyphicon-share'></i> ", (Eval("FirstName") + " " + Eval("LastName")), "") : string.Empty %></td>
<td><span class="text-left text-sm-2 pull-left"><%# Eval("DueDate") == null ? string.Empty : GlobalFunctions.ParagraphEvalHtml("<i class='glyphicon glyphicon-time'></i> ", Eval("DueDate").ToString(), "") %></span></td>
<td>
<asp:LinkButton runat="server" CssClass="btn btn-sm btn-danger pull-left" Text="<i class='glyphicon glyphicon-trash' style='color:#fff;'></i>" /></td>
</tr>
</ItemTemplate>
</asp:DataList>
</table>
这是代码隐藏(.cs):
protected void OnChecked(object sender, EventArgs e)
{
foreach (DataListItem dli in ListOfTasks.Items)
{
CheckBox chk = (CheckBox)dli.FindControl("DoneCb");
Label lbl = (Label)dli.FindControl("TaskIdVal");
if (chk.Checked)
{
GlobalFunctions.DoneTask(lbl.Text);
}
}
}
最后,这是函数:
public static void DoneTask(object TaskId)
{
db.update("UPDATE Tasks SET Done = 1, DoneDate = '"
+ DateTime.Now + "' WHERE Id = '"+TaskId+"'");
}
我非常感谢你的帮助: - )