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"를이 목록 상자에 설정하면 페이지가 다시로드됩니다 (피하려고하는 내용).
요약하면 - UpdatePanel 내의 ListBox에서 항목을 클릭하면 아무 일도 일어나지 않습니다 (이벤트가 실행되지 않음). 선택한 인덱스가 변경되면 페이지를 다시로드하지 않도록하고 싶습니다. 내가 솔루션 (된 ClientID, AsyncPostBack의 다스를 시도했다 "일반"다시 게시 트리거와 같아요 내가 하나의 간단한 디테일을 놓친하고 화가 날 운전 것
누군가가편집을 도와 드릴까요
-.? @mason는 지적 , 문제는 포스트 백에 문제가 발생 \r\n
문자를 포함 오버라이드 (override) message.ToString()
방법이었다.
믿을 수 없습니다. 'msgList를 추가하면 잘 작동합니다. 문제는 Message 클래스의'.ToString()'에서 재정의되었습니다. 나는'\ r \ n'을 메서드에 넣었습니다. 내가 그것을 제거했을 때, 다른 모든 것들이 잘 작동했습니다. 문제가 해결되었습니다. – nighthawk
@nighthawk 솔루션으로 업데이트했습니다. 어쨌든, 결코 UpdatePanel을 사용하지 마십시오. 대신 AJAX를 쉽게 만들 수있는 클라이언트 측 스크립팅 프레임 워크 (예 : jQuery)와 함께 [ASP.NET 웹 API] (http://www.asp.net/web-api)로 전환하십시오. – mason
UpdatePanel이 많은 문제를 일으키고 페이지 속도가 느려졌 기 때문에 아마 WebAPI로 바꿀 것입니다. – nighthawk