2

SelectedIndexオブジェクトのプロパティを<asp:DropDownList>カスタムクラス(私のapp_codeフォルダー内にある)のメソッドにバインドするときに発生している問題に関して、私はかなり行き詰まっています。

ObjectDataSourceおよびGridViewオブジェクトのテキスト (ObjectDataSourceデータ ソースと同じものを使用) を以下に示します。カスタム クラス (さらに下に表示) で取得したメソッド (オブジェクトのプロパティGetSelectedIndexにバインドしようとした)が見つからないようです。SelectIndexGridView

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

バインドしようとしているカスタム クラスには、次のものが含まれています。

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



....
}

これが使用するクラス オブジェクトは、次のように定義された EmployeeDetails クラスです。

[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() { }
}

私は本当にこれに困惑しているのではないかと心配しています。さまざまなヘルプ フォーラムを検索しましたが、正確な問題を説明または診断するものは実際には見つかりませんでした。

誰かが悟りへの救済策/解決策/道を提案できるなら、私は最も感謝しています.

これが初歩的なものである場合は、事前にお詫び申し上げます。ただし、弁明しますが、私は ASP.NET の複雑さに慣れていません。

よろしく、

ゴードン・ノリー

4

1 に答える 1

0

GetSelectedTitleがページ自体以外のクラスのメソッドである場合( CarCrash)、参照するときにクラス名を指定する必要があります。試してみてくださいEmployeeFunctions.GetSelectedTitle()

于 2012-09-04T08:51:05.843 に答える