<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Q" HeaderText="Q" />
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:CheckBox ID="CCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="CCheckbox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="R">
<ItemTemplate>
<asp:CheckBox ID="RCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="RCheckbox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="E">
<ItemTemplate>
<asp:CheckBox ID="ECheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="ECheckbox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是GridView的Aspx代码
if (ViewState["SearchGrid"] == null)
{
SearchTable.Columns.Add(new DataColumn("Q", typeof(string)));
SearchTable.Columns.Add(new DataColumn("C", typeof(bool)));
SearchTable.Columns.Add(new DataColumn("R", typeof(bool)));
SearchTable.Columns.Add(new DataColumn("E", typeof(bool)));
ViewState["SearchGrid"] = SearchTable;
}
SearchTable =(DataTable) ViewState["SearchGrid"];
DataRow dr = null;
dr = SearchTable.NewRow();
dr["Q"] = MySearchTextBox.Text;
switch (SearchType)
{
case "Contains": dr["C"] =true;
break;
case "Related": dr["R"] = true;
break;
case "Exact": dr["E"] = true;
break;
}
和我的代码背后。
在添加行时,我必须检查特定复选框。 我一直试图通过将其值设置为true来检查复选框,但它似乎不起作用。谁能告诉我这里做错了什么?
答案 0 :(得分:1)
您可以在标记中绑定值:Checked ='&lt;%#Eval(“C”)%&gt;'
<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Q" HeaderText="Q" />
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:CheckBox ID="CCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="CCheckbox_CheckedChanged" Checked='<%# Eval("C") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="R">
<ItemTemplate>
<asp:CheckBox ID="RCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="RCheckbox_CheckedChanged" Checked='<%# Eval("E") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="E">
<ItemTemplate>
<asp:CheckBox ID="ECheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="ECheckbox_CheckedChanged" Checked='<%# Eval("R") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
或者您可以使用rowdatabound事件:OnRowDataBound =“SearchGrid_RowDataBound”
MarkUP更改:
<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false" OnRowDataBound="SearchGrid_RowDataBound">
Codebehind:注意我使用了列表对象而不是数据表。
public class TestObject
{
public string Q { get; set; }
public bool C { get; set; }
public bool R { get; set; }
public bool E { get; set; }
}
public partial class Default : System.Web.UI.Page
{
public List<TestObject> Values { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//NEED SOME DATA TO TEST THE RESULT
Values = new List<TestObject>();
Values.Add(new TestObject() { Q = "test 1", C = true, E = true, R = true });
Values.Add(new TestObject() { Q = "test 1", C = true, E = false, R = true });
Values.Add(new TestObject() { Q = "test 1", C = true, E = true, R = false });
Values.Add(new TestObject() { Q = "test 1", C = false, E = true, R = true });
//BIND TO THE GRID
SearchGrid.DataSource = Values;
SearchGrid.DataBind();
}
}
//FIRES FOR EVERY ROW IN THE GRID
protected void SearchGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//WE ONLY CARE ABOUT THE DATAROW NOT HEADER ETC
if (e.Row.RowType != DataControlRowType.DataRow)
return;
//GET THE BOUND INDIVIDUAL ITEM
TestObject obj = (TestObject)e.Row.DataItem; //IN YOUR CASE THIS WOULD BE: DataRow row = (DataRow)e.Row.DataItem
//FIND ALL THE CHECKBOXES
CheckBox cchk = e.Row.FindControl("CCheckBox") as CheckBox;
CheckBox echk = e.Row.FindControl("ECheckBox") as CheckBox;
CheckBox rchk = e.Row.FindControl("RCheckBox") as CheckBox;
//CHECK IT OR NOT BASED ON THE DATATITEMS VALUE
if (cchk != null)
cchk.Checked = obj.C; //IN YOUR CASE THIS WOULD BE: bool c = (bool)row["C"];
if (echk != null)
echk.Checked = obj.E;
if (rchk != null)
rchk.Checked = obj.R;
}
}
答案 1 :(得分:0)
您可以尝试查看GridView.RowDataBound的功能。使用此事件在绑定数据时捕获行,然后您应该能够访问CheckBox项并分配其.Checked值。
链接:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx