生成的HTML示例:
<span disabled="disabled"><input id="ctl00_ContentBody_GridView_ctl02_ctl01" type="checkBox" name="ctl00$ContentBody$GridView$ctl02$ctl01" checked="checked" disabled="disabled" /></span>
有人会说出怎么弄清楚吗?
解决方法
>添加编辑链接
在您的GridView标签中,添加AutoGenerateEditButton =“True”。当您的GridView在浏览器中呈现时,您现在应该会找到一个标记为“编辑”的超链接。如果您单击它,则GridView中的字段将变为可编辑,并且“编辑”链接将成为两个链接,一个用于将更改保存到数据库,另一个将其丢弃。使用这种方法,可以根据您如何进行数据绑定,将GridView中的更改连接到数据库。此示例使用sqlDataSource控件。
alt text http://philippursglove.com/stackoverflow/checkboxgridview1.png
alt text http://philippursglove.com/stackoverflow/checkboxgridview2.png
>在其中添加一个带有CheckBox的TemplateField
在< columns>内标签,您可以添加您为自己设置数据绑定的TemplateField。
< asp:TemplateField HeaderText =“Discontinued”>
<&的ItemTemplate GT;
< asp:CheckBox runat =“server”ID =“DiscontinuedCheckBox”Checked =“<%#Eval(”Discontinued“)%>” AutoPostback =“true”OnCheckedChanged =“DiscontinuedCheckBox_CheckedChanged”/>
< / ItemTemplate中>
< / ASP:的TemplateField>
alt text http://philippursglove.com/stackoverflow/checkboxgridview3.png
此复选框将被启用,但您需要自己做这些工作以将任何更改反映回数据库。只要你可以获得一个数据库密钥,这是很简单的,因为你需要在某个时候运行一个UPDATE语句,并且你想在右边的一行运行它!这里有两种方法可以做到这一点:
在您的Gridview标签中,添加DataKeyNames =“MyDatabasePrimaryKey”。然后在您的CheckedChanged事件处理程序中,您需要找到您正在处理的行,并在DataKeys数组中查找。
protected void DiscontinuedCheckBox_CheckedChanged(object sender,EventArgs e) { CheckBox DiscontinuedCheckBox; sqlConnection conn; sqlCommand cmd; int productId; GridViewRow selectedRow; // Cast the sender object to a CheckBox DiscontinuedCheckBox = (CheckBox)sender; // We can find the row we clicked the checkBox in by walking up the control tree selectedRow = (GridViewRow)DiscontinuedCheckBox.Parent.Parent; // GridViewRow has a DataItemIndex property which we can use to look up the DataKeys array productId = (int)ProductGridView.DataKeys[selectedRow.DataItemIndex].Value; using (conn = new sqlConnection(ProductDataSource.ConnectionString)) { cmd = new sqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; if (DiscontinuedCheckBox.Checked) { cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductId.ToString(); } else { cmd.CommandText = "UPDATE Products SET Discontinued = 0 WHERE ProductId = " + ProductId.ToString(); } conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } }
或者,您可以在HiddenField控件中添加密钥:
< asp:TemplateField HeaderText =“Discontinued”>
<&的ItemTemplate GT;
< asp:hiddenfield runat =“server”id =“ProductIdHiddenField”Value ='<%#Eval(“ProductID”)%>‘ />
< asp:CheckBox runat =“server”ID =“DiscontinuedCheckBox”Checked =“<%#Eval(”Discontinued“)%>” AutoPostback =“true”OnCheckedChanged =“DiscontinuedCheckBox_CheckedChanged”/>
< / ItemTemplate中>
< / ASP:的TemplateField>
protected void DiscontinuedCheckBox_CheckedChanged(object sender,EventArgs e) { CheckBox DiscontinuedCheckBox; HiddenField ProductIdHiddenField; DiscontinuedCheckBox = (CheckBox)sender; ProductIdHiddenField = (HiddenField)DiscontinuedCheckBox.Parent.FindControl("ProductIdHiddenField"); using (conn = new sqlConnection(ProductDataSource.ConnectionString)) { ... if (DiscontinuedCheckBox.Checked) { cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductIdHiddenField.Value; } ... }