2
<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 来检查复选框,但它似乎不起作用。有人可以告诉我我在这里做错了什么吗?

4

2 回答 2

1

您可以在标记中绑定到值:Checked='<%# Eval("C") %>'

<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"

标记更改:

<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false" OnRowDataBound="SearchGrid_RowDataBound">

代码隐藏:注意我使用了列表对象而不是数据表。

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;
    }
}
于 2013-09-06T16:48:00.007 回答
0

您可以尝试检查 GridView.RowDataBound 的功能。使用此事件捕获绑定了数据的行,然后您应该能够访问 CheckBox 项并分配其 .Checked 值。

链接:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

于 2013-09-06T16:32:15.003 回答