如何以编程方式访问AspXGridView的EditItemTemplate中的任何AspxControl

时间:2011-11-25 08:15:10

标签: asp.net devexpress aspxgridview edititemtemplate

非常简单,我觉得自己是个白痴:(

我刚开始使用DevX Controls。它的文档和示例项目是SUCKS!

我的问题是:

我的aspx页面上有一个ASPxGridView:

<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>

如何在ASPX.CS文件上访问dxdateMyField1或dxcomboMyfield2?我想写:

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

非常感谢。

2 个答案:

答案 0 :(得分:3)

您无法直接访问EditItemTemplate控件。您可以在HtmlRowCreated事件中访问它们:

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

检查访问模板中包含的控件的documentation

可以将ASPxLabel.NamingContainer属性强制转换为GridViewEditItemTemplateContainer,并通过GridViewEditItemTemplateContainer.Text属性获取列值。

但我喜欢technique of using the Init/Load event handler.当网格切换到编辑模式时,会引发ASPxLabel.Load事件。请查看本文The general technique of using the Init/Load event handler以获取实施帮助。

[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();    
} 

更新活动:

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

在DevExpress上已经存在一个问题 - ASPxGridView - How to find a control inside the EditItemTemplate

答案 1 :(得分:1)

您可以使用组合框init / load事件处理程序来设置组合数据源。如果这对您不起作用,请使用FindEditRowCellTemplateControl(使用注释中的链接进行进一步说明)。