views:

69

answers:

2

I just started learning silverlight by creating a silverlight application in Visual Web Developer 2008.

I have a public property defined in the user control. How do I access this property value in the aspx codebehind page? Please help.

A: 

Not sure what you mean but you can give your UserControl in the XAML side a name with: x:Name="myControl" next you can use this.myControl.MyProperty.

Peter Kiers
+1  A: 

You cannot access a property on the UserControl from aspx code-behind. Aspx code-behind executes on the server where as the Silverlight UserControl runs on the client.

If you want your aspx code-behind to supply data to the Silverlight application you use the "initParams" parameter for the object tag:-

<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
          <param name="source" value="ClientBin/SilverlightApplication1.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="4.0.50303.0" />
          <param name="autoUpgrade" value="true" />
                  <param name="initParams" id="initParams" runat="server" />
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=4.0.50303.0" style="text-decoration:none">
              <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
          </a>
        </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
    </form>
</body>

The above is the default object tag configuration from the test aspx page created to host the silverlight app. However there is one difference, the <param name="initParams" element has been added and marked as a server side control.

Now server-side aspx code-behind can modify the value of this param element:-

protected void Page_Load(object sender, EventArgs e)
{
    initParams.Attributes["value"] = "input=Hello";
}    

A usercontrol that wants to discover the values specified in this way can do so with code like this:-

    public MainPage()
    {
        InitializeComponent();
        SomeTextBox.Text = App.Current.Host.InitParams["input"];
    }
AnthonyWJones