Back to Devexpress

Accessing Controls Contained within Templates

aspnet-3922-components-grid-view-concepts-templates-accessing-controls-contained-within-templates.md

latest4.5 KB
Original Source

Accessing Controls Contained within Templates

  • Nov 08, 2021
  • 2 minutes to read

ASPxGridView has multiple methods that allow you to access controls contained within templates. These methods are listed in the following topic: Member Table: Templates.

Example 1: Data Item Template

In this example, the ASPxGridView.FindRowCellTemplateControl method is used to access ASPxButtonEdit template controls contained within column cells. If the processed customer is older than 30, the ASPxButtonEdit control is disabled.

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" OnHtmlRowPrepared="ASPxGridView1_HtmlRowPrepared" DataSourceID="SqlDataSource1" ...>
    <Columns>
        <dx:GridViewDataTextColumn FieldName="Oid" VisibleIndex="0">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="1">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="Age" VisibleIndex="2">
        </dx:GridViewDataTextColumn>
    </Columns>
    ...
</dx:ASPxGridView>
csharp
protected void ASPxGridView1_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e) {
    ASPxButtonEdit btnEdit = ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex,
        ASPxGridView1.Columns["Name"] as GridViewDataColumn, "ASPxButtonEdit1") as ASPxButtonEdit;
    if (Convert.ToInt32(ASPxGridView1.GetRowValues(e.VisibleIndex, "Age")) > 30)
        btnEdit.Enabled = false;
}
vb
Protected Sub ASPxGridView1_HtmlRowPrepared(ByVal sender As Object, ByVal e As ASPxGridViewTableRowEventArgs)
    Dim btnEdit As ASPxButtonEdit = TryCast(ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, TryCast(ASPxGridView1.Columns("Name"), GridViewDataColumn), "ASPxButtonEdit1"), ASPxButtonEdit)
    If Convert.ToInt32(ASPxGridView1.GetRowValues(e.VisibleIndex, "Age")) > 30 Then
        btnEdit.Enabled = False
    End If
End Sub

Example 2: Edit Item Template

In this example, the ASPxGridView.FindEditRowCellTemplateControl method is used to access text box template controls contained within the Name column’s edit cells. If the processed customer is older than 30, the text box is disabled.

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" OnHtmlRowCreated="ASPxGridView1_HtmlRowCreated" DataSourceID="SqlDataSource1" ...>
    <Columns>
        <dx:GridViewDataTextColumn FieldName="Oid" VisibleIndex="0">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="1">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="Age" VisibleIndex="2">
        </dx:GridViewDataTextColumn>
    </Columns>
    ...
</dx:ASPxGridView>
csharp
protected void ASPxGridView1_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e) {
    if (e.RowType != GridViewRowType.InlineEdit) return;
    ASPxTextBox txtBox = ASPxGridView1.FindEditRowCellTemplateControl(ASPxGridView1.Columns["Name"] as GridViewDataColumn, "ASPxTextBox1") as ASPxTextBox;
    if (txtBox == null) return;
    if (Convert.ToInt32(ASPxGridView1.GetRowValues(e.VisibleIndex, "Age")) > 30)
        txtBox.Enabled = false;
}
vb
Protected Sub ASPxGridView1_HtmlRowCreated(ByVal sender As Object, ByVal e As ASPxGridViewTableRowEventArgs)
     If e.RowType <> GridViewRowType.InlineEdit Then
          Return
     End If
     Dim txtBox As ASPxTextBox = TryCast(ASPxGridView1.FindEditRowCellTemplateControl(TryCast(ASPxGridView1.Columns("Name"), GridViewDataColumn), "ASPxTextBox1"), ASPxTextBox)
     If txtBox Is Nothing Then
          Return
     End If
     If Convert.ToInt32(ASPxGridView1.GetRowValues(e.VisibleIndex, "Age")) > 30 Then
          txtBox.Enabled = False
     End If
End Sub