ASP.NET - 如果页面回发,标签不会更新
我有一个 MainPage.Master 和另一个 ContentPage.Master,它使用 MainPage.master 作为其母版页(我认为它“继承”主母版页?不确定术语..)
在 MainPage.Master.cs 上,我在 Page_Load 中有代码查询数据库并返回一个 int (这是 DataTable 中总行数的计数),这在每个页面加载时都会使用一些包含 int 的 html 更新 asp:literal。这在更改页面时工作正常,但在回发时则不起作用。
例如,当用户单击添加到此数据表的按钮时,它会被添加,但 asp:literal 不会更改,直到我单击更改页面的链接。
回顾一下;它添加到 DataTable 但不会更新 asp:literal 中的计数。我认为这与回发有关,但不确定该怎么做。
来自 MasterPage.master.cs 的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["OrderID"] == null)
{
NoItemsInCart.Text = "<span class=\"ajax_cart_no_product\">0 items</span>";
}
else
{
var itemsincart = Cart.ItemsInCart((int)Session["OrderID"]);
NoItemsInCart.Text = string.Format("<span class=\"ajax_cart_no_product\">{0} item(s)</span>", itemsincart);
}
}
因此,当用户单击按钮添加到数据表时,此代码实际上确实有效,但最初不起作用。当我导航到另一个页面时,它会更新并显示正确的数字。
谢谢,
迈克尔
I have a MainPage.Master and another ContentPage.Master which uses MainPage.master as its masterpage ( i think it 'inherits' the main master page? not sure on terminology..)
On MainPage.Master.cs I have code within Page_Load which queries a database and returns an int (which is a count of total number of rows in a DataTable) this on each page load updates an asp:literal with some html which includes the int. This works fine during changing pages but not on postback.
For instance when a user clicks a button that adds to this DataTable it is getting added but the asp:literal doesn't change untill I click a link which changes the page.
So to recap; it adds to the DataTable but doesn't update the count in the asp:literal. I think this has something to do with postbacks but not sure what to do..
The code from MasterPage.master.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["OrderID"] == null)
{
NoItemsInCart.Text = "<span class=\"ajax_cart_no_product\">0 items</span>";
}
else
{
var itemsincart = Cart.ItemsInCart((int)Session["OrderID"]);
NoItemsInCart.Text = string.Format("<span class=\"ajax_cart_no_product\">{0} item(s)</span>", itemsincart);
}
}
So this code does actually work but not initialy when a user clicks the button to add to the DataTable. When I navigate to another page it updates and shows the correct number.
Thanks,
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望在用户单击按钮时更改此设置,请将其添加到事件方法而不是页面加载中。这应该对你有用。
If you want this to change when the user clicks a button, add it to the event method rather than on the page load. This should then work for you.