有没有办法让ASP.NET DropDownList中的项目的文本或值绑定到源上的方法而不是属性?将ASP.NET DropDownList的DataTextField绑定到方法?
6
A
回答
0
要做到这一点的唯一方法是处理DropDownList的Databinding事件,调用方法并自己设置DropDownList项中的值。
-4
以声明:
<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>
以上结合到返回泛型列表的方法,但你也可以绑定到返回一个DataReader的方法。你也可以用代码创建你的数据源。
3
这是我的解决方案:
<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>();
}
0
有时候,我需要使用导航属性为DataTextField,像(“User.Address.Description”),所以我决定创建一个简单的控制,从DropDownList中派生。 我也实现了一个ItemDataBound事件,可以帮助。
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);
}
}
}
}
1
这里有两个例子从一个类
你的aspx页面
<asp:DropDownList ID="DropDownListJour1" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownListJour2" runat="server">
</asp:DropDownList>
您的aspx.cs页面
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();
}
你怨妇结合在ASP.net下拉。 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;
}
}
+0
非常有用,我能找到的唯一答案之一显示了如何设置DataTextField和DataValueField – 2016-06-30 07:35:13
相关问题
- 1. 将数据绑定到ASP.NET中的DropDownList
- 2. DropDownList的与DataValueField和DataTextField asp.net C#
- 3. asp.net mvc dropdownlist绑定
- 4. 将dropdownlist绑定到dropdownlist以查看详细信息ASP.NET C#
- 5. 无法将数据绑定到DropDownList
- 6. 数据绑定到asp.net中的dropdownlist?
- 7. 将ASP.net DropDownList的选定值绑定到自定义对象
- 8. Asp.Net MVC DropDownList数据绑定
- 9. 将XMLWebService中的值绑定到ASP.Net中的Dropdownlist中
- 10. 如何将json数据绑定到使用jquery的asp.net dropdownlist?
- 11. 如何将GridView的列绑定到DropDownList?
- 12. dropdownlist由属性组成的DataTextField?
- 13. 将CheckBox绑定到方法
- 14. 将方法绑定到gridview
- 15. 的DropDownList绑定到的ActionResult Create方法MVC 4 VS2012
- 16. 使用asp.net mvc 2.0将数据从数据库绑定到dropdownlist
- 17. 绑定到ListControl而不指定DataTextField和DataValueField?
- 18. 将DropDownList绑定到十进制数
- 19. 将DropDownList数据源绑定到XPathSelect
- 20. 如何将结构绑定到DropDownList
- 21. 将字符串数组绑定到DropDownList?
- 22. 如何将Kendo DropDownList绑定到Observable类?
- 23. 是否可以将hiddenfield绑定到dropdownlist?
- 24. 如何将XDocument绑定到DropDownList?
- 25. ASP.NET绑定的DropDownList抛出错误
- 26. 的DropDownList的设置DataTextField =的ItemIndex
- 27. 无法将DropDownList的selectedIndex绑定到Bindable类的selectedIndex成员
- 28. 如何使用C#将ASP.NET数据源绑定到ASP.NET动态ListView中的DropDownList?
- 29. 将ViewModel绑定到DropDownListFor除第三个值以外dataValueField/dataTextField
- 30. 导航属性上的DropDownList DataTextField
在您的示例中,DataTextField和DataValueField是属性。我需要将源代码中调用方法的结果作为文本或值。 – kenstone 2008-09-25 18:04:32