如何在服务器端访问 DetailRow 的 GridViewDataColumn

How to Access GridViewDataColumn of DetailRow on server side

我正在使用 ASPxGridView,但无法访问 DetailRow 中的文本框(即在第二个网格视图中,而不是在主网格视图中)

谁能帮帮我

ASPX

 <Templates>
   <DetailRow>
     <dx:ASPxGridView ID="dgCustomers" runat="server" KeyFieldName="CustomerCode"                  OnBeforePerformDataSelect="dgCustomers_BeforePerformDataSelect" OnDataBinding="dgCustomers_DataBinding" Width="100%" OnRowDeleting="dgCustomers_RowDeleting" OnRowInserting="dgCustomers_RowInserting"
         OnRowUpdating="dgCustomers_RowUpdating">
        <Columns>
          <dx:GridViewDataColumn FieldName="CustomerCode" VisibleIndex="0">
            <EditItemTemplate>
              <asp:TextBox ID="txtCustCode" runat="server" Width="50px" />
            </EditItemTemplate>
          </dx:GridViewDataColumn>
          <dx:GridViewDataColumn>
            <EditItemTemplate>
               <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
            </EditItemTemplate>
          <dx:GridViewCommandColumn ShowNewButton="true" ShowEditButton="true" ShowDeleteButton="true" VisibleIndex="4">
          </dx:GridViewCommandColumn>
     </Columns>

代码隐藏

我试过这样的事情:

 ASPxGridView detail = dgDepots.FindDetailRowTemplateControl(0, "dgCustomers") as ASPxGridView;
        if (detail != null)
        {
            for (int i = 0; i < detail.VisibleRowCount; i++)
            {
                var item = detail.FindDetailRowTemplateControl(0, "txtCustCode");
                TextBox textbox = detail.FindRowCellTemplateControl(i, detail.Columns["CustomerCode"] as GridViewDataColumn, "txtCustCode") as TextBox;
                var anything = textbox.Text;
                if (CustomerCodeArray.Contains(anything))
                {
                   //Something
                }
            }
        }

但是在调试时我可以看到 textbox 显示 null

我哪里做错了请帮助我

好吧,我这样做了。

首先我有一个扩展方法来访问控制 y 单元格模板。

public static object GetDataItemControl(this ASPxGridView _grid, int _visibleIndex, string _fieldName, string _controlID)
{
    return _grid.FindRowCellTemplateControl(_visibleIndex, _grid.Columns[_fieldName] as GridViewDataColumn, _controlID);
}

接下来,你应该使用按钮的属性 NamingContainer来访问位于detail row的gridview,通过这个和function你可以访问位于的textbox在模板中。例如:

我用的是devexpressbootstrap但是一样。

视图,您需要输入列名才能访问它。

<dx:BootstrapGridView ID="gv" runat="server" AutoGenerateColumns="False" DataSourceID="DataSource" KeyFieldName="KeyField">
    <SettingsDataSecurity AllowEdit="True" /><SettingsBehavior AllowFocusedRow="true" />
    <Toolbars>
        <dx:BootstrapGridViewToolbar>
            <Items>
                <dx:BootstrapGridViewToolbarItem Command="Edit" />
                <dx:BootstrapGridViewToolbarItem Command="Refresh" />
            </Items>
        </dx:BootstrapGridViewToolbar>
    </Toolbars>
    <Columns>
        <dx:BootstrapGridViewDataColumn Name="CustomCode">
            <DataItemTemplate>
                <asp:TextBox ID="txtCustCode" runat="server" Width="50px" />
            </DataItemTemplate>
        </dx:BootstrapGridViewDataColumn>
        <dx:BootstrapGridViewDataColumn>
            <DataItemTemplate>
                <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
            </DataItemTemplate>
        </dx:BootstrapGridViewDataColumn>
    </Columns>
</dx:BootstrapGridView>

后面的代码,我们使用属性 NamingContainer访问gridview,然后我们使用扩展方法访问模板中的控件。

protected void btnSearch_Click(object sender, EventArgs e)
{
    Button _button = sender as Button;

    BootstrapGridView _grid = _button.NamingContainer.NamingContainer.NamingContainer as BootstrapGridView;

    TextBox _textBox = _grid.GetDataItemControl(_grid.FocusedRowIndex, "CustomCode", "txtCustCode") as TextBox;

    String _text = _textBox.Text;
}

我使用此代码,它对我有用,如果您有任何问题,请在评论中提出。