設定Gridview,Repter...中的linkbutton按鈕不可用

赤砂之蠍我愛羅發表於2012-12-03

假設在Gridview中的分頁控制元件中有linkbutton:

<PagerTemplate>
                                當前第:<asp:Label ID="lblCurrentPage" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>">
                                </asp:Label>頁 | 
                                一共:
                                <asp:Label ID="lblAllPage" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>頁
                                <asp:LinkButton ID="lnkFirstPage" runat="server" OnClientClick="return clearData();"
                                    CommandName="Page" CommandArgument="First"
                                      Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>">第一頁</asp:LinkButton>
                                <asp:LinkButton ID="lnkUpPage" runat="server" OnClientClick="return clearData();"
                                    CommandName="Page" CommandArgument="Prev" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>">上一頁</asp:LinkButton>
                                <asp:LinkButton ID="lnkDownPage" runat="server" OnClientClick="return clearData();"
                                    CommandName="Page" CommandArgument="Next" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != ((GridView)Container.Parent.Parent).PageCount - 1 %>">下一頁</asp:LinkButton>
                                <asp:LinkButton ID="lnkLastPage" runat="server" OnClientClick="return clearData();"
                                    CommandName="Page" CommandArgument="Last" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != ((GridView)Container.Parent.Parent).PageCount - 1 %>">最後一頁</asp:LinkButton>
                                跳轉到:
                                <asp:TextBox ID="txtNeedPage" Width="20px" runat="server" onkeyup='value=value.replace(/[^\d]/g,"") '
                                    onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
                                    Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:TextBox>
                                <asp:LinkButton ID="lnkGoto" runat="server" CommandName="Page" OnClientClick="return clearData();"
                                    CommandArgument="-2">Go</asp:LinkButton>
                                跳轉到:
                                <asp:DropDownList ID="ddlNeedPage" runat="server"  onchange="javascript:selectChange();"
                                AutoPostBack="true" OnSelectedIndexChanged="ddlNeedPage_SelectedIndexChanged">
                                </asp:DropDownList>
                            </PagerTemplate>

設定了當在首頁的時候:“首頁”,“第一頁”不可點選,雖然按鈕已經禁用了!


但事實上:他還是可以點選的,不信你試試!!!


經過除錯:發現LinkButton首頁 lnkFirstPage的OnClientClick居然還有值!


解決辦法:在Gridview的_gvGuest_RowDataBound讓OnClientClick為空就行了!

LinkButton lnkFirstPage = e.Row.FindControl("lnkFirstPage") as LinkButton;
                if (lnkFirstPage.Enabled == false)
                    lnkFirstPage.OnClientClick = "";

if (e.Row.RowType == DataControlRowType.Pager)
            { 
                LinkButton lnkFirstPage = e.Row.FindControl("lnkFirstPage") as LinkButton;
                if (lnkFirstPage.Enabled == false)
                    lnkFirstPage.OnClientClick = "";
                LinkButton lnkUpPage = e.Row.FindControl("lnkUpPage") as LinkButton;
                if (lnkUpPage.Enabled == false)
                    lnkUpPage.OnClientClick = "";
                LinkButton lnkDownPage = e.Row.FindControl("lnkDownPage") as LinkButton;
                if (lnkDownPage.Enabled == false)
                    lnkDownPage.OnClientClick = "";
                LinkButton lnkLastPage = e.Row.FindControl("lnkLastPage") as LinkButton;
                if (lnkLastPage.Enabled == false)
                    lnkLastPage.OnClientClick = "";
            }



相關文章