数据列表给错误的ajax
我有一个放在内容占位符内部的数据列表控件.
I have a data list control which is placed inside a content place holder.
<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 →</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="<<" />
<asp:Button ID="btnNext" runat="server" Text=">>" OnClick="btnNext_Click" />
<asp:Panel ID="BottomPager_Panel" runat="server">
</asp:Panel>
</asp:Content>
单击下一步按钮时,我正在控件上进行分页并导航到下一页.
在页面加载中,我正在这样做
On click of next button i am doing paging on the control and navigating to the next page.
On page load i am doing this
if (!IsPostBack)
{
pageCount = 1;
PageNo = 1;
startPage = 6 * (PageNo - 1) + 1;
lastPage = startPage + 5;
bindDataList();
}
这是我的bindDataList代码
This is my code for 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();
}
在单击下一步按钮时,我编写了以下代码
On click of next button i have written the following code
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();
}
我面临的问题是,它每次都向我显示与第一次在页面上加载的内容相同的内容.当我调试代码时,我可以看到数据列表已加载了新记录,但未在页面上反映出来,因此我尝试了删除缓存,但它确实有所帮助.当我从触发器部分中删除下一个按钮的那一刻,它给了我适当的记录,但是整个页面都在发回我不想要的东西.这是已删除的代码
Problem i am facing is that it every time shows me the same content that is loaded the first time on the page. When i debug the code i can see that the data list is loaded with the new records but it is not reflected on the page i tried removing caching but it dint help. The moment i removed the next button from the trigger section it is giving me the proper record but the complete page is getting post back which i dont want. This is the removed code
<asp:AsyncPostBackTrigger ControlID="btnNext" />
请帮助我长期以来一直坚持下去.我是这项技术的新手.预先感谢.
Please help i am stuck with this since long. I am new to this technology. Thanks in advance.
您发布的代码不完整.
您在哪里初始化了以下值:
1. pageCount
2. PageNo
3. startPage
4. lastPage
如果我没记错的话,上面的变量被宣布为公共权利?
如果我没记错的话,您的应用程序有一个母版页.
如果是这样,则单击按钮而不是在系统上执行事件
将回发到母版页,然后显示到当前页,结果是
您要初始化的上述四个变量的值.
考虑一些处理上述变量值而不初始化的方法.
您可以使用.
会话状态或查看状态来处理这种情况...
示例:
Session ["startPage"] = startPage.ToString();
或
VieState ["startPage"] = startPage.ToString();
然后从页面事件中检索..
示例:
int startPage = Convert.ToInt(Session ["startPage"])
或
int startPage = Convert.ToInt(ViewState ["startPage"])
会话状态即将到期...记住...
您可以搜索这两种用法之间的差异,并确定哪一种是最好的...
请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.
问候,
代数
Hi,
Your code you''d posted is incomplete.
Where did you initialized the value of the following:
1. pageCount
2. PageNo
3. startPage
4. lastPage
If I am not mistaken above variable was declared as public right?
If I am not mistaken your application has a Master Page.
If so, then clicking button(s) rather executing an event the system
will postback to Master Page then to present page, resulting the
value of you four above variable to initialized.
Think something to handle the above variables value not to initialized.
You may use.
Session State or View State to handle this situation...
Example:
Session["startPage"] = startPage.ToString();
or
VieState["startPage"] = startPage.ToString();
Then on retrieving from page event..
Example:
int startPage = Convert.ToInt(Session["startPage"])
or
int startPage = Convert.ToInt(ViewState["startPage"])
Session State Expires... Remember...
You may search the differences between the two usage and decide which is best...
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Regards,
Algem
尝试将您的代码修改为:
Hi,
Try to modified your code as:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
进入
into
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMod="Conditional">
然后在您的点击事件中:
then in your click event:
UpdatePanel1.Update();
或者,如果不起作用,请尝试以下方法:
or if not work the try this one:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnNext" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="dlProdocut" EventName="Click" />
</Triggers>
<ContentTemplate>
....
....
希望对您有所帮助.
问候,
代数
....
....
Hope this could help.
Regards,
Algem