3

This is the ascx control in .aspx

<Menu:MNU ID="PMPHeaderMenu" runat="server" HiLiter="<%=h%>"></Menu:MNU>

in aspx.cs I have

  public int h = 1;
  ....
  ....
  h = 5;

in ascx.cs I have the HiLiter Property

   public string HiLiter { get; set; }

When I debug I get the value as <%=h%> for HiLiter when I expect it to be 5.

How will I pass the server side set value to the user control?

Venkat
  • 1,702
  • 2
  • 27
  • 47
  • 1
    Possible duplicate of [Using c# in Web Forms to passing parameter to user control](https://stackoverflow.com/questions/48811256/using-c-sharp-in-web-forms-to-passing-parameter-to-user-control) – VDWWD Feb 19 '18 at 10:21
  • [Check this](https://stackoverflow.com/a/1393407/5558443) – NnN Feb 19 '18 at 10:35

1 Answers1

4

You can't use <%=%> for controls with runat="server" for setting properties, <%=%> is similar like Response.Write.

<%# %> (Data-binding expressions) can be used to fill control properties, but the control should be inside a data-binding container like GridView, DetailsView, FormView and Repeater.

In your case you should set the value of the property from the code behind (aspx.cs) page like following.

 PMPHeaderMenu.HiLiter = h;
 this.DataBind();
Venkat
  • 1,702
  • 2
  • 27
  • 47
PSK
  • 17,547
  • 5
  • 32
  • 43
  • 1
    @venkat not sure why you added this.DataBind(); – PSK Feb 20 '18 at 04:31
  • The control neednot be in a databinding container like GridView, DetailsView as you mentioned. Using this.DataBind() serves the purpose. – Venkat Feb 20 '18 at 04:40
  • If you assigning the property through code behind you don't need to write this.DataBind(); and it's also not necessary it to be a container. Issue is only with Data-binding expressions – PSK Feb 20 '18 at 04:43
  • I had to use this.DataBind() in code behind otherwise it didnot work. I am not sure why. – Venkat Feb 20 '18 at 04:44
  • surprising, I tried the exact and it worked for me :) – PSK Feb 20 '18 at 04:46