【发布时间】:2013-04-25 16:16:29
【问题描述】:
我是 ASP.Net 上的菜鸟,为此被困了一段时间。
每当我的下拉列表的索引发生变化时,我都想用对象填充我的转发器。
这很好用,但是当我在我的下拉列表中选择一个包含任何对象的值时,上次调用的旧对象仍然存在,我希望它们消失。
我尝试使用 Datasource=null 清除转发器中的项目,然后再次执行 Databind,但该操作有效。
我认为它与中继器上的 ItemDataBound 事件有关。 当我在下拉列表中选择包含任何对象的值时,不会调用 ItemDatabound。
ItemDataBound 代码:
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 代码:
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.
}
}
【问题讨论】:
-
当你在下拉列表中没有值时隐藏你的转发器
-
我没想到。但这不起作用。也许我需要回发来“将更改设置为可见”?
-
你能显示你更新的代码吗
-
更新的sn-p代码现在在第一篇了。
-
嗯。我试图删除更新面板。现在它的作品,但当然还有回发。我想避免那些。但是上面的代码不起作用..我真的不知道为什么它不起作用
标签: c# asp.net drop-down-menu repeater itemdatabound