How to programmatically reach any AspxControl inside an AspXGridView's EditItemTemplate

DortGen picture DortGen · Nov 25, 2011 · Viewed 10.6k times · Source

Its very simple and i feel myself as an idiot :(

I newly started to using DevX Controls. Its documentation and sample projects are SUCKS!

My problem is:

I have an ASPxGridView on my aspx page:

<dx:ASPxGridView ID="dxdgMyGrid" runat="server" AutoGenerateColumns="False" OnStartRowEditing="DxGridStartRowEditing">
<SettingsEditing Mode="PopupEditForm" PopupEditFormHeight="200px" PopupEditFormWidth="500px"
    EditFormColumnCount="2" PopupEditFormHorizontalAlign="Center" PopupEditFormVerticalAlign="Middle"
    PopupEditFormModal="true" />
<Columns>
    <dx:GridViewDataTextColumn FieldName="MyField1" VisibleIndex="1">
        <EditFormSettings VisibleIndex="0" />
        <EditItemTemplate>
            <dx:ASPxDateEdit ID="dxdateMyField1" runat="server">
            </dx:ASPxDateEdit>
        </EditItemTemplate>
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataColumn FieldName="MyField2" VisibleIndex="4">
        <EditFormSettings VisibleIndex="1" />
        <EditItemTemplate>
            <dx:ASPxComboBox ID="dxcomboMyField2" runat="server">
            </dx:ASPxComboBox>
        </EditItemTemplate>
    </dx:GridViewDataColumn>
</Columns>

How can i reach dxdateMyField1 or dxcomboMyfield2 on ASPX.CS file? I want to write:

dxcomboMyField2.DataSource = GetMyData2List();
dxcomboMyField2.SelectedItemIndex = 0;
... etc.

Thanks a lot.

Answer

Niranjan Singh picture Niranjan Singh · Nov 25, 2011

You cannot access the EditItemTemplate Control Directly. You can access them at the HtmlRowCreated event as:

if (e.RowType != GridViewRowType.InlineEdit) return;
    ASPxTextBox txtBox = ASPxGridView1.FindEditRowCellTemplateControl(ASPxGridView1.Columns["Name"]
            as GridViewDataColumn, "ASPxTextBox1") as ASPxTextBox;

Check the documentation on Accessing Controls Contained within Templates

It is possible to cast the ASPxLabel.NamingContainer property to GridViewEditItemTemplateContainer and get a column value via the GridViewEditItemTemplateContainer.Text property.

But I like the technique of using the Init/Load event handler.When the grid switches to edit mode, the ASPxLabel.Load event is raised. Check this article The general technique of using the Init/Load event handler for implementation help.

[ASPx]

<dxe:ASPxTextBox ID="txtName" runat="server" Width="170px" OnInit="txtName_Init">

</dxe:ASPxTextBox>

[C#]

ASPxTextBox txtName;    

protected void txtName_Init(object sender, EventArgs e)    
{    
    txtName = (ASPxTextBox)sender;

    GridViewEditFormTemplateContainer container = txtName.NamingContainer as GridViewEditFormTemplateContainer;
// You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference

    if (!container.Grid.IsNewRowEditing)

        txtName.Text = DataBinder.Eval(container.DataItem, "CategoryName").ToString();    
} 

Update Event:

protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
    e.NewValues["CategoryName"] = txtName.Text;
} 

There is already an question - ASPxGridView - How to find a control inside the EditItemTemplate on DevExpress fourm .