asp.net dropdownlist datatextfield 바인딩 메소드?
-
02-07-2019 - |
문제
어쨌든 ASP.NET DropdownList에 텍스트 나 값이 속성이 아닌 소스의 메소드에 바인딩되는 항목이 있습니까?
해결책
이를 수행하는 유일한 방법은 DropdownList의 데이터베이닝 이벤트를 처리하고 방법을 호출하고 DropdownList 항목에 직접 값을 설정하는 것입니다.
다른 팁
이것은 내 해결책입니다.
<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>();
}
다음은 수업에서 ASP.NET의 드롭 다운을 바인딩하기위한 두 가지 예입니다.
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();
}
귀하의 Jour.cs 클래스 (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;
}
}
때로는 내비게이션 속성을 Datatextfield ( "user.address.description")와 같이 사용해야하므로 DropdownList에서 파생되는 간단한 컨트롤을 작성하기로 결정했습니다. 또한 도움이 될 수있는 항목 행사 이벤트도 구현했습니다.
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 );
}
}
}
}
선언적으로 :
<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를 반환하는 메소드에도 바인딩 할 수도 있습니다. 코드로 데이터 소스를 만들 수도 있습니다.
제휴하지 않습니다 StackOverflow