问题
I am trying to access my combobox in my codebehind file C# but I keep getting an error that says 'cmbYearOfStudy' does not exist in the current context
This is the C# code that is giving the error name of file => 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"]);
}
This is the aspx code name of file => 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>
回答1:
The 'cmbYearOfStudy' control is hold by the DataItemTemplate and is created for each row, and should be accessible respectively. It is possible to use the The general technique of using the Init/Load event handler approach to access the required control directly:
<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:
You have grvEnrollmentDetails GridView in your page. The ComboBox cmbYearOfStudy is only inside GridView, respectively in each row of the table except the header and footer. If could binded data to ComboBox, you must do it in event of creating table row.
Now, some changes in your code.
In aspx code file BulkScheduleAllocation.aspx:
<dx:ASPxGridView ID="grvEnrollmentDetails" runat="server" KeyFieldName="ModuleEnrollmentId" SettingsPager-Mode="ShowAllRecords" OnRowCreated="grvEnrollmentDetails_OnRowCreated">
In codebehind file 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 exists is only available within a data row of GridView.
来源:https://stackoverflow.com/questions/64822898/access-aspxcombobox-from-dataitemtemplate-in-current-context