SelectedIndex
在将对象的属性绑定<asp:DropDownList>
到自定义类(位于我的app_code
文件夹中)中的方法时,我遇到了一个问题。
ObjectDataSource
一个和一个对象的文本GridView
- 使用相同ObjectDataSource
的数据源 - 可以在下面看到。似乎GetSelectedIndex
我在自定义类中获得的方法(进一步显示) - 并试图绑定到SelectIndex
我的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>
我尝试绑定的自定义类包含以下内容:
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 的复杂性相当陌生。
问候,
戈登·诺里