2

I'm rather stuck concerning a problem I'm having when binding the SelectedIndex property of the <asp:DropDownList> object to a method in a custom class (which is located within my app_code folder).

The text for an ObjectDataSource and a GridView object - which uses the same ObjectDataSource as its data source - can be seen below. It seems that the GetSelectedIndex method that I've got in my custom class (show further down) - and tried to bind to the SelectIndex property of my GridView object - can't be found:

<%@ 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>

The custom classes that I'm trying to bind to contains the following:

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());
}



....
}

The class object which this uses is the EmployeeDetails class, defined as follows:

[Serializable]
public class EmployeeDetails
{


    public int EmployeeID{get;set;}

    public string FirstName{get;set;} 

    public string LastName{get;set;}

    public string TitleOfCourtesy{get;set;}

    public string City { get; set; }

    public DateTime? BirthDate { get; set; }

    public string Notes { get; set; }

    public string Address { get; set; }

    public string HomePhone { get; set; }

    public string PostalCode { get; set; }

    public EmployeeDetails(int employeeID, string firstName, string lastName, string titleOfCourtesy, DateTime? birthDate, string city = "", string notes = "Blank")
    {
        EmployeeID = employeeID;
        FirstName = firstName;
        LastName = lastName;
        TitleOfCourtesy = titleOfCourtesy;
        City = city;
        BirthDate = birthDate;
        Notes = notes;
    }

    public EmployeeDetails(int employeeID, string firstName, string lastName, string titleOfCourtesy, DateTime? birthDate, string city = "", string notes = "Blank", string homePhone = "", string postalCode = "", string address = "")
    {
        EmployeeID = employeeID;
        FirstName = firstName;
        LastName = lastName;
        TitleOfCourtesy = titleOfCourtesy;
        City = city;
        BirthDate = birthDate;
        Notes = notes;
        HomePhone = homePhone;
        Address = address;
        PostalCode = postalCode;
    }
    public EmployeeDetails() { }
}

I'm afraid that I'm really rather stumped by this. I've searched many different help forums, but haven't really come across anything which describes / diagnoses my exact problem.

If anyone might be able to suggest a remedy / solution / path to enlightenment, I'd be most grateful.

Apologies in advance if this is elementary - but, in my defence, I'm rather new to the intricacies of ASP.NET.

Regards,

Gordon Norrie

khr055
  • 28,690
  • 16
  • 36
  • 48
  • You know your method is actually called `GetSelectedTitle`, not `GetSelectedIndex`, right? – AakashM Sep 03 '12 at 12:17
  • Ooops, yes, you're right. Thanks for pointing this out. I'd changed this when I was fiddling about with it. However, even with the 'SelectedIndex' property of the EditItemTemplate object pointing at the method, 'GetSelectedTitle', it still produces this error. – Gordon Norrie Sep 04 '12 at 08:35

1 Answers1

0

If GetSelectedTitle is a method in any class other than the page itself (CarCrash), you'll need to specify the class name when you refer to it. Try EmployeeFunctions.GetSelectedTitle().

AakashM
  • 62,551
  • 17
  • 151
  • 186
  • Thanks for your reply. Unfortunately I'd already tried that one - but this just returns the error: 'CS0103: The name 'EmployeeFunctions' does not exist in the current context'. The mystery continues..... – Gordon Norrie Sep 05 '12 at 15:38