我有一個基本的網格視圖啓用分頁。我在數據庫中有11行,gridview的頁面大小爲10.當我來到頁面時,它顯示了10行,而在我的分頁器中,它在第一頁顯示了我的鏈接,並顯示了第二頁的鏈接。當我點擊第二頁時,它刷新頁面並進入第2頁,並顯示第11行,孤獨的行。在UpdatePanel中的GridView頁面下降1行
但是,當我在它周圍放置更新面板時,會丟棄最後一行。當我來到頁面時,它顯示與沒有更新面板相同的內容。它在尋呼機上顯示10行,第1頁和第2頁。然而,當我點擊第2頁時,它確實是ajax的東西,但不顯示第二頁上的最後一條記錄。然後,如果我從第2頁返回到第1頁,它只顯示9行而不是顯示的10行。
出於某種原因,當我有一個更新面板圍繞我的gridview,頁面一次後,它從不顯示最後一行。我嘗試了RenderMode,ChildrenAsTriggers和UpdateMode的所有不同組合,但都無濟於事。我也在頁面上有一個表單,它允許你在更新面板中添加新的行到數據庫中,因此Gridview中也有一個編輯和刪除鏈接。當我通過表單或編輯/刪除添加一個新行時,這不會發生......它只發生在我頁面時。
任何想法,爲什麼gridview不會顯示最後一個記錄/行後,我只有當頁面更新面板內?
這裏是代碼...
ASPX
<asp:UpdatePanel runat="server" ID="uPnl" ChildrenAsTriggers="true" RenderMode="Block" UpdateMode="Always">
<ContentTemplate>
<asp:Panel runat="server" ID="pnlAddComment" CssClass="addComment" DefaultButton="btnSubmitComment">
<h1 class="postComment">Post Comment</h1>
<asp:TextBox runat="server" ID="txtName" CssClass="txtName"></asp:TextBox>
<ajaxToolkit:TextBoxWatermarkExtender ID="txtNameW" runat="server"
TargetControlID="txtName"
WatermarkText="- Type your name here -"
WatermarkCssClass="txtNameWatermark" />
<asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="txtName" Display="Dynamic" ErrorMessage="Please enter your name" ValidationGroup="comment"></asp:RequiredFieldValidator>
<asp:TextBox runat="server" ID="txtComment" TextMode="MultiLine" CssClass="txtComment"></asp:TextBox>
<ajaxToolkit:TextBoxWatermarkExtender ID="TBWE2" runat="server"
TargetControlID="txtComment"
WatermarkText="- Write anything you'd like about this event -"
WatermarkCssClass="txtCommentWatermark" />
<asp:RequiredFieldValidator runat="server" ID="reqComment" ControlToValidate="txtComment" Display="Dynamic" ErrorMessage="Please enter your comment" ValidationGroup="comment"></asp:RequiredFieldValidator>
<div class="buttons">
<asp:Button runat="server" ID="btnSubmitComment" ValidationGroup="comment" Text="Submit Comment" />
<span class="loader">Saving</span>
</asp:Panel>
<h1>Recent Comments</h1>
<a name="comments"> </a>
<asp:GridView runat="server" ID="gvComments" DataKeyNames="CommentID" PagerSettings-Position="TopAndBottom" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" GridLines="None" CssClass="comments">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="comment user">
<p>
<img src="images/icon.gif" width="46" height="55" />
<%#Eval("UserComment")%>
<span>
Posted by <%#Eval("UserName")%> <br/>
on <%#Format(Eval("DateCreated"), "MM/dd/yyyy")%> at <%#Format(Eval("DateCreated"), "h:mm tt")%>
</span>
<asp:LinkButton runat="server" CausesValidation="false" ID="lnkEdit" CssClass="edit" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lnkDelete" CommandArgument='<%#Eval("CommentID")%>' CssClass="delete" OnClientClick="return confirm('Are you sure you want to delete this comment?');" CausesValidation="false" OnClick="DeleteComment" Text="Delete"></asp:LinkButton>
</p>
</div>
</ItemTemplate>
<EditItemTemplate>
<div class="comment user">
<p>
<img src="images/icon.gif" width="46" height="55" />
<label>Name:</label>
<asp:TextBox runat="server" ID="txtNameEdit" Width="240" Text='<%#Eval("UserName")%>'></asp:TextBox><br />
<label>Comment:</label>
<asp:TextBox runat="server" TextMode="MultiLine" ID="txtCommentEdit" Width="240" Height="100" Text='<%#Eval("UserComment") %>'></asp:TextBox>
<asp:LinkButton runat="server" ID="lnkCancel" CommandName="Cancel" CssClass="cancel" CausesValidation="false" Text="Cancel"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lnkUpdate" CommandName="Update" CssClass="update" CausesValidation="false" Text="Update"></asp:LinkButton>
</p>
</div>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="grdFooter" HorizontalAlign="right" />
<PagerSettings PageButtonCount="7" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
VB代碼背後
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bindComments()
End If
End Sub
Private Sub bindComments()
gvComments.DataSource = dataaccess.getdataset("SELECT * FROM Comments ORDER BY DateCreated DESC", Data.CommandType.Text)
gvComments.DataBind()
End Sub
Protected Sub gvComments_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvComments.RowEditing
gvComments.EditIndex = e.NewEditIndex
bindComments()
End Sub
Protected Sub gvComments_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvComments.RowCancelingEdit
gvComments.EditIndex = -1
bindComments()
End Sub
Protected Sub gvComments_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvComments.PageIndexChanging
gvComments.PageIndex = e.NewPageIndex
bindComments()
End Sub
Protected Sub btnSubmitComment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitComment.Click
Dim userType As String = "attendee"
If Not IsNothing(Request.QueryString("user")) AndAlso Request.QueryString("user").Length > 0 Then
userType = Request.QueryString("user")
End If
dataaccess.NoReturnQuery("INSERT INTO Comments (UserName, UserComment, UserType) VALUES ('" & txtName.Text.Replace("'", "''") & "','" & txtComment.Text.Replace("'", "''").Replace(vbCrLf, "<br />") & "','" & userType & "')", Data.CommandType.Text)
txtComment.Text = ""
txtName.Text = ""
gvComments.PageIndex = 0
bindComments()
End Sub
Sub DeleteComment(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lnk As LinkButton = TryCast(sender, LinkButton)
If Not IsNothing(lnk) AndAlso IsNumeric(lnk.CommandArgument) Then
dataaccess.NoReturnQuery("DELETE FROM Comments WHERE CommentID = " & lnk.CommandArgument, Data.CommandType.Text)
bindComments()
End If
End Sub
Protected Sub gvComments_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvComments.RowUpdating
Dim row As GridViewRow = gvComments.Rows(e.RowIndex)
Dim txtCommentEdit As TextBox = CType(row.FindControl("txtCommentEdit"), TextBox)
Dim txtNameEdit As TextBox = CType(row.FindControl("txtNameEdit"), TextBox)
dataaccess.NoReturnQuery("UPDATE Comments SET UserComment = '" & txtCommentEdit.Text.Replace("'", "''").Replace(vbCrLf, "<br />") & "', UserName = '" & txtNameEdit.Text.Replace("'", "''") & "' WHERE CommentID = " & gvComments.DataKeys(e.RowIndex).Value, Data.CommandType.Text)
gvComments.EditIndex = -1
bindComments()
End Sub
Protected Sub gvComments_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvComments.RowDataBound
Dim lnkDelete As LinkButton = CType(e.Row.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
If Request.QueryString("user") = "admin" Then
lnkDelete.Visible = True
Else
lnkDelete.Visible = False
End If
End If
Dim lnkEdit As LinkButton = CType(e.Row.FindControl("lnkEdit"), LinkButton)
If Not IsNothing(lnkEdit) Then
If Request.QueryString("user") = "admin" Then
lnkEdit.Visible = True
Else
lnkEdit.Visible = False
End If
End If
End Sub
燦你分享代碼? ASPX和代碼背後都會有所幫助。 – 2009-10-04 10:17:10
我添加了我的代碼。感謝Troy的幫助。 – vipergtsrz 2009-10-04 23:44:26