我有一个GridView,其中列是一个复选框。在数据库中,这些复选框被隐藏到一个。因此,如果用户取消选中该复选框,我会调用Web方法来更新数据库。
GridView代码:
<asp:GridView style="width:75%"
ID="gvCVRTDetails"
ShowHeaderWhenEmpty="true"
CssClass="tblResults"
runat="server"
OnRowDataBound="gvCVRTDetails_RowDataBound"
DataKeyField="ID"
AutoGenerateColumns="false"
allowpaging="false"
AlternatingRowStyle-BackColor="#EEEEEE">
<HeaderStyle CssClass="tblResultsHeader" />
<Columns>
<asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Checklist Items"></asp:BoundField>
<asp:BoundField DataField="Checked" HeaderText="OK"></asp:BoundField>
</asp:GridView>
代码背后:
protected void gvCVRTDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
lookupCVRT work = (lookupCVRT)e.Row.DataItem;
GridView gv = sender as GridView;
e.Row.Attributes.Add("ID", "gvCVRTDetails_" + work.ID);
e.Row.Cells[0].Attributes.Add("onclick", "event.stopPropagation();");
HtmlGenericControl lnkShowHide = (HtmlGenericControl)e.Row.FindControl("lnkShowHide");
HyperLink ChecklistItem = (HyperLink)e.Row.FindControl("ID");
bool bAll = work.Checked;
e.Row.Cells[4].Text = "<input type=\"checkbox\" " + ((bAll) ? "checked" : "") + "/>";
e.Row.Cells[4].Attributes.Add("onclick", "UpdateCheckedBox(" + work.ID.ToString() + ", this.Checked);");
}
}
Javascript功能:
function UpdateCheckedBox(ID, Checked) {
alert(Checked);
PageMethods.UpdateCheckUserControl(ID, Checked, OnUpdateSuccess, OnUpdateFail);
}
但是调用this.Checked
返回undefined而不是true或false。我也试过this.Value
,但这不起作用
答案 0 :(得分:0)
您应该将最后一行更改为以下
e.Row.Cells[4].Attributes.Add("onclick", "UpdateCheckedBox('" + work.ID.ToString() + "', this.checked);");
因为如果你不在quotes
左右覆盖work.ID.ToString()
,那么它将被视为一个变量,而且它是错误的。与其他人建议的那样,使用checked
代替&#39; Checked&#39;。
<强>更新强>
e.Row.Cells[4].Text = "<input onclick=UpdateCheckedBox('" + work.ID.ToString() + "', this.checked) type=\"checkbox\" " + ((bAll) ? "checked" : "") + "/>";
答案 1 :(得分:0)
答案正在从boundfield
复选到TemplateField
:
<asp:TemplateField HeaderText ="OK?" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkChecked" runat="server" Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Checked")) %>' ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
然后在使用后面的代码中:
CheckBox chkChecked = e.Row.FindControl("chkChecked") as CheckBox;
chkChecked.Attributes.Add("onclick", "UpdateCheckedBox(" + work.ID.ToString() + ", this.checked);");
更容易选择复选框而不是像我想要的那样选择单元格。