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

假装没事ソ 提交于 2019-12-12 15:18:33

问题


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.


回答1:


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 .




回答2:


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



来源:https://stackoverflow.com/questions/8266282/how-to-programmatically-reach-any-aspxcontrol-inside-an-aspxgridviews-edititemt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!