Có anyway để có các mục trong một ASP.NET DropDownList có hoặc văn bản hoặc giá trị của họ ràng buộc với một phương pháp trên nguồn chứ không phải là một tài sản?Ràng buộc ASP.NET DropDownList DataTextField vào phương thức?
Trả lời
Cách duy nhất để làm điều đó là xử lý sự kiện Databinding của DropDownList, gọi phương thức và tự đặt giá trị trong mục DropDownList.
khai báo:
<asp:DropDownList ID="ddlType" runat="server" Width="250px" AppendDataBoundItems="true" DataSourceID="dsTypeList" DataTextField="Description" DataValueField="ID">
<asp:ListItem Value="0">All Categories</asp:ListItem>
</asp:DropDownList><br />
<asp:ObjectDataSource ID="dsTypeList" runat="server" DataObjectTypeName="MyType" SelectMethod="GetList" TypeName="MyTypeManager">
</asp:ObjectDataSource>
liên kết với chúng ở trên để một phương pháp mà trả về một danh sách chung, nhưng bạn cũng có thể liên kết với một phương pháp mà trả về một DataReader. Bạn cũng có thể tạo nguồn dữ liệu của bạn trong mã.
Đây là giải pháp của tôi:
<asp:DropDownList ID="dropDownList" runat="server" DataSourceID="dataSource" DataValueField="DataValueField" DataTextField="DataTextField" />
<asp:ObjectDataSource ID="dataSource" runat="server" SelectMethod="SelectForDataSource" TypeName="CategoryDao" />
public IEnumerable<object> SelectForDataSource()
{
return _repository.Search().Select(x => new{
DataValueField = x.CategoryId,
DataTextField = x.ToString() // Here is the trick!
}).Cast<object>();
}
Đôi khi tôi cần phải sử dụng Navigation Properties như DataTextField, giống như ("User.Address.Description"), vì vậy tôi quyết định tạo ra một điều khiển đơn giản mà bắt nguồn từ DropDownList. Tôi cũng đã triển khai một sự kiện ItemDataBound có thể trợ giúp.
public class RTIDropDownList : DropDownList
{
public delegate void ItemDataBoundDelegate(ListItem item, object dataRow);
[Description("ItemDataBound Event")]
public event ItemDataBoundDelegate ItemDataBound;
protected override void PerformDataBinding(IEnumerable dataSource)
{
if (dataSource != null)
{
if (!AppendDataBoundItems)
this.Items.Clear();
IEnumerator e = dataSource.GetEnumerator();
while (e.MoveNext())
{
object row = e.Current;
var item = new ListItem(DataBinder.Eval(row, DataTextField, DataTextFormatString).ToString(), DataBinder.Eval(row, DataValueField).ToString());
this.Items.Add(item);
if (ItemDataBound != null) //
ItemDataBound(item, row);
}
}
}
}
Dưới đây là 2 ví dụ về ràng buộc một thả xuống trong ASP.net từ một lớp
trang aspx của bạn
<asp:DropDownList ID="DropDownListJour1" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownListJour2" runat="server">
</asp:DropDownList>
trang aspx.cs của bạn
protected void Page_Load(object sender, EventArgs e)
{
//Exemple with value different same as text (dropdown)
DropDownListJour1.DataSource = jour.ListSameValueText();
DropDownListJour1.DataBind();
//Exemple with value different of text (dropdown)
DropDownListJour2.DataSource = jour.ListDifferentValueText();
DropDownListJour2.DataValueField = "Key";
DropDownListJour2.DataTextField = "Value";
DropDownListJour2.DataBind();
}
jour của bạn. cs class (jour.cs)
public class jour
{
public static string[] ListSameValueText()
{
string[] myarray = {"a","b","c","d","e"} ;
return myarray;
}
public static Dictionary<int, string> ListDifferentValueText()
{
var joursem2 = new Dictionary<int, string>();
joursem2.Add(1, "Lundi");
joursem2.Add(2, "Mardi");
joursem2.Add(3, "Mercredi");
joursem2.Add(4, "Jeudi");
joursem2.Add(5, "Vendredi");
return joursem2;
}
}
Rất hữu ích, một trong số ít câu trả lời duy nhất mà tôi có thể tìm thấy cho thấy cách thiết lập DataTextField & DataValueField –
- 1. ASP.NET WebForms DropDownList dữ liệu ràng buộc hành vi lạ?
- 2. Cài đặt SelectedValue của dữ liệu bị ràng buộc DropDownList
- 3. Ràng buộc DropDownList vào ListItemCollection và Giá trị không được thêm vào DDL
- 4. Asp.Net MVC Liên kết dữ liệu DropDownList
- 5. Ràng buộc danh sách thả xuống với JQuery trong Asp.Net
- 6. ASP.Net - Phương thức gọi từ đánh dấu bao gồm ràng buộc dữ liệu
- 7. Tham số ASP.NET MVC ActionFilter ràng buộc
- 8. Ràng buộc loại nhiều phương thức chung (OR)
- 9. DropDownList Binding vào MVC Xem
- 10. Dữ liệu ràng buộc vào html5 DataList trong asp.net
- 11. Chuyển đổi một hình thức không ràng buộc thành một hình thức bị ràng buộc?
- 12. Populating ASP.NET MVC DropDownList
- 13. ASP.NET MVC Mẫu danh sách ràng buộc
- 14. ASP.NET EditorTemplate DropdownList
- 15. ASP.NET Auto Complete DropDownList
- 16. Thêm món hàng vào databound DropDownList
- 17. Danh sách thả xuống ràng buộc bên trong GridView edititemtemplate
- 18. Laravel mô hình hình thức ràng buộc
- 19. ASP.NET MVC ViewModel và DropDownList
- 20. Phân lớp DropDownList trong ASP.NET
- 21. ASP.NET MVC DropDownList Selected Value Problem
- 22. Phương pháp chung với nhiều ràng buộc
- 23. Generic TimeSpan ràng buộc trong Asp.NET MVC 2
- 24. asp.net ràng buộc một danh sách để một GridView
- 25. Ràng buộc vào trường const trong Silverlight
- 26. Giá trị được chọn ASP.Net DropDownList
- 27. Biểu thức mã ASP.NET, Ràng buộc dữ liệu và các Expressoins tuyên bố khác
- 28. Ràng buộc vào một cấu trúc
- 29. Mô hình động ràng buộc với ASP.NET WEB API
- 30. Giải thích ràng buộc mô hình tùy chỉnh ASP.Net MVC
Trong mẫu của bạn, DataTextField và DataValueField là các thuộc tính. Tôi cần kết quả của việc gọi một phương thức trên nguồn là văn bản hoặc giá trị. – kenstone