3

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.

DortGen
  • 402
  • 2
  • 8
  • 22
  • 1
    Would this help? http://documentation.devexpress.com/#AspNet/CustomDocument3922 – Wouter de Kort Nov 25 '11 at 08:18
  • @DortGen, controls from template are not easily accessible because template get instantiated at some later point and there can be more than one instantiation (e.g. repeater's item template). Only way is to use `FindControl` method on the relevant container when you are sure about template creation (e.g. in this edit row creation). Regardless, check link by Wouter de Kort - that lists the event to be used and method to be used to find the control. – VinayC Nov 25 '11 at 08:32
  • @DortGen: Why do you dislike these resources? Have you ever tried to contact DX guys regarding this? – Mikhail Nov 28 '11 at 21:03

2 Answers2

3

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 .

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • Hello Mr Niranjan, I'm trying to use your snippet. Except, i'm using a AspXDropDown control whereby your sample codes ASPxTextBox control and it's working well when editing mode. But when i click Update button the works confusing: Debugger enters OnInit method of DropDown again and in the RowUpdating(or in my case RowInserting) methods ASPxDataUpdatingEventArgs's NewValues collection there is no any piece of my EditTemplate control :( Even in the second run of OnInit method there is no info about DropDown's own.. It nulled again.. What is my mistake? – DortGen Nov 26 '11 at 15:02
  • have you seen the general technique init/load page.. there is example with combo box.. check those example .. there example2.. If raise exception then add dropdownlist in edit template and bind its data at `HTMLRowCreated` event. – Niranjan Singh Nov 26 '11 at 16:26
1

You can use combo box init/load event handler for setting combo datasource. If that doesn't work for you, use FindEditRowCellTemplateControl (use link in comments for further explanation).

Filip
  • 3,257
  • 2
  • 22
  • 38