Ajax与数据给出错误

本文关键字:出错 错误 数据 Ajax | 更新日期: 2023-09-27 18:02:55

我有一个数据列表控件,它被放置在一个内容占位符中。

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:ScriptManager ID="MainScriptManager" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:DataList ID="dlProdocut" runat="server" RepeatColumns="3" EditItemIndex="-1"
                RepeatDirection="Horizontal" OnItemDataBound="dlProdocut_ItemDataBound">
                <ItemTemplate>
                    <asp:Table ID="Table1" runat="server" border="0" CellSpacing="0" CellPadding="0">
                        <asp:TableRow>
                            <asp:TableCell Height="10" HorizontalAlign="Center" VerticalAlign="Top">
                            </asp:TableCell>
                        </asp:TableRow>
                        <%--Image--%>
                        <asp:TableRow>
                            <asp:TableCell Height="150" Width="7" HorizontalAlign="left" VerticalAlign="top">
                                <asp:HyperLink ID="hyrProductImg" runat="server">
                              <img alt='<%# DataBinder.Eval(Container.DataItem,"Title")%>' src="../images/<%# DataBinder.Eval(Container.DataItem,"SmallImage") %>" border="0" width="226" height="166" />
                                </asp:HyperLink>
                            </asp:TableCell>
                            <asp:TableCell Width="5">  </asp:TableCell>
                        </asp:TableRow>
                        <%--Title--%>
                        <asp:TableRow>
                            <asp:TableCell Height="45" Width="7" CssClass="product-name" HorizontalAlign="Center"
                                VerticalAlign="Top">
                            <strong> <%# DataBinder.Eval(Container.DataItem, "Title")%></strong>
                            </asp:TableCell>
                        </asp:TableRow>
                        <%--ShortDescription--%>
                        <asp:TableRow>
                            <asp:TableCell Width="7" HorizontalAlign="Justify" VerticalAlign="Top" CssClass="testimonial-text">
                                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ShortDescription")%>'></asp:Label>
                            </asp:TableCell>
                        </asp:TableRow>
                        <%--Read More--%>
                        <asp:TableRow>
                            <asp:TableCell HorizontalAlign="Left">
                                <asp:HyperLink ID="lnkProductDetails" CssClass="read-more" runat="server">Read More &rarr;</asp:HyperLink>
                            </asp:TableCell>
                        </asp:TableRow>
                        <asp:TableRow>
                            <asp:TableCell Height="60" HorizontalAlign="Justify" VerticalAlign="Top" CssClass="testimonial-text">
                            </asp:TableCell>
                        </asp:TableRow>
                    </asp:Table>
                </ItemTemplate>
            </asp:DataList>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="dlProdocut" />
            <asp:AsyncPostBackTrigger ControlID="btnNext" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:Label ID="lblPage" runat="server" Text="" />
    <asp:Button ID="btnPrevious" runat="server" Text="&lt;&lt;" />
    <asp:Button ID="btnNext" runat="server" Text="&gt;&gt;" OnClick="btnNext_Click" />
    <asp:Panel ID="BottomPager_Panel" runat="server">
    </asp:Panel>
</asp:Content>

单击下一个按钮,我正在对控件进行分页并导航到下一页。在页面加载时,我这样做

if (!IsPostBack)
    {
        pageCount = 1;
        PageNo = 1;
        startPage = 6 * (PageNo - 1) + 1;
         lastPage = startPage + 5;
         bindDataList();
    }

这是我的代码bindDataList

public void bindDataList()
{
    string source = ConfigurationManager.ConnectionStrings["Cad-B"].ToString();
    SqlConnection con = new SqlConnection(source);
    SqlCommand cmd = new SqlCommand("sp_GetProductPagingByMenuId", con);
    //cmd.CommandType = CommandType.StoredProcedure;
    //cmd.CommandText = "sp_GetProductPagingByMenuId";
    con.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@startPage", startPage);
    cmd.Parameters.Add("@lastPage", lastPage);
    SqlDataReader dr =    cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    dlProdocut.DataSource = dt;
    dlProdocut.DataBind();
}

在点击下一步按钮时,我编写了以下代码

protected void btnNext_Click(object sender, EventArgs e)
{
    dlProdocut.DataSource = null;
    dlProdocut.DataBind();
    pageCount++;
    PageNo = pageCount;
    startPage = 6 * (PageNo - 1) + 1;
    lastPage = startPage + 5;
    bindDataList();
}

我面临的问题是,它每次显示我相同的内容是加载在页面上的第一次。当我调试代码时,我可以看到数据列表加载了新的记录,但它没有反映在我试图删除缓存的页面上,但它没有帮助。那一刻,我从触发器部分删除了下一个按钮,它给我适当的记录,但完整的页面是得到post back,我不想要。这是被删除的代码

<asp:AsyncPostBackTrigger ControlID="btnNext" />

请帮助我被困在这个很长时间。我是这项技术的新手。

Ajax与数据给出错误

尝试:

<Triggers> 
<asp:AsyncPostBackTrigger ControlID="DatalistId$btnNext" />
</Triggers>
相关文章: