c# - asp.net page lifecycle issue -


i have user control "header" label display amount of item in basket. this:

protected void page_load(object sender, eventargs e) {     lblbasketcount.text = session["basketitemscount"]!=null?session["basketitemscount"].tostring():"0"; } 

then have page has method:

public void btnadditemtoshoppingcart_click(object sender, eventargs ) {     session["basketitemscount"] = (from b in db.cartitems b.crt_id == cartid select b).sum(p => p.item_quantity); } 

the problem in page life cycle additem method fired after control had page_load event. label refresh after reload of page.
edit:
header control declared in master page:

<%@ register tagprefix="asp" tagname="footer" src="~/controls/footer.ascx" %>     <asp:header id="header" runat="server" /> 

and located in controls folder.

i'm using wap in namespace sitename.controls.header

don't use page_load event (or other page events) in usercontrols. lead issues this. instead should use properties, methods , events in control.

you example have property basketcount in usercontrol:

public string basketcount {     { lblbasketcount.text; }     set { lblbasketcount.text = value; } } 

then page can use property:

var basketcount = (from b in db.cartitems b.crt_id == cartid select b).sum(p => p.item_quantity); session["basketitemscount"] = basketcount; mycontrol.basketcount = basketcount.tostring(); 

to reference control in masterpage should provide public property there maps it:

for example(in masterpage):

public header headercontrol {           {          return header;      } } 

and can call property 1 of contentpages in way(f.e. if master's type named "sitemaster"):

((sitemaster)page.master).headercontrol.basketcount = basketcount.tostring(); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -