开发者

ASP.NET - If page is postback, label doesn't update

开发者 https://www.devze.com 2023-03-02 00:37 出处:网络
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..)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号