从gridview的TemplateFields中动态创建的控件中获取数据

本文关键字:控件 获取 数据 创建 动态 TemplateFields gridview | 更新日期: 2023-09-27 18:17:45

我有一个网页,有一个gridview绑定到自定义对象的列表,但也有1动态创建的TemplateFields。这些字段是在Page_PreRender中创建的,可以是基于列表中对象的文本框或下拉列表。我在页面底部有一个按钮,需要保存按下时在动态对象中输入的所有数据。当我试图找到动态控件时,我无法使用FindControl()方法这样做。它总是返回空白。

如何检索用户输入/选择的数据?

这是gridview

<div id="divSearchCriteriaGrid" runat="server" class="padding-top-15">
                    <asp:GridView ID="gvSearchCriteria" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvSearchCriteria_OnRowDataBound" GridLines="None">
                        <Columns>
                            <asp:BoundField DataField="SearchFieldId" Visible="False" />
                            <asp:TemplateField HeaderText="Search Field">
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbDisplay" runat="server" AutoPostBack="False" onclick="ToggleCriteriaControls(this)" />
                                </ItemTemplate>
                                <ItemStyle Width="25%" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Begin Criteria">
                                <ItemStyle Width="35%" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="End Criteria">
                                <ItemStyle Width="35%" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="ControlTypeId" Visible="False" />
                        </Columns>
                    </asp:GridView>
                </div>

下面是我创建控件的代码:

public void Page_PreRender(object sender, EventArgs e)
    {
        TryAction(PrepareLoad);
    }
private void PrepareLoad()
    {
        if (IsPostBack) return;
        BindData(); 
    }
private void BindData()
    {
        gvSearchCriteria.DataSource = null;
        gvSearchCriteria.DataSource = SearchFieldList;
        gvSearchCriteria.DataBind();
    } 
protected void gvSearchCriteria_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SearchField searchField = e.Row.DataItem as SearchField;
            if (searchField != null)
            {
                e.Row.Cells[0].Text = searchField.SearchFieldId.ToString();
                e.Row.Cells[4].Text = searchField.ControlTypeId.ToString();
                CheckBox checkBox = (CheckBox)e.Row.FindControl("cbDisplay");
                checkBox.Text = searchField.FieldDescription;
                checkBox.Checked = searchField.Checked;
                checkBox.Enabled = true;
                if (searchField.ControlTypeId.ToString() == "1")
                {
                    RadTextBox textBox = new RadTextBox();
                    textBox.ID = "txtBoxBegin";
                    e.Row.Cells[2].Controls.Add(textBox);
                    textBox = new RadTextBox();
                    textBox.ID = string.Format("txt{0}End", searchField.SearchFieldId);
                    e.Row.Cells[3].Controls.Add(textBox);
                }
                else if (searchField.ControlTypeId.ToString() == "2")
                {
                    RadComboBox comboBox = new RadComboBox();
                    comboBox = new SearchService().GetRadComboBoxById(searchField.SearchFieldId);
                    comboBox.ID = string.Format("cbo{0}Begin", searchField.SearchFieldId);
                    e.Row.Cells[2].Controls.Add(comboBox);
                }
            }
        }
    }

这一切都很好,控件被创建与复选框。这是我的代码,试图通过gridview的每一行循环,以获得用户输入的数据,这是不工作的。

private void Save()
    {
       foreach (GridViewRow row in gvSearchCriteria.Rows)
       {
           CheckBox include = (CheckBox)row.FindControl("cbDisplay");
           int id, controlTypeId;
           string criteriaOne = string.Empty;
           string criteriaTwo = string.Empty;
           if (!include.Checked) continue;
           id = int.Parse(row.Cells[0].Text);
           if (controlTypeId == "1")
           {
               RadTextBox radTextBox = (RadTextBox) row.FindControl("txtBoxBegin");
               if (radTextBox != null)
               {
                   criteriaOne = radTextBox.Text;
               }
               radTextBox = (RadTextBox)row.FindControl("txtBoxEnd");
               if (radTextBox != null)
               {
                   criteriaTwo = radTextBox.Text;
               }
           }
           else if(controlTypeId == "2")
           {
               RadComboBox radComboBox = (RadComboBox)row.FindControl(string.Format("cbo{0}Begin",id));
               if (radComboBox != null)
               {
                   criteriaOne = radComboBox.SelectedValue;
               }
           }
       } 
    }

radTextBox和radComboBox变量我试图使用FindControlId总是返回null。单元格0每次都返回正确的Id。cbDisplay复选框总是返回行是否被选中,单元格4获得ControlTypeId很好。它是TemplateFields,我不能得到的值。

从gridview的TemplateFields中动态创建的控件中获取数据

我能够从动态控件中获得信息,一旦我将BindData()函数移动到Page_Init而不是Page_PreRender()。