我有一个项目表,其中项目是否仍处于活动状态的值为bit
。我创建了一个存储过程,用于提取项目名称和项目的值(0
表示非活动状态,1
表示活动状态。当我在我的网页上显示时,bit
值显示在复选框中(这很好,因为我希望能够使用按钮更新这些值)。
问题在于我无法点击或取消点击它们。
如何正确提取数据,以便点击项目名称的复选框进行更新?
非常感谢任何帮助。
public void loadProj()
{
SqlConnection con;
DataTable dt = new DataTable();
string CS = ConfigurationManager.ConnectionStrings["ProjDB"].ConnectionString;
using (con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("GetProjActive", con);
SqlDataAdapter sa = new SqlDataAdapter(cmd);
callStoredProcedure(cmd, sa);
sa.Fill(dt);
GridView.DataSource = dt;
GridView.DataBind();
}
}
答案 0 :(得分:0)
您需要在asp:TemplateField
列中设置GridView
,并在此模板中设置asp:CheckBox
控件。
设置CheckBox
值:应使用CheckBox
事件将布尔数据库值绑定到OnRowDataBound
。
更改数据库值:在模板字段的OnCheckedChanged
控件上注册CheckBox
事件。
HTML标记:
<asp:GridView ID="gv"
runat="server"
DataSourceID="..."
OnRowDataBound="gv_DataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk_selection" runat="server" OnCheckedChanged="chk_selection__CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>
代码背后:
protected void gv_DataBound(object sender, GridViewRowEventArgs e)
{
// Get this row's CheckBox control
CheckBox chkSelector = (CheckBox)e.Row.FindControl("chk_selection");
// Cast the database boolean value and set the checkbox's Checked property
chkSelector.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "bool_field"));
}
protected void chk_selection__CheckedChanged(object sender, EventArgs e)
{
// Update your database
}