ASP.NET - If page is postback, label doesn't update
I have a MainPage.M开发者_Go百科aster 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
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.
精彩评论