c# - Asp.Net WebForms - How to pass ViewData as param to User Control -


i new asp.net , have inherited app update. i'm going quite though have struck situation need understanding.

i have user control uc:speciesgrid takes arguments: applicationid , species should viewdata , passed arguments, cannot find syntax works. hardcoded call (that works) is:

<uc:speciesgrid runat="server" id="speciesgrid1" applicationid="6191" species="dog"/> 

i tried combinations of <%, <%=, <%# , none work. like:

<uc:speciesgrid runat="server" id="speciesgrid1" applicationid='<%appdata["noiid"]%>' species="dog"/> 

the errors same:

{"cannot create object of type 'system.int32' string representa{ion 'viewdata[\"noiid\"]' 'applicationid' property."}

it obvious whatever i've included being interpreted literally rather having server substitute in value.

i tried setting variable , using error same.

<% var applicationid = viewdata["noiid"]; %> 

what working accessing viewdata['noiid'] in user controller:

protected void page_load(object sender, eventargs e) {     ...     debug.writeline("in controller, viewdata['noiid'] is: " + viewdata["noiid"]); }  > in controller, viewdata['noiid'] is: 6191 

it seem me that:

  • user controls built initialy on server "before" application starts. hence why can't pass <% arguments.
  • and pass arguments way through viewdata , similar mechanisms interpreted later in life cycle "at run time".

however i'd prefer pass arguments parameters if @ possible.

is possible? or understanding in paragraph 2 above correct?

i think use mvc pattern in asp.net , tend pass parameter view usercontrol. it, can not use code below in view page:

<uc:speciesgrid runat="server" id="speciesgrid1" applicationid="6191" species="dog"/> 

because view page has not code behind. pass parameter usercontrol, can use code:

<% html.partial("~/views/home/speciesgridcontroller.ascx", viewdata["noiid"]); %> 

html.partial or html.renderpartial used load ascx or partial view in asp.net mvc. can viewdata["noiid"] , set variable or property of ascx in page_load this:

usercontrol


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -