ASP.net UpdatePanel - Works first time then fails
I have a simple update panel that contains an ASP DataList. It shows a list of documents and their approval status. Here is some sample markup
<asp:UpdatePanel ID="upDocuments" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:DataList ID="dlDocuments" runat="server" RepeatLayout="Table">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" style="width:100%;">
<thead>
<th><span style="font-size:110%;">User Documents</span></th>
<th></th>
<th></th>
<th></th>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink ID="lnkDocName" runat="server" /></td>
<td>
<asp:Label ID="lblDocType" runat="server" /></td>
<td>
<asp:Label ID="lblApproved" runat="server" /></td>
<td>
<asp:LinkButton ID="btnApprove" runat="server" Visible="false" Text="Approve" />
<asp:Label ID="lblApprovedBy" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
In my code-behind, I have a routine called from Page_Load to get the data and bind it to the DataList. Here are the relevant lines.
// Load the documents - loaded to docList (List<Document>)
dlDocuments.DataSource = docList;
dlDocuments.DataBind();
All is great so far. Then, during the开发者_JS百科 DataList's item bound handler, I bind the btnApprove control to a handler to load the document and approve it.
if (!doc.Approved)
{
LinkButton btnApprove = (LinkButton)e.Item.FindControl("btnApprove");
btnApprove.Click += new EventHandler(btnApprove_Click);
btnApprove.Attributes.Add("docKey", doc.docKey);
btnApprove.Visible = true;
}
Then,
protected void btnApprove_Click(object sender, EventArgs e)
{
LinkButton btnApprove = (LinkButton)sender;
int docKey = Int32.Parse(btnApprove.Attributes["docKey"]);
Document doc = // Load document
doc.Approved = true;
doc.ApprovedBy = Page.User.Identity.Name;
doc.ApprovedDate = DateTime.Now;
// Save doc back to DB
LoadUserDocuments();
}
Again, all is great. I load the page, see the documents and their status and the links. My test case goes like this:
- Load the page
- Click "Approve" for the first document.
- Verify it is approved.
- Click "Approve" for the second document.
- Verify it is approved.
My test fails on step 5. I've debugged and the click handler is called, the DataList is re-bound to the results and the document is saved back to the database as approved. However, the page never refreshes to show the updated status of the second document. Does anyone know what step I'm missing on the post back that causes subsequent post backs to fail?
Are you checking for PostBack when you bind your data on Page_Load?
if(!IsPostBack)
BindDataList();
精彩评论