2013-04-25 13 views
5

Jestem nowicjuszem na ASP.Net i utknąłem w tym przez jakiś czas.Jak powiązać repeater ItemDataBound, aby zaktualizować na liście rozwijanej SelectedIndexChanged

Za każdym razem, gdy zmienia się wskaźnik mojego rozwijania, chcę wypełnić mój repeater obiektami.

Działa to dobrze, ale gdy im wybiorę wartość w moim menu rozwijanym, która zawiera wszystkie obiekty, które wciąż znajdują się w starych obiektach z ostatniego połączenia, chcę, aby zniknęły.

Próbowałem usunąć elementy z przemiennika za pomocą Datasource = null, a następnie ponownie wykonać Databind, ale ta praca działa.

Myślę, że ma to miejsce w przypadku zdarzenia ItemDataBound na moim przekaźniku. Funkcja ItemDatabound nie jest wywoływana, gdy wybiorę wartość na liście rozwijanej, która zawiera dowolne obiekty.

ItemDataBound KOD:

protected void rptStudentQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     Label lblAnswer = e.Item.FindControl("lblAnswer") as Label; 
     TextBox tbxAnswer = e.Item.FindControl("tbxAnswer") as TextBox; 
     Button btnSend = e.Item.FindControl("btnSend") as Button; 
     if (lblAnswer.Text == "" || lblAnswer == null) 
     { 
      lblAnswer.Visible = false; 
      lblAnswer.Enabled = false; 
      tbxAnswer.Visible = true; 
      tbxAnswer.Enabled = true; 
      btnSend.Enabled = true; 
      btnSend.Visible = true; 
     } 
     else 
     { 
      lblAnswer.Visible = true; 
      lblAnswer.Enabled = true; 
      tbxAnswer.Visible = false; 
      tbxAnswer.Enabled = false; 
      btnSend.Enabled = false; 
      btnSend.Visible = false; 

     } 
    } 
} 

OnSelectedIndexChanged Kod:

protected void DrpdwnLectureName_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string SelectedLecture = DrpdwnLectureName.SelectedValue; 
    string user = Server.HtmlEncode(Context.User.Identity.Name).ToString(); 
    using (var client = new WCFReference.SRSServiceClient()) 
    { 
     var LectureList = client.GetTeacherLecture(user); 
     foreach (var item in LectureList) 
     { 
      if (item.LectureName == DrpdwnLectureName.SelectedValue) 
      { 
       var list = client.GetStudentQuestions(item.LectureID, user); 
       rptStudentQuestion.DataSource = list; 
       rptStudentQuestion.DataBind(); 
      } 
     }    

    } 

} 

Znacznik:

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
      <asp:DropDownList ID="DrpdwnLectureName" AutoPostBack="True" runat="server" OnSelectedIndexChanged="DrpdwnLectureName_SelectedIndexChanged"></asp:DropDownList> 
      <asp:Panel ID="PrintPanel" runat="server"> 
       <asp:Label ID="Label1" runat="server" Text="Gör en .pdf på besvarade frågor"></asp:Label> 
       <asp:Button ID="btnDoPdf" runat="server" Text="Button" OnClick="btnDoPdf_Click" /> 
      </asp:Panel> 
      <asp:Repeater ID="rptStudentQuestion" runat="server" OnItemCommand="rptStudentQuestion_ItemCommand" OnItemDataBound="rptStudentQuestion_ItemDataBound"> 
       <ItemTemplate> 
        <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("StudentQuestionQuestion") %>'></asp:Label> 
        <br /> 
        <asp:TextBox ID="tbxAnswer" runat="server" Visible="false"></asp:TextBox> 
        <asp:Button ID="btnSend" CommandName="SendAnswer" runat="server" Text="Skicka svar" CommandArgument='<%# Eval("StudentQuestionID") %>' /> 
        <br /> 
        <asp:Label ID="lblAnswer" runat="server" Text='<%# Eval("StudentQuestionAnswer") %>' Visible="false"></asp:Label> 
        <br /> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

zaktualizowanego kodu zgodnie z wnioskiem (Fragment z DrpdwnLectureName_SelectedIndexChanged)

if (item.LectureName == DrpdwnLectureName.SelectedValue) 
{ 
    var list = client.GetStudentQuestions(item.LectureID, user); 
    if (list.Count() > 0) 
    { 
     rptStudentQuestion.Visible = true; 
     rptStudentQuestion.DataSource = list; 
     rptStudentQuestion.DataBind(); 
    } 
    else 
    { 
     rptStudentQuestion.Visible = false; // In debug it preforms this, but nothing happens.        
    } 
} 
+0

ukryć swój repeater gdy u nie mają wartość na liście rozwijanej –

+0

Nie pomyślałem o tym. Ale to nie działa. Może potrzebuję odświeżenia strony, aby "ustawić widoczne zmiany"? –

+0

możesz pokazać zaktualizowany kod –

Odpowiedz

5

To nie jest rozwiązanie, ale można rozwiązać aktualizacji panel uaktualniania problem. Można sterować ręcznie aktualizację Updatepanel to robi:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"> 
     <ContentTemplate> 
      <asp:DropDownList ID="DrpdwnLectureName" AutoPostBack="True" runat="server" OnSelectedIndexChanged="DrpdwnLectureName_SelectedIndexChanged"></asp:DropDownList> 
      <asp:Panel ID="PrintPanel" runat="server"> 
       <asp:Label ID="Label1" runat="server" Text="Gör en .pdf på besvarade frågor"></asp:Label> 
       <asp:Button ID="btnDoPdf" runat="server" Text="Button" OnClick="btnDoPdf_Click" /> 
      </asp:Panel> 
      <asp:Repeater ID="rptStudentQuestion" runat="server" OnItemCommand="rptStudentQuestion_ItemCommand" OnItemDataBound="rptStudentQuestion_ItemDataBound"> 
       <ItemTemplate> 
        <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("StudentQuestionQuestion") %>'></asp:Label> 
        <br /> 
        <asp:TextBox ID="tbxAnswer" runat="server" Visible="false"></asp:TextBox> 
        <asp:Button ID="btnSend" CommandName="SendAnswer" runat="server" Text="Skicka svar" CommandArgument='<%# Eval("StudentQuestionID") %>' /> 
        <br /> 
        <asp:Label ID="lblAnswer" runat="server" Text='<%# Eval("StudentQuestionAnswer") %>' Visible="false"></asp:Label> 
        <br /> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ContentTemplate> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="DrpdwnLectureName" /> 
     </Triggers> 
    </asp:UpdatePanel> 

i kiedy chcesz zaktualizować panelu w kodzie, zadzwoń: „UpdatePanel1.Update()”

if (item.LectureName == DrpdwnLectureName.SelectedValue) 
{ 
    var list = client.GetStudentQuestions(item.LectureID, user); 
    if (list.Count() > 0) 
    { 
     rptStudentQuestion.Visible = true; 
     rptStudentQuestion.DataSource = list; 
     rptStudentQuestion.DataBind(); 
    } 
    else 
    { 
     rptStudentQuestion.Visible = false; // In debug it preforms this, but nothing happens.  
     UpdatePanel1.Update() //This 'force' updatepanel updating   
    } 
} 
Powiązane problemy