我正在使用Gridview开发WebForm以显示客户端将检索的数据。但是我在Gridview中更新数据时遇到问题,在客户端检查CheckBox之后,你会按一个按钮来更新数据库中的值。但我的复选框总是返回FALSE。 我使用.Net framework 4
public void Button_Submit_Onclick(object sender, EventArgs e)
{
for (int i = 0; i < GridView2.Rows.Count; i++)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
int recordid = Convert.ToInt32(GridView2.DataKeys[i].Values[0]);
bool private1 = Convert.ToBoolean(GridView2.FindControl("CheckBox1"));
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Update DetailCosts set private='" + private1 + "' where recordid=" + recordid;
con.Open();
if (private1 == true)
{
cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
}
else
{
cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
cmd.Parameters.Add("@private1", SqlDbType.Bit).Value = private1;
}
cmd.ExecuteNonQuery();
con.Close();
}}
这是Gridview的代码。
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" DataKeyNames="recordid">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="recordid" HeaderText="recordid" SortExpression="recordid">
</asp:BoundField>
<asp:BoundField DataField="Employee" HeaderText="Employee" SortExpression="Employee">
</asp:BoundField>
<asp:BoundField DataField="mesec" HeaderText="Month" SortExpression="mesec">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="godina" HeaderText="Year" SortExpression="godina">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="CallDate" HeaderText="Call Date" SortExpression="CallDate">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PhoneType" HeaderText="Phone Type" SortExpression="PhoneType">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="land" HeaderText="Land" SortExpression="land">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="number" HeaderText="Number" SortExpression="number">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Minutes" HeaderText="Minutes" SortExpression="Minutes">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Costs" HeaderText="Costs" SortExpression="Costs">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="privateCost" HeaderText="Private Costs" SortExpression="privateCost">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Private" SortExpression="private" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("private") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Komentar" HeaderText="Commnet" SortExpression="Komentar">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>