I am new to ASP.net and have inherited an app to update. I'm going quite well though have struck a situation I need some help understanding.
I have a user control uc:speciesgrid
that takes some arguments: applicationId
and species
should be from ViewData and passed as arguments, however I cannot find a syntax that works. A hardcoded call to it (that works) is:
<uc:speciesgrid runat="server" ID="speciesgrid1" applicationId="6191" species="Dog"/>
I tried all combinations of <%, <%=, <%#
and none will work. So something like:
<uc:speciesgrid runat="server" ID="speciesgrid1" applicationId='<%AppData["NoiId"]%>' species="Dog"/>
The errors are all the same:
{"Cannot create an object of type 'System.Int32' from its string representa{ion 'ViewData[\"NoiId\"]' for the 'applicationId' property."}
It is obvious that whatever I've included is being interpreted literally rather than having the server substitute in the value.
I also tried setting a variable and using that but the error is the same.
<%
var applicationId = ViewData["NoiId"];
%>
What IS working is accessing ViewData['NoiId']
in the User Controller:
protected void Page_Load(object sender, EventArgs e)
{
...
Debug.WriteLine("In the controller, ViewData['NoiId'] is: " + ViewData["NoiId"]);
}
> In the controller, ViewData['NoiId'] is: 6191
It would seem to me that:
- User Controls are built initialy on the server "before" that application starts. Hence why I can't pass any
<%
arguments. - And to pass arguments the only way is through
ViewData
and similar mechanisms that are interpreted later in the life cycle "at run time".
However I'd prefer pass arguments as parameters if at all possible.
Is it possible? Or is my understanding in that paragraph 2 above correct?