2013-04-25 21 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   
    } 
} 
相关问题