Im a rookie on ASP.Net and been stuck for this for a while.
Everytime the index of my dropdown changes i want to fill my repeater with objects.
This works fine, but when im selecting a value in my dropdown that dosent contain any objects the old objects from the last call is still there, i want them to disappear.
I've tried to clear the items from the repeater using Datasource=null and then do a Databind again, but that dosent work.
I think it has with the ItemDataBound event on my repeater. The ItemDatabound is not called when i select a value in the dropsdownlist that dosent contain any objects.
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();
}
}
}
}
Markup CODE:
<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>
Updated Code as Requested(Snippet from 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.
}
}
This is not a solution, but can solve your update panel updating problem. You can control updatepanel update manually doing this:
<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>
and when you want to update the panel in code, you call: "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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With