有没有办法在按下“新建”按钮时调用某些服务器端代码?

时间:2013-08-30 14:05:11

标签: c# javascript asp.net devexpress aspxgridview

点击"edit"上的ASPxGridView按钮后,会触发 StartRowEditing事件。发生这种情况时,我设置了一个名为"IsEditing"的局部变量,这样当 CellEditorIntialize 触发时,我可以为comboboxes设置数据源。我这样做是因为如果用户按下取消,则CellEditorInitialize事件再次触发,因为combobox不可用,我会得到一个空参考问题。

我需要对"new"按钮执行相同操作,但没有"StartSrowInserting"事件。

有什么想法吗?

这是C#中的服务器代码。

这是StartRowEditingEvent:

protected void gvLocation_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
    {
        this.IsEditing = true; //There's not editing property in this event, so everytime it fires, we'll set this flag to true
        if (e.Cancel) //And we'll only set the flag to false when canceling
            this.IsEditing = false;
    }

当我单击ASPxGridView上的“编辑”按钮时,此事件将触发。它会将该变量设置为true。如果用户取消对话框,则会将其设置为false。

然后,当控件初始化时,将触发CellEditorInitialize事件。

protected void gvLocation_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        e.Column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;

        if (this.IsEditing) //Only populate fields when editing
        {
            if (e.Column.FieldName == "LocationPK")
                e.Editor.Visible = false; //We don't want LocationPK to be updated
            else if (e.Column.FieldName == "ShalePlay")
            {
                ASPxComboBox combo = (ASPxComboBox)e.Editor;
                mcCommon.Setup(ref combo, true, LookupValuesShalePlay.List, "Description", "PK");
            }
            else if (e.Column.FieldName == "FieldType")
            {
                ASPxComboBox combo = (ASPxComboBox)e.Editor;
                mcCommon.Setup(ref combo, true, LookupValuesFieldType.List, "Description", "PK");
            }
            else if (e.Column.FieldName == "County")
            {
                ASPxComboBox combo = (ASPxComboBox)e.Editor;
                mcCommon.Setup(ref combo, true, LookupValuesCounty.List, "Description", "PK");
            }
            else if (e.Column.FieldName == "State")
            {
                ASPxComboBox combo = (ASPxComboBox)e.Editor;
                mcCommon.Setup(ref combo, true, LookupValuesState.List, "Description", "PK");
            }
            else
            {

            }
        }
    }

我确定哪个控件是哪个,并分配数据源。如果我不使用该标志,当用户取消编辑时,它将抛出一个空异常。当用户在插入时单击“新建”或“取消”时,也会触发此事件。但是,在CellEditorInitiliaze触发之前,单击“新建”按钮时,我发现没有任何事件会触发。

所以我需要一种方法来在用户点击“新”和“取消”按钮时设置该标志,这样我就可以设置标志了。

这是ASPxGridView的标记。

<dx:ASPxGridView
ID="gvLocation"
runat="server"
AutoGenerateColumns="False"
DataSourceID="edsLocations"
ClientInstanceName="gvLocation"
ViewStateMode="Disabled"
KeyFieldName="LocationPK"
Width="600px"
OnCellEditorInitialize="gvLocation_CellEditorInitialize"
OnCommandButtonInitialize="gvLocation_CommandButtonInitialize"
OnStartRowEditing="gvLocation_StartRowEditing"
>
    <ClientSideEvents BeginCallback="
                    function(s, e) {loadingPanel.Show();}"
EndCallback="
                    function(s, e) {loadingPanel.Hide();}" />
<Columns>
    <dx:GridViewDataHyperLinkColumn FieldName="LocationPK" ReadOnly="True" VisibleIndex="0" Visible="false">
        <PropertiesHyperLinkEdit TextField="LocationPK" />
    </dx:GridViewDataHyperLinkColumn>
    <dx:GridViewDataTextColumn FieldName="LocationName" VisibleIndex="1">
         <DataItemTemplate>
            <a href="javascript:void(0);" onclick="gvLocation_LinkClick('<%# Container.VisibleIndex %>');"><%# DataBinder.Eval(Container.DataItem,"LocationName") %></a>
        </DataItemTemplate>
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataComboBoxColumn FieldName="FieldType" VisibleIndex="2">
        <PropertiesComboBox TextField="FieldType" ValueField="FieldType" />
    </dx:GridViewDataComboBoxColumn>
    <dx:GridViewDataComboBoxColumn FieldName="State" VisibleIndex="4">
        <PropertiesComboBox ValueField="State" TextField="State" />
    </dx:GridViewDataComboBoxColumn>
    <dx:GridViewDataComboBoxColumn FieldName="CountyName" VisibleIndex="3">
        <PropertiesComboBox ValueField="CountyName" TextField="CountyName" ValueType="System.String" DataSourceID="edsCounty" />
    </dx:GridViewDataComboBoxColumn>
    <dx:GridViewDataComboBoxColumn FieldName="ShalePlay" VisibleIndex="5">
        <PropertiesComboBox ValueField="ShalePlay" TextField="ShalePlay" />
    </dx:GridViewDataComboBoxColumn>
    <dx:GridViewCommandColumn VisibleIndex="6">
        <EditButton Visible="True">
        </EditButton>
        <NewButton Visible="True">
        </NewButton>
    </dx:GridViewCommandColumn>
</Columns>
<Settings ShowFilterBar="Visible" ShowFilterRow="True" ShowGroupPanel="True" />
<SettingsBehavior AllowFocusedRow="True" />
<SettingsLoadingPanel Mode="Disabled" />

1 个答案:

答案 0 :(得分:2)

您可以使用ASPxGridView.InitNewRow来捕获启动新插入的时间。然后,您可以使用ASPxGridView.CancelRowEditing来捕获取消。

http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_InitNewRowtopic

http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_CancelRowEditingtopic