c# - Pass parameters to a user control - asp.net -


i have user control:

<user:ratingstars runat="server" product="<%= getproductid() %>" category="<%= getcategoryid() %>"></user:ratingstars> 

you can see fill in product , category calling 2 methods:

public string getproductid() {     return productid.tostring(); }  public string getcategoryid() {     return categoryid.tostring(); } 

i not understand why, in user control, when take data received (product , category) gives me "<%= getproductid() %>" instead of giving id received method...

any kindly appreciated...

edit: solved with: product='<%# getproductid() %>'

last problem: in user control have this:

 public string productid;  public string product {         {         return productid;     }     set     {         productid = value;     } } 

so, expect productid set ok in user control. unfortunately null when try use it...

is there wrote that's incorrect?

so compile-time checking, can give user control id , set product , category properties in c# this:

aspx:

<user:ratingstars id="myusercontrolid" runat="server" product="<%= getproductid() %>" category="<%= getcategoryid() %>"></user:ratingstars> 

cs:

myusercontrolid.product = getproductid(); myusercontrolid.category = getcategoryid(); 

also, 5arx mentions, once you've populated refreshing page reload control , you'll lose product , category ids. can handle using viewstate on properties in user control, this:

private const string productkey = "productviewstatekey"; public string product {         {         if (viewstate[productkey] == null)         {               // whatever want here in case it's null                // throw error, return string.empty or whatever         }         return viewstate[productkey].tostring();     }      set     {         viewstate[productkey] = value;     } } 

note: i've updated property name casing follow convention, makes more sense me way! personally, i'd suffix ids id (eg: productid) distinguish property contains product object. read more coding standards here: are there suggestions developing c# coding standards / best practices document?


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -