2010-04-23 4 views
3

데이터베이스에서 드롭 다운 목록을 채우는 데 문제가 있습니다. 드롭 다운 컨트롤을 찾을 수없는 데이터 소스를 설정하려고 할 때 DetailsView에 있으므로 편집 모드 일 때만 생성되는 것과 관련이있을 수 있습니다. 여전히 편집 중이면 현재 모드라고 말하며, 거기에서 무슨 일이 일어나는지 확실하지 않습니다. 여기 DetailsView 내 DropDownList 바인딩

은 영문 파일의 코드입니다 :

<asp:DetailsView id="DetailsView1" runat="server" AutoGenerateRows="false" DataSourceID="myMySqlDataSrc" DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="False" > 
    <Fields> 
     <asp:TemplateField HeaderText="Region"> 
      <ItemTemplate><%# Eval("region_name") %></ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="RegionDropdownList" runat="server" SelectedValue='<%# Bind("region_id")%>' /> 
      </EditItemTemplate> 
     </asp:TemplateField>   
    </Fields> 
</asp:DetailsView> 

그리고 이것은 뒤에있는 코드에서이다 :

protected void DetailsView1_ItemCreated(object sender, EventArgs e) 
{ 
    ArrayList regionsList = BPBusiness.getRegions(); 
    if (DetailsView1.CurrentMode == DetailsViewMode.Edit) 
    { 
     DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList"); 
     if (ddlRegions != null) 
     { 
      ddlRegions.DataSource = regionsList; 
      ddlRegions.DataBind(); 
     } 
    } 
} 

에 기억 itemcreated 방법으로 그 일을

ArrayList regionsList = BPBusiness.getRegions(); 
if (DetailsView1.CurrentMode == DetailsViewMode.Edit) 
{ 
    DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList"); 
    if (ddlRegions != null) 
    { 
     ddlRegions.DataSource = regionsList; 
     ddlRegions.DataBind(); 
    } 
} 
+0

뒤에 존재 에서? –

+0

Page_Load에 있습니다. – annelie

답변

3

아직 작성하지 않은 경우 코드의 샘플을 DetailsView1_ModeChanged 또는 DetailsView1_DataBound 메소드 안에 배치하십시오. DetailsView1_ModeChanging 메서드에 있으면 모드가 실제로 변경되지 않았습니다.

편집 : 또한, 당신이 그렇게 같은 DataTextField 및 DataValueField을 설정해야합니다 :

DropDownList1.DataTextField = "TextFieldName"; 
DropDownList1.DataValueField = "ValueFieldName"; 

또한 SelectedValue 바인딩을 제거; throw 오류를 제외하고는 아무것도 수행하지 않습니다.

편집 2 : 당신이 정말로 그것을 먼저 데이터 바인딩 때 드롭 다운리스트의 특정 값을 선택해야하는 경우, 당신은 이런 식으로 뭔가를 할 수 :

if(DropDownList1.Items.Contains(DropDownList1.Items.FindByValue("Value"))) 
{ 
    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value)); 
} 
코드에서 샘플을 무엇 방법
+0

DetailsView_DataBound 메서드에 넣으려고했는데 SelectedValue에 오류가 발생하지만 목록을 제거하면 오류가 발생합니다. 채워진다. 단지 System.Collections.ArrayList를 사용하여 원하는 값 대신 텍스트 및 값 속성을 어떻게 든 설정해야한다고 생각합니다! :) 나는 그 사람들을 거기에있는 SelectedValue로 다시 테스트하려고 노력할 것이다. – annelie

+0

@annelie 제 편집 참조 :). –

+0

고마워, 나는 그것을 시험해 볼 것이다! 나는 arraylists의 arraylist가 있었으므로 먼저 arraylists를 변경해야합니다. 잘하면 일해야합니다! :) – annelie

2

시도 세트 OnItemCreated="DetailsView1_ItemCreated"

+0

고마워요,이 컨트롤을 찾을 수 있지만 ddlRegions.DataBind() 실패 "Eval(), XPath() 및 Bind() 같은 데이터 바인딩 방법은 데이터 바인딩 된 컨트롤의 컨텍스트에서 사용할 수 있습니다. ". – annelie

+0

Eval ("region_name") 대신 DataBinder.Eval (Container.DataItem, "region_name") 메서드를 사용합니다. – Glennular

관련 문제