2013-04-25 23 views
5

我是ASP.Net的新手,一直堅持這一點。如何綁定中繼器ItemDataBound,在下拉列表中進行更新SelectedIndexChanged

每次我的下拉列表變化的索引我想填充我的中繼器的對象。

這工作得很好,但是當我在我的下拉菜單中選擇一個值時,dosent包含任何對象,最後一次調用的舊對象仍然存在,我希望它們消失。

我試圖清除使用Datasource = null中繼器的項目,然後再次做一個Databind,但該dosent工作。

我認爲它與我的中繼器上的ItemDataBound事件有關。 當我在dosent包含任何對象的下拉列表中選擇一個值時,不會調用ItemDatabound。

的ItemDataBound CODE:

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 CODE:

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(); 
      } 
     }    

    } 

} 

標記代碼:

<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> 

更新的代碼的要求(摘錄自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

隱藏你的中繼當u沒有下拉列表中的值 – 2013-04-25 16:25:35

+0

我沒有想到這一點。但那不起作用。也許我需要回發「設置更改可見」? – 2013-04-25 16:30:10

+0

是否可以顯示您的更新代碼 – 2013-04-25 16:35:32

回答

5

這不是一個解決方案,但可以解決你的更新面板的更新問題。您可以控制的UpdatePanel更新手動這樣做:

<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> 

,當你要更新代碼面板,你叫:「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   
    } 
} 
相關問題