【问题标题】:Access ASPxComboBox from DataItemTemplate in current context在当前上下文中从 DataItemTemplate 访问 ASPxComboBox
【发布时间】:2021-02-25 13:53:08
【问题描述】:

我试图在我的代码隐藏文件 C# 中访问我的组合框,但我不断收到一条错误消息,提示“cmbYearOfStudy”在当前上下文中不存在

这是给出错误的 C# 代码 文件名 => BulkScheduleAllocation.aspx.cs

private void PopulateScheduleAcademicYearCombo()
    {
        for (var iYear = DateTime.Now.Year + 5; iYear >= 1999; iYear--)
        {
            var oItem = new ListEditItem(iYear.ToString(), iYear.ToString());
            cmbYearoOfStudy.Items.Add(oItem);
        }
        cmbYearOfStudy.DataBind();

        Utils.PopulateCombobox(ref cmbYearOfStudy, Request.QueryString["acyr"]);
    }

这是aspx代码 文件名 => BulkScheduleAllocation.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BulkScheduleAllocation.aspx.cs" Inherits="Student_Popup_BulkScheduleAllocation" %>

                <dx:LayoutItem Width="40%" VerticalAlign="Top" RowSpan="3" ShowCaption="False" CaptionSettings-Location="Top" CaptionSettings-HorizontalAlign="Left" CssClass="labelfont">
                    <LayoutItemNestedControlCollection>
                        <dx:LayoutItemNestedControlContainer>
                             <dx:ASPxGridView ID="grvEnrollmentDetails" runat="server" KeyFieldName="ModuleEnrollmentId" SettingsPager-Mode="ShowAllRecords">
                                <Columns>
                                    <dx:GridViewDataColumn FieldName="Academicyear" VisibleIndex="0" Caption="Year" />
                                    <dx:GridViewDataColumn FieldName="ModuleName" VisibleIndex="1" Caption="Module Name" />
                                    <dx:GridViewDataColumn FieldName="ScheduleName" VisibleIndex="2" Caption="Schedule Name" />
                                    <dx:GridViewDataColumn FieldName="ScheduleGroupName" VisibleIndex="3" Caption="Group Name" />
                                    <dx:GridViewDataColumn FieldName="Result" VisibleIndex="4" Caption="Status" />
                                    <dx:GridViewDataColumn FieldName="Year" VisibleIndex="5" Caption="Status" >
                                            <DataItemTemplate>
                                                <dx:ASPxComboBox ID="cmbYearOfStudy" NullText="Please select" runat="server" Width="250" AutoPostBack="true" OnSelectedIndexChanged="cmbProposedQualification_SelectedIndexChanged"></dx:ASPxComboBox>
                                            </DataItemTemplate>
                                        </dx:GridViewDataColumn>
                                        <dx:GridViewDataColumn FieldName="Schedule" VisibleIndex="6" Caption="Accept">
                                            <DataItemTemplate>
                                                <dx:ASPxComboBox ID="cmbChangeSchedule" runat="server" DataSourceID="objStatuses" Enabled='<%# (((int)Eval("marker_id")==0 && (int)Eval("id")>0)?true:false) %>' ValueField="ID" TextField="Status_Name"></dx:ASPxComboBox>
                                            </DataItemTemplate>
                                        </dx:GridViewDataColumn>
                                </Columns>
                            </dx:ASPxGridView>
                        </dx:LayoutItemNestedControlContainer>
                    </LayoutItemNestedControlCollection>
                    <CaptionSettings HorizontalAlign="Left" Location="Top" />
                </dx:LayoutItem>

【问题讨论】:

    标签: c# asp.net data-binding combobox devexpress


    【解决方案1】:

    “cmbYearOfStudy”控件由 DataItemTemplate 保存并为每一行创建,并且应该可以分别访问。可以使用The general technique of using the Init/Load event handler 方法直接访问所需的控件:

    <dx:GridViewDataColumn ...>
        <DataItemTemplate>
            <dx:ASPxComboBox ID="cmbYearOfStudy" ... OnInit="cmbYearOfStudy_Init"></dx:ASPxComboBox>
        </DataItemTemplate>
    </dx:GridViewDataColumn>
    
    protected void cmbYearOfStudy_Init(object sender, EventArgs e) {
        ASPxComboBox cmbYearOfStudy = (ASPxComboBox)sender;
    
        for (var iYear = DateTime. Now.Year + 5; iYear >= 1999; iYear--) {
            var oItem = new ListEditItem(iYear.ToString(), iYear.ToString());
            cmbYearOfStudy.Items.Add(oItem);
        }
        //cmbYearOfStudy.DataBind(); //not required when adding .Items manually
    
        Utils.PopulateCombobox(ref cmbYearOfStudy, Request.QueryString["acyr"]);
    }
    

    【讨论】:

      【解决方案2】:

      您的页面中有 grvEnrollmentDetails GridView。 ComboBox cmbYearOfStudy 只在 GridView 内部,分别在表的每一行中,除了页眉和页脚。 如果可以将数据绑定到 ComboBox,则必须在创建表行时进行。

      现在,对您的代码进行一些更改。

      在 aspx 代码文件 BulkScheduleAllocation.aspx 中:

      <dx:ASPxGridView ID="grvEnrollmentDetails" runat="server" KeyFieldName="ModuleEnrollmentId" SettingsPager-Mode="ShowAllRecords" OnRowCreated="grvEnrollmentDetails_OnRowCreated">
      

      在代码隐藏文件 BulkScheduleAllocation.aspx.cs 中:

      protected void grvEnrollmentDetails_OnRowCreated(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              ComboBox cmbYearoOfStudy = (ComboBox)e.Row.FindControl("cmbYearoOfStudy");
              for (var iYear = DateTime.Now.Year + 5; iYear >= 1999; iYear--)
              {
                  var oItem = new ListEditItem(iYear.ToString(), iYear.ToString());
                  cmbYearoOfStudy.Items.Add(oItem);
              }
          }
      }
      

      Combobox 存在仅在 GridView 的数据行中可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 2014-11-14
        • 2012-05-15
        • 1970-01-01
        相关资源
        最近更新 更多