2009-10-02 53 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(); 
}