我有UpdatePanel(ASP.Net WebForms,.Net 4.0)的问题。这里是代码:UpdatePanel触发器没有触发
<div class="container-fluid">
<form id="form1" runat="server">
<h2>Poruke</h2>
<div class="row">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<asp:UpdatePanel ID="msgListUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<ContentTemplate>
<div class="col-md-4">
<asp:ListBox ID="msgList" runat="server" OnSelectedIndexChanged="msgList_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="AutoID"></asp:ListBox>
</div>
<div class="col-md-8">
<asp:ListBox ID="conversationList" runat="server" ClientIDMode="AutoID"></asp:ListBox>
<br class="divider" />
<p>
Odgovor: <span>
<asp:TextBox ID="replyTxtbox" runat="server"></asp:TextBox></span>
</p>
<asp:Button ID="sendBtn" runat="server" Text="Pošalji" OnClick="sendBtn_Click" EnableViewState="false" ClientIDMode="AutoID" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="msgList" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</div>
,这是代码隐藏...
int userIdCookie = 0;
message selected = new message();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
if (!Page.IsPostBack)
{
if (Int32.TryParse(HttpContext.Current.User.Identity.Name, out userIdCookie))
{
message msg = new message();
var allMsg = msg.allMessagesFormatted().Distinct().ToList();
msgList.DataSource = allMsg;
msgList.DataBind();
}
}
else
{
// test only!
replyTxtbox.Text = msgList.SelectedIndex.ToString();
msgListUpdatePanel.Update();
}
}
protected void msgList_SelectedIndexChanged(object sender, EventArgs e)
{
message msg = new message();
var allMsg = msg.allMessagesFormatted().Distinct().ToList();
msgList.DataSource = allMsg;
IList<message> boundList = (IList<message>)msgList.DataSource;
selected = boundList[msgList.SelectedIndex];
var conversation = msg.allMessagesFormatted().FindAll(x => x.conversationGuid == selected.conversationGuid);
conversationList.DataSource = conversation;
conversationList.DataBind();
}
protected void sendBtn_Click(object sender, EventArgs e)
{
if(selected.recipientId != 0)
{
message newmsg = new message();
newmsg.senderId = userIdCookie;
newmsg.recipientId = selected.recipientId;
newmsg.subject = selected.subject;
newmsg.messageTxt = replyTxtbox.Text;
newmsg.conversationGuid = selected.conversationGuid;
newmsg.time = DateTime.Now;
newmsg.Add();
}
}
msgList被填充正常,但当我改变选择,什么都不会发生 - 它的SelectedIndex事件永远不会触发。如果我将AutoPostBack =“true”设置为此列表框,它会重新加载页面(这正是我试图避免的)。
总结 - 当我点击ListBox里面的项目UpdatePanel时,没有任何反应(事件没有被触发)。我想在选定索引更改时避免页面重新加载。我试图解决方案(客户端ID,AsyncPostBack,十几“常规”回发的触发器,我想我错过了一个简单的细节和它的驾驶我疯狂
谁能帮
编辑 - ?作为@mason指出,问题是在含有\r\n
字符引起回发的问题overidden message.ToString()
方法。
令人难以置信。当我添加msgList时它工作得很好。DataSource = new List(){“A”,“B”,“C”};' 问题是在'Message'类中覆盖'.ToString()'。我把'\ r \ n'放在方法中,而不是那样。当我删除它时,其他所有工作都很好。 问题解决! –
nighthawk
2015-02-05 23:55:09
@nighthawk我更新了解决方案。无论如何,我建议不要使用UpdatePanel。相反,切换到使用[ASP.NET Web API](http://www.asp.net/web-api)以及使AJAX变得轻松的客户端脚本框架(如jQuery)。 – mason 2015-02-06 00:14:06
我可能会将它切换到WebAPI,因为UpdatePanel会导致很多问题(比如这个),并且会减慢页面的速度。 – nighthawk 2015-02-06 00:19:43