在论坛中查看了很多关于checkbox联动选中的贴子,但是我不知道如何用程序(后台程序,即通过查询数据库获取的状态,来决定是否选中此项)来控制这些checkbox的选中与否,偶的页面如下所示
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="347px">
<Columns>
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="<input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);' />全选" FooterText="全选">
<ItemTemplate>
<input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' onclick='SingleCheckJs();' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>请各位帮忙,谢谢了
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="347px">
<Columns>
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="<input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);' />全选" FooterText="全选">
<ItemTemplate>
<input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' onclick='SingleCheckJs();' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>请各位帮忙,谢谢了
即数据库中的bit型!至于其他情况就要在RowDataBound事件中手工处理了!
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="347px" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="<input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);' />全选" FooterText="全选">
<ItemTemplate>
<input type="checkbox" runat="server" id="checkboxname" name="checkboxname" value='' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("id");
dt.Rows.Add(new string[] { "name1", "0" });
dt.Rows.Add(new string[] { "name2", "1" }); this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)e.Row.Cells[1].FindControl("checkboxname");
cb.Checked = Convert.ToBoolean(Convert.ToInt32(((DataRowView)e.Row.DataItem)["id"]));
}
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox) row.FindControl("checkboxname")).Checked;
}