2009-10-02 50 views
0

的访问ChildControl值我试图寻找这个问题和大量的结果出来了,但不完全是我得到,所以这里有云:一个GridView

我有一个简单的GridView控件,我想访问一次submited

子控件的值,我这样做:

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

问题是,复选框总是经过即使我检查=假的!

我需要做些什么来获取帖子值?它会在点击完成后断开网格中的任何东西,我只是得到空的控件。

在我的aspx页面直接I有:

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

我有个项目,这种行为的工作,但我不能缝明白为什么我有这样一个在这里这个简单的页面.. ..

任何人都有线索?

谢谢。

回答

1

孩子错误...

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

代替

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