0

我试图搜索这个问题并且出现了很多结果,但不完全是我得到的,所以这里是:

我有一个简单的 GridView 控件,我想在提交后访问子控件的值

我正在这样做:

<asp:GridView ID="gvQuery" runat="server" GridLines="None" CellPadding="5" CellSpacing="5"
    OnRowDataBound="gvQuery_RowDataBound" ShowHeader="False" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField ItemStyle-Width="20px">
            <ItemTemplate>
                <asp:CheckBox ID="chkActive" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Description" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="ddlCondition" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtField1" runat="server" />
                <span class="text2">and&nbsp;<asp:TextBox ID="txtField2" runat="server" /></span>
                <asp:HiddenField ID="hfFieldName" runat="server" Value='<%# Eval("InternalName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:Button runat="server" ID="btnSearch" Text="   Search   " 
                onclick="btnSearch_Click" />

然后,在btnSearch_Click事件中,我有正常的循环

foreach (GridViewRow gvr in gvQuery.Rows)
{
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        CheckBox ch = gvr.FindControl("chkActive") as CheckBox;
        DropDownList dd = gvr.FindControl("ddlCondition") as DropDownList;
        TextBox t1 = gvr.FindControl("txtField1") as TextBox;
        TextBox t2 = gvr.FindControl("txtField2") as TextBox;
        HiddenField hf = gvr.FindControl("hfFieldName") as HiddenField;

        if (ch.Checked)
        {
            SearchResultField srf = new SearchResultField();
            Field field = fields.Find(x => x.Name == hf.Value);

            srf.Name = field.Name;
            srf.Operator = dd.SelectedValue;
            srf.Owner = field.WhereOwner;
            srf.Param1 = t1.Text;
            srf.Param2 = t2.Text;
            srf.Type = field.FieldType;

            sr.Fields.Add(srf);
        }
    }
}

问题CheckBox总是 Checked = false 即使我检查它!

我需要做什么才能获得帖子值?似乎在单击后我完全松开了网格中所做的任何事情,我只是得到了空控件。

在我的aspx页面直接我有:

<%@ Page 
     Title="" 
     Language="C#" 
     MasterPageFile="~/3Rows.master" 
     AutoEventWireup="true"
     ValidateRequest="false" 
     CodeFile="Default.aspx.cs" 
     Inherits="_Default" %>

我确实有具有这种行为的项目,但我无法理解为什么我在这个简单的页面中有这个......

有人有线索吗?

谢谢你。

4

1 回答 1

2

孩子的错误...

protected void Page_Load(object sender, EventArgs e)
{
    PopulateData();
}

反而

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        PopulateData();
}
于 2009-10-02T12:28:41.620 回答