更新面板中Asp Repeater的页面回发问题

时间:2015-04-27 10:33:41

标签: jquery asp.net

我正在使用两个转发器控件来显示数据库表记录,另一个用于显示我开发的自定义分页以显示分页,例如1 2 3 4 ...

当我点击1或2或3之类的分页号码时,我将两个转发器控件都保存在asp更新面板中,这样页面就会被回发但是在同一个更新面板中还有一些其他按钮,当我点击那些按钮时页面我没有回发,但是当我点击任何页码时我想停止发回。

请在我点击分页号码

时建议如何阻止页面回发

2 个答案:

答案 0 :(得分:0)

您可以对按钮应用单击处理程序以阻止默认操作。类似的东西:

<script type="text/javascript">
    $(document).ready(function() {
        $(".pagingButton").click(function (e) {
            e.preventDefault();
        });
    });

</script>
<asp:Button runat="server" ID="btnPage1" CssClass="pagingButton"/>
<asp:Button runat="server" ID="btnPage2" CssClass="pagingButton"/>
<asp:Button runat="server" ID="btnPage3" CssClass="pagingButton"/>

http://api.jquery.com/event.preventdefault/

希望这有帮助

答案 1 :(得分:0)

这是我在没有页面postback的情况下工作的示例。请按照此示例。

在aspx页面中,只需粘贴以下代码即可。 ScriptManager用于异步分页。

第1步:

<form id="form1" runat="server">   
<asp:ScriptManager ID="MainScriptManager" runat="server" />
    <asp:UpdatePanel ID="pnlHelloWorld" runat="server">        
        <ContentTemplate>
            <div>
                <asp:Repeater ID="rptItems" runat="server">        
                    <ItemTemplate>            
                        <div>
                            <div>
                                <%# DataBinder.Eval(Container.DataItem,"Address") %>
                            </div>
                            <div>
                                <%# DataBinder.Eval(Container.DataItem, "ProductCategory")%>
                            </div>
                            <div>
                                <%# DataBinder.Eval(Container.DataItem, "BrandName")%>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
            <div>
               <asp:LinkButton ID="lbPrev" runat="server" OnCommand="lbPrev_Command">Prev </asp:LinkButton> 
                <asp:Repeater ID="rptPages" runat="server" OnItemDataBound="rptPaging_ItemDataBound">       
                    <ItemTemplate>
                        <div style="float:left;padding-left:10px;">
                            <asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#Container.DataItem %>" CssClass="text" runat="server"><%# Container.DataItem %>
                            </asp:LinkButton>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
               <asp:LinkButton ID="lbNext" runat="server" OnCommand="lbNext_Command">Next </asp:LinkButton> 
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>     
</form>

第2步:

public static int totalPages = 0;
public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        LoadData();
}

private void LoadData()
{
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conTest"].ToString());
    cn.Open();
    SqlDataAdapter da = new SqlDataAdapter("select BrandName,ProductCategory,Address from  Addresss", cn); //U can use your query here
    DataTable dt = new DataTable();
    da.Fill(dt);
    cn.Close();

    PagedDataSource pgitems = new PagedDataSource();
    DataView dv = new DataView(dt);
    pgitems.DataSource = dv;

    pgitems.AllowPaging = true;
    pgitems.PageSize = 10;
    pgitems.CurrentPageIndex = PageNumber;
    totalPages = pgitems.PageCount - 1;

    if (pgitems.PageCount > 1)
    {
        rptPages.Visible = true;
        ArrayList pages = new ArrayList();
        for (int i = 0; i < pgitems.PageCount; i++)
            pages.Add((i + 1).ToString());
        rptPages.DataSource = pages;
        rptPages.DataBind();
    }
    else
        rptPages.Visible = false;

    rptItems.DataSource = pgitems;
    rptItems.DataBind();
}

void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
    LoadData();
}



protected void lbPrev_Command(Object sender, CommandEventArgs e)
    {
        if (PageNumber == 0)
        {
            PageNumber = 0;
        }
        else
        {
            PageNumber = PageNumber - 1;
        }
        LoadData();
    }

protected void lbNext_Command(Object sender, CommandEventArgs e)
{
    if (PageNumber == totalPages)
    {
        PageNumber = totalPages;
    }
    else
    {
        PageNumber = PageNumber + 1;
    }        
    LoadData();
}

用于分页

protected void rptPaging_ItemDataBound(object source, RepeaterItemEventArgs e)
{
    LinkButton lnk = (LinkButton)e.Item.FindControl("btnPage");

    if (PageNumber > 0)
    {            
        if (lnk.CommandArgument.ToString() == (PageNumber + 1).ToString())
        {
            lnk.ForeColor = System.Drawing.Color.Red;
            lnk.Font.Bold = true;
            lnk.Attributes.CssStyle.Add("text-decoration", "none");
            lnk.Attributes.CssStyle.Add("pointer-events", "none");
        }
        else
        {
            lnk.ForeColor = System.Drawing.Color.Black;
        }
    }
    else if(lnk.CommandArgument.ToString() == "1")
    {
        lnk.Font.Bold = true;
        lnk.Attributes.CssStyle.Add("text-decoration", "none");
        lnk.ForeColor = System.Drawing.Color.Red;
        lnk.Attributes.CssStyle.Add("pointer-events", "none");
    }
}