views:

94

answers:

1

Hi guys,

I tried to search for this question and plenty results come up, but not exactly what I'm getting, so here it goes:

I have a simple GridView control and I want to access the value of the child controls once submited

I'm doing this:

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

and then, in the btnSearch_Click event I have the normal loop

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);
        }
    }
}

Problem is that the CheckBox is always Checked = false even if I check it!

What do I need to do, to get the post values? it seams that after clicking I completely loose anything done in the grid, I'm just getting empty controls.

in my aspx page direct I have:

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

I do have projects with this behavior working but I can't seam to understand why do I have this one here in this simple page....

Anyone have a clue?

Thank you.

+1  A: 

child mistake ...

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

instead

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        PopulateData();
}
balexandre