CS0103:名称' GetSelectedIndex'在当前上下文中不存在

时间:2012-08-30 16:16:50

标签: asp.net gridview objectdatasource html-select edititemtemplate

我很担心我将SelectedIndex对象的<asp:DropDownList>属性绑定到自定义类(位于我的{中)的方法时遇到的问题{1}}文件夹)。

app_codeObjectDataSource对象的文本 - 使用相同的GridView作为其数据源 - 可以在下面看到。似乎我在自定义类中显示的ObjectDataSource方法(进一步向下显示) - 并尝试绑定到我的GetSelectedIndex对象的SelectIndex属性 - 可以&# 39;找不到:

GridView

我尝试绑定的自定义类包含以下内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CarCrash.aspx.cs" Inherits="CarCrash" %>
....
<asp:ObjectDataSource ID="DataBindingODS" runat="server" DataObjectTypeName="EmployeeDetails" 
    TypeName="EmployeeFunctionsClass"
    SelectMethod="GetEmployees" 
    InsertMethod="InsertEmployeeAlternative" 
    UpdateMethod="UpdateEmployeeAlternative"
    EnablePaging="True"
    SelectCountMethod="CountEmployeesAlternative"
    MaximumRowsParameterName="maxRows" 
    OldValuesParameterFormatString="original_{0}"/>
    <asp:GridView ID="GridView1" runat="server" CssClass="style1" PageSize="5"
        DataSourceID="DataBindingODS" AutoGenerateColumns="False">
        <Columns>
        <asp:TemplateField HeaderText="Employee Details">            
            <EditItemTemplate>
                <b>
                    <%# Eval("EmployeeID") %>
                    <asp:DropDownList ID="EditTitle" runat="server" SelectedIndex='<%# GetSelectedTitle(DataBinder.Eval("TitleOfCourtesy")) %>' DataSource='<%# "TitlesOfCourtesy" %>' />
                    <%# Eval("FirstName") %>
                    <%# Eval("LastName") %>
                </b>
                <hr />
                <small><i>
                <%# Eval("Address") %><br />
                <%# Eval("City") %>, <%# Eval("Country") %>,
                <%# Eval("PostalCode") %><br />
                <%# Eval("HomePhone") %>
                </i>
                <br /><br />
                <asp:TextBox Text='<%# Bind("Notes") %>' runat="server" ID="textBox"   TextMode="MultiLine" Width="413px" />
                </small>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Background">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

它使用的类对象是EmployeeDetails类,定义如下:

public class EmployeeFunctionsClass
{

public string connStr { get; set; }

public EmployeeFunctionsClass()
{
    connStr = WebConfigurationManager.ConnectionStrings["EmployeeConnection"].ConnectionString;
}

public int employeeID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string titleOfCourtesy { get; set; }

......
public List<EmployeeDetails> GetEmployeesAlternative()
{
    string strGetEmp = "SELECT EmployeeID, FirstName, LastName, TitleOfCourtesy, BirthDate, City from Employees";

    SqlConnection conn = new SqlConnection(connStr);
    SqlCommand cmd = new SqlCommand(strGetEmp, conn);
    cmd.CommandType = CommandType.Text;

    List<EmployeeDetails> employees = new List<EmployeeDetails>();

    try
    {
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            string city;
            DateTime birthDate;

            if (reader["City"] is System.DBNull)
            { city = ""; }
            else
            { city = (string)reader["City"]; }
            if (reader["BirthDate"] != System.DBNull.Value)
            { birthDate = (DateTime)reader["BirthDate"]; }
            else
            { birthDate = DateTime.MinValue; }



            EmployeeDetails emp = new EmployeeDetails(
            (int)reader["EmployeeID"],
            (string)reader["FirstName"],
            (string)reader["LastName"],
            (string)reader["TitleOfCourtesy"], birthDate,
            city, "", "", "", ""

            );

            employees.Add(emp);


        }
        reader.Close();

        employees.Sort(new Comparison<EmployeeDetails>((x, y) => String.Compare(x.LastName, y.LastName)));

        return employees;

    }

    catch (SqlException err)
    {
        throw new ApplicationException("Data error.");
    }
    finally
    {
        conn.Close();
    }
}


public static string[] TitlesOfCourtesy   //********** This is bound to DataSource property of DropDownList
{
    get { return new string[] { "Mr.", "Mrs.", "Dr.", "Ms.", "Mrs." }; }
}


public static int GetSelectedTitle(object title)  //****** This is bound to SelectedIndex property of DropDownList - and is giving the error at the head of this post.
{
    return Array.IndexOf(TitlesOfCourtesy, title.ToString());
}



....
}

我担心我真的很难过。我已经搜索了很多不同的帮助论坛,但我们并没有真正遇到任何描述/诊断我确切问题的内容。

如果有人能够提出补救措施/解决方案/启蒙之路,我将非常感激。

如果这是基本的,请提前道歉 - 但是,在我的辩护中,我对ASP.NET的复杂性很新。

此致

Gordon Norrie

1 个答案:

答案 0 :(得分:0)

如果GetSelectedTitle是除页面本身以外的任何类中的方法(CarCrash),则在引用它时需要指定类名。试试EmployeeFunctions.GetSelectedTitle()