c# asp.net drop-down-menu repeater itemdatabound

c# - Cómo enlazar el repetidor ItemDataBound, para actualizar en la lista desplegable SelectedIndexChanged



asp.net drop-down-menu (1)

Soy un novato en ASP.Net y he estado atascado por esto por un tiempo.

Cada vez que cambia el índice de mi lista desplegable quiero llenar mi repetidor con objetos.

Esto funciona bien, pero cuando estoy seleccionando un valor en mi menú desplegable que contiene dos objetos que los objetos antiguos de la última llamada todavía está allí, quiero que desaparezcan.

Intenté borrar los elementos del repetidor utilizando Datasource = null y luego realicé un Databind nuevamente, pero ese dosent funcionó.

Creo que tiene el evento ItemDataBound en mi repetidor. El ItemDatabound no se llama cuando selecciono un valor en la lista de dropsdown que dosent contiene cualquier objeto.

ItemDataBound CÓDIGO:

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

CÓDIGO DE MARCACIÓN:

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

Código actualizado como solicitado (fragmento de 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. } }


Esta no es una solución, pero puede resolver su problema de actualización del panel de actualización. Puede controlar la actualización de updatepanel manualmente haciendo esto:

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

y cuando desee actualizar el panel en el código, llame al: "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 } }