我正在尝试更新我的测试表以将值存储在数据库中。我想只更新启用的复选框值而不是禁用复选框值。我已经尝试了下面的代码,但它显示错误:
无法将'System.Web.UI.WebControls.Button'类型的对象强制转换为 输入'System.Web.UI.WebControls.CheckBox'。
<asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gv1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="mGrid1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="datedif" HeaderText="Day/Hour" SortExpression="datedif" />
<asp:TemplateField HeaderText="Hour1">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn1" runat="server" OnClick="Check_Click(this)" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour1" runat="server" Checked='<%# bool.Parse( Eval("hour1").ToString()) %>' Enabled='<%# Eval("hour1").ToString().Equals("False") %>'
OnCheckedChanged="CheckBox1_CheckedChanged" OnClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour2">
<EditItemTemplate>
<asp:CheckBox ID="ChkColumn2" runat="server" OnClick="Check_Click(this)" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour2" runat="server" Checked='<%# bool.Parse( Eval("hour2").ToString()) %>' Enabled='<%# Eval("hour2").ToString().Equals("False") %>'
OnCheckedChanged="CheckBox6_CheckedChanged" OnClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour3">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn3" runat="server" OnClick="Check_Click(this)" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour3" runat="server" Checked='<%# bool.Parse( Eval("hour3").ToString()) %>' Enabled='<%# Eval("hour3").ToString().Equals("False") %>'
OnCheckedChanged="CheckBox3_CheckedChanged" OnClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour4">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn4" runat="server" OnClick="Check_Click(this)" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour4" runat="server" Checked='<%# bool.Parse( Eval("hour4").ToString()) %>' Enabled='<%# Eval("hour4").ToString().Equals("False") %>'
OnCheckedChanged="CheckBox4_CheckedChanged" OnClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour5">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn5" runat="server" OnClick="Check_Click(this)" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour5" runat="server" Checked='<%# bool.Parse( Eval("hour5").ToString()) %>' Enabled='<%# Eval("hour5").ToString().Equals("False") %>'
OnCheckedChanged="CheckBox5_CheckedChanged" OnClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
foreach (GridViewRow r in Gv1.Rows)
{
if (
(
(
(
(((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour1"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour1"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour2"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour2"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour3"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour3"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour4"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour4"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour5"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour5"))).Checked == false)
)
)
)
)
{
CheckBox chk = (CheckBox)sender;
bool hour1 = (r.FindControl("chkHour1") as CheckBox).Checked;
bool hour2 = (r.FindControl("chkHour2") as CheckBox).Checked;
bool hour3 = (r.FindControl("chkHour3") as CheckBox).Checked;
bool hour4 = (r.FindControl("chkHour4") as CheckBox).Checked;
bool hour5 = (r.FindControl("chkHour5") as CheckBox).Checked;
string datedif = r.Cells[0].Text;
object subject1 = DBNull.Value, subject2 = DBNull.Value, subject3 = DBNull.Value, subject4 = DBNull.Value, subject5 = DBNull.Value;
if (hour1)
{
subject1 = ddlsubj.SelectedValue;
}
if (hour2)
{
subject2 = ddlsubj.SelectedValue;
}
if (hour3)
{
subject3 = ddlsubj.SelectedValue;
}
if (hour4)
{
subject4 = ddlsubj.SelectedValue;
}
if (hour5)
{
subject5 = ddlsubj.SelectedValue;
}
SqlCommand comm = new SqlCommand();
comm.Connection = con2;
comm.CommandType = CommandType.Text;
con2.Open();
if (chk.Enabled && chk.Checked==true)
{
comm = new SqlCommand("UPDATE test SET subject1=@subject1,subject2=@subject2,subject3=@subject3,subject4=@subject4,subject5=@subject5,hour1 = CASE WHEN (@hour1)= 'false' THEN 'false' ELSE 'true' END, hour2 = CASE WHEN (@hour2)= 'false' THEN 'false' ELSE 'true' END, hour3 = CASE WHEN (@hour3)= 'false' THEN 'false' ELSE 'true' END , hour4 = CASE WHEN (@hour4)= 'false' THEN 'false' ELSE 'true' END , hour5 = CASE WHEN (@hour5)= 'false' THEN 'false' ELSE 'true' END where datedif=@datedif", con2);
}
comm.Parameters.AddWithValue("@subject1", subject1);
comm.Parameters.AddWithValue("@subject2", subject2);
comm.Parameters.AddWithValue("@subject3", subject3);
comm.Parameters.AddWithValue("@subject4", subject4);
comm.Parameters.AddWithValue("@subject5", subject5);
comm.Parameters.AddWithValue("@hour1", hour1);
comm.Parameters.AddWithValue("@datedif", datedif);
comm.Parameters.AddWithValue("@hour2", hour2);
comm.Parameters.AddWithValue("@hour3", hour3);
comm.Parameters.AddWithValue("@hour4", hour4);
comm.Parameters.AddWithValue("@hour5", hour5);
comm.ExecuteNonQuery();
con2.Close();
}
}
}
这些是我的(复选框)发件人文件。
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < Gv1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)Gv1.Rows[i].Cells[0].FindControl("chkHour1");
if (chk.Checked == true)
{
chk.Enabled = false;
}
else
{
chk.Enabled = true;
}
}
}